forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
133 lines (117 loc) · 4.41 KB
/
init.lua
File metadata and controls
133 lines (117 loc) · 4.41 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
local utils = require('CopilotChat.utils')
local M = {}
local default_prompts = {
Explain = 'Explain how it works.',
Tests = 'Briefly explain how selected code works then generate unit tests.',
}
_COPILOT_CHAT_GLOBAL_CONFIG = {}
-- Set up the plugin
---@param options (table | nil)
-- - show_help: ('yes' | 'no') default: 'yes'.
-- - disable_extra_info: ('yes' | 'no') default: 'yes'.
-- - hide_system_prompt: ('yes' | 'no') default: 'yes'.
-- - proxy: (string?) default: ''.
-- - language: (string?) default: ''.
-- - temperature: (string?) default: '0.1'. Value between 0.0 and 1.0.
-- - prompts: (table?) default: default_prompts.
-- - debug: (boolean?) default: false.
M.setup = function(options)
vim.g.copilot_chat_show_help = options and options.show_help or 'yes'
vim.g.copilot_chat_disable_separators = options and options.disable_extra_info or 'yes'
vim.g.copilot_chat_hide_system_prompt = options and options.hide_system_prompt or 'yes'
vim.g.copilot_chat_proxy = options and options.proxy or ''
vim.g.copilot_chat_language = options and options.language or ''
vim.g.copilot_chat_temperature = options and options.temperature or '0.1'
local debug = options and options.debug or false
_COPILOT_CHAT_GLOBAL_CONFIG.debug = debug
-- Merge the provided prompts with the default prompts
local prompts = vim.tbl_extend('force', default_prompts, options and options.prompts or {})
vim.g.copilot_chat_user_prompts = prompts
-- Loop through merged table and generate commands based on keys.
for key, value in pairs(prompts) do
utils.create_cmd('CopilotChat' .. key, function()
vim.cmd('CopilotChat ' .. value)
end, { nargs = '*', range = true })
end
-- Troubleshoot and fix the diagnostic issue at the current cursor position.
utils.create_cmd('CopilotChatFixDiagnostic', function()
local diagnostic = utils.get_diagnostics()
if diagnostic == 'No diagnostics available' then
vim.notify('No diagnostic issue found at the current cursor position.', vim.log.levels.INFO)
return
end
local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
-- Copy all the lines from current buffer to unnamed register
vim.cmd('normal! ggVG"*y')
vim.cmd(
'CopilotChat Please assist with the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
)
end, { nargs = '*', range = true })
-- Show debug info
utils.create_cmd('CopilotChatDebugInfo', function()
-- Get the log file path
local log_file_path = utils.get_log_file_path()
-- Get the rplugin path
local rplugin_path = utils.get_remote_plugins_path()
-- Create a popup with the log file path
local lines = {
'CopilotChat.nvim Info:',
'- Log file path: ' .. log_file_path,
'- Rplugin path: ' .. rplugin_path,
'If you are facing issues, run `:checkhealth CopilotChat` and share the output.',
'There is a common issue is "Ambiguous use of user-defined command". Please check the pin issues on the repository.',
'Press `q` to close this window.',
'Press `?` to open the rplugin file.',
}
local width = 0
for _, line in ipairs(lines) do
width = math.max(width, #line)
end
local height = #lines
local opts = {
relative = 'editor',
width = width + 4,
height = height + 2,
row = (vim.o.lines - height) / 2 - 1,
col = (vim.o.columns - width) / 2,
style = 'minimal',
border = 'rounded',
}
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_open_win(bufnr, true, opts)
-- Bind 'q' to close the window
vim.api.nvim_buf_set_keymap(
bufnr,
'n',
'q',
'<cmd>close<CR>',
{ noremap = true, silent = true }
)
-- Bind `?` to open remote plugin detail
vim.api.nvim_buf_set_keymap(
bufnr,
'n',
'?',
-- Close the current window and open the rplugin file
'<cmd>close<CR><cmd>edit '
.. rplugin_path
.. '<CR>',
{ noremap = true, silent = true }
)
end, {
nargs = '*',
range = true,
})
utils.log_info(
'Execute ":UpdateRemotePlugins" and restart Neovim before starting a chat with Copilot.'
)
utils.log_info('If issues arise, run ":healthcheck" and share the output.')
end
return M