Skip to content

Commit 43d9721

Browse files
deathbeamgptlang
authored andcommitted
refactor: rewrite the plugin to be lua-based (#83)
* Refactor the plugin to be lua-based Signed-off-by: Tomas Slusny <slusnucky@gmail.com> * Add open/close/toggle commands and function * Add support for prompt.mapping to map prompts to keys * Fix issue with system_prompt replace not using correct value and fix naming of USER_ prompts * Move some of chat buffer logic to separate file, allow changing window layout properly * Disable python part of the plugin for now * Fix check if message is copilot message Signed-off-by: Tomas Slusny <slusnucky@gmail.com> --------- Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent c51783a commit 43d9721

11 files changed

Lines changed: 1293 additions & 499 deletions

File tree

lua/CopilotChat/chat.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local Spinner = require('CopilotChat.spinner')
2+
local class = require('CopilotChat.utils').class
3+
4+
local function create_buf()
5+
local bufnr = vim.api.nvim_create_buf(false, true)
6+
vim.api.nvim_buf_set_name(bufnr, 'copilot-chat')
7+
vim.bo[bufnr].filetype = 'markdown'
8+
vim.treesitter.start(bufnr, 'markdown')
9+
return bufnr
10+
end
11+
12+
local Chat = class(function(self, name)
13+
self.bufnr = create_buf()
14+
self.spinner = Spinner(self.bufnr, name)
15+
end)
16+
17+
function Chat:validate()
18+
if not vim.api.nvim_buf_is_valid(self.bufnr) then
19+
self.bufnr = create_buf()
20+
self.spinner.bufnr = self.bufnr
21+
end
22+
end
23+
24+
function Chat:append(str)
25+
self:validate()
26+
27+
local last_line, last_column = self:last()
28+
vim.api.nvim_buf_set_text(
29+
self.bufnr,
30+
last_line,
31+
last_column,
32+
last_line,
33+
last_column,
34+
vim.split(str, '\n')
35+
)
36+
37+
return self:last()
38+
end
39+
40+
function Chat:last()
41+
self:validate()
42+
43+
local last_line = vim.api.nvim_buf_line_count(self.bufnr) - 1
44+
local last_line_content = vim.api.nvim_buf_get_lines(self.bufnr, -2, -1, false)
45+
local last_column = #last_line_content[1]
46+
return last_line, last_column
47+
end
48+
49+
function Chat:clear()
50+
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
51+
end
52+
53+
return Chat

lua/CopilotChat/code_actions.lua

Lines changed: 46 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,106 +4,73 @@ local telescope_pickers = require('telescope.pickers')
44
local finders = require('telescope.finders')
55
local themes = require('telescope.themes')
66
local conf = require('telescope.config').values
7-
local utils = require('CopilotChat.utils')
7+
local select = require('CopilotChat.select')
8+
local chat = require('CopilotChat')
89

910
local help_actions = {}
1011
local user_prompt_actions = {}
1112

1213
local function generate_fix_diagnostic_prompt()
13-
local diagnostic = utils.get_diagnostics()
14-
if diagnostic == 'No diagnostics available' then
15-
return diagnostic
14+
local diagnostic = select.diagnostics()
15+
if not diagnostic then
16+
return 'No diagnostics available'
1617
end
1718

18-
local file_name = vim.fn.expand('%:t')
19-
local line_number = vim.fn.line('.')
2019
return 'Please assist with fixing the following diagnostic issue in file: "'
21-
.. file_name
22-
.. ':'
23-
.. line_number
24-
.. '". '
25-
.. diagnostic
20+
.. diagnostic.prompt_extra
2621
end
2722

2823
local function generate_explain_diagnostic_prompt()
29-
local diagnostic = utils.get_diagnostics()
30-
if diagnostic == 'No diagnostics available' then
31-
return diagnostic
24+
local diagnostic = select.diagnostics()
25+
if not diagnostic then
26+
return 'No diagnostics available'
3227
end
3328

34-
local file_name = vim.fn.expand('%:t')
35-
local line_number = vim.fn.line('.')
36-
return 'Please explain the following diagnostic issue in file: "'
37-
.. file_name
38-
.. ':'
39-
.. line_number
40-
.. '". '
41-
.. diagnostic
29+
return 'Please explain the following diagnostic issue in file: "' .. diagnostic.prompt_extra
4230
end
4331

4432
--- Help command for telescope picker
45-
--- This will copy all the lines in the buffer to the unnamed register
33+
--- This will send whole buffer to copilot
4634
--- Then will send the diagnostic to copilot chat
47-
---@param prefix string
48-
local function diagnostic_help_command(prefix)
49-
if prefix == nil then
50-
prefix = ''
51-
else
52-
prefix = prefix .. ' '
53-
end
54-
55-
return function(prompt_bufnr, _)
56-
actions.select_default:replace(function()
57-
actions.close(prompt_bufnr)
58-
local selection = action_state.get_selected_entry()
59-
60-
-- Select all the lines in the buffer to uname register
61-
vim.cmd('normal! ggVG"*y')
62-
63-
-- Get value from the help_actions and execute the command
64-
local value = ''
65-
for _, action in pairs(help_actions) do
66-
if action.name == selection[1] then
67-
value = action.label
68-
break
69-
end
35+
local function diagnostic_help_command(prompt_bufnr, _)
36+
actions.select_default:replace(function()
37+
actions.close(prompt_bufnr)
38+
local selection = action_state.get_selected_entry()
39+
40+
-- Get value from the help_actions and execute the command
41+
local value = ''
42+
for _, action in pairs(help_actions) do
43+
if action.name == selection[1] then
44+
value = action.label
45+
break
7046
end
47+
end
7148

72-
vim.cmd(prefix .. value)
73-
end)
74-
return true
75-
end
49+
chat.ask(value, { selection = select.buffer })
50+
end)
51+
return true
7652
end
7753

7854
--- Prompt command for telescope picker
7955
--- This will show all the user prompts in the telescope picker
8056
--- Then will execute the command selected by the user
81-
---@param prefix string
82-
local function generate_prompt_command(prefix)
83-
if prefix == nil then
84-
prefix = ''
85-
else
86-
prefix = prefix .. ' '
87-
end
88-
89-
return function(prompt_bufnr, _)
90-
actions.select_default:replace(function()
91-
actions.close(prompt_bufnr)
92-
local selection = action_state.get_selected_entry()
93-
94-
-- Get value from the prompt_actions and execute the command
95-
local value = ''
96-
for _, action in pairs(user_prompt_actions) do
97-
if action.name == selection[1] then
98-
value = action.label
99-
break
100-
end
57+
local function generate_prompt_command(prompt_bufnr, _)
58+
actions.select_default:replace(function()
59+
actions.close(prompt_bufnr)
60+
local selection = action_state.get_selected_entry()
61+
62+
-- Get value from the prompt_actions and execute the command
63+
local value = ''
64+
for _, action in pairs(user_prompt_actions) do
65+
if action.name == selection[1] then
66+
value = action.label
67+
break
10168
end
69+
end
10270

103-
vim.cmd(prefix .. value)
104-
end)
105-
return true
106-
end
71+
chat.ask(value)
72+
end)
73+
return true
10774
end
10875

10976
local function show_help_actions()
@@ -136,25 +103,18 @@ local function show_help_actions()
136103
results = action_names,
137104
}),
138105
sorter = conf.generic_sorter(opts),
139-
attach_mappings = diagnostic_help_command('CopilotChat'),
106+
attach_mappings = diagnostic_help_command,
140107
})
141108
:find()
142109
end
143110

144111
--- Show prompt actions
145-
---@param is_in_visual_mode boolean?
146-
local function show_prompt_actions(is_in_visual_mode)
112+
local function show_prompt_actions()
147113
-- Convert user prompts to a table of actions
148114
user_prompt_actions = {}
149115

150-
for key, prompt in pairs(vim.g.copilot_chat_user_prompts) do
151-
table.insert(user_prompt_actions, { name = key, label = prompt })
152-
end
153-
154-
local cmd = 'CopilotChat'
155-
156-
if is_in_visual_mode then
157-
cmd = "'<,'>CopilotChatVisual"
116+
for key, prompt in pairs(chat.get_prompts(true)) do
117+
table.insert(user_prompt_actions, { name = key, label = prompt.prompt })
158118
end
159119

160120
-- Show the menu with telescope pickers
@@ -170,7 +130,7 @@ local function show_prompt_actions(is_in_visual_mode)
170130
results = action_names,
171131
}),
172132
sorter = conf.generic_sorter(opts),
173-
attach_mappings = generate_prompt_command(cmd),
133+
attach_mappings = generate_prompt_command,
174134
})
175135
:find()
176136
end

0 commit comments

Comments
 (0)