forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
77 lines (64 loc) · 1.94 KB
/
utils.lua
File metadata and controls
77 lines (64 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local M = {}
local log = require('CopilotChat.vlog')
--- Get the log file path
---@return string
M.get_log_file_path = function()
return log.get_log_file()
end
-- The CopilotChat.nvim is built using remote plugins.
-- This is the path to the rplugin.vim file.
-- Refer https://neovim.io/doc/user/remote_plugin.html#%3AUpdateRemotePlugins
-- @return string
M.get_remote_plugins_path = function()
local os = vim.loop.os_uname().sysname
if os == 'Linux' or os == 'Darwin' then
return '~/.local/share/nvim/rplugin.vim'
else
return '~/AppData/Local/nvim/rplugin.vim'
end
end
--- Create custom command
---@param cmd string The command name
---@param func function The function to execute
---@param opt table The options
M.create_cmd = function(cmd, func, opt)
opt = vim.tbl_extend('force', { desc = 'CopilotChat.nvim ' .. cmd }, opt or {})
vim.api.nvim_create_user_command(cmd, func, opt)
end
--- Log info
---@vararg any
M.log_info = function(...)
-- Only save log when debug is on
if not _COPILOT_CHAT_GLOBAL_CONFIG.debug then
return
end
log.info(...)
end
--- Log error
---@vararg any
M.log_error = function(...)
-- Only save log when debug is on
if not _COPILOT_CHAT_GLOBAL_CONFIG.debug then
return
end
log.error(...)
end
--- Get diagnostics for the current line
--- It uses the built-in LSP client in Neovim to get the diagnostics.
--- @return string
M.get_diagnostics = function()
local buffer_number = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
local line_diagnostics = vim.lsp.diagnostic.get_line_diagnostics(buffer_number, cursor[1] - 1)
if #line_diagnostics == 0 then
return 'No diagnostics available'
end
local diagnostics = {}
for _, diagnostic in ipairs(line_diagnostics) do
table.insert(diagnostics, diagnostic.message)
end
local result = table.concat(diagnostics, '. ')
result = result:gsub('^%s*(.-)%s*$', '%1'):gsub('\n', ' ')
return result
end
return M