Skip to content

Commit a1de0aa

Browse files
committed
refactor!: centralize sticky prompt and selection handling
This refactoring improves the CopilotChat plugin's state management by: - Adding `insert_sticky` function to centralize sticky prompt management - Creating `set_selection` to provide a cleaner API for selection handling - Consolidating highlight management in `update_highlights` - Standardizing function signatures and return values - Improving `Chat:set_prompt` functionality - Removing redundant code for better maintainability BREAKING CHANGES: - `M.resolve_prompt` now returns config first and prompt second - `M.update_selection` removed in favor of new `M.set_selection` function - `M.resolve_embeddings` renamed to `M.resolve_context` - Selection setting now only configurable in global config, not per-operation - `get_selection` no longer accepts a config parameter - Selection handling moved from dynamic config to static global config - Changed sticky format and processing approach - Prompts in config now use explicit `sticky` property instead of prefixing Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent bdddfeb commit a1de0aa

File tree

7 files changed

+262
-174
lines changed

7 files changed

+262
-174
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,6 @@ Below are all available configuration options with their default values:
453453
headless = false, -- Do not write to chat buffer and use history(useful for using callback for custom processing)
454454
callback = nil, -- Callback to use when ask response is received
455455

456-
-- default selection
457-
selection = function(source)
458-
return select.visual(source) or select.buffer(source)
459-
end,
460-
461456
-- default window options
462457
window = {
463458
layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace'
@@ -499,6 +494,12 @@ Below are all available configuration options with their default values:
499494
error_header = '# Error ', -- Header to use for errors
500495
separator = '───', -- Separator to use in chat
501496

497+
-- default selection
498+
-- see config/select.lua for implementation
499+
selection = function(source)
500+
return select.visual(source) or select.buffer(source)
501+
end,
502+
502503
-- default providers
503504
-- see config/providers.lua for implementation
504505
providers = {
@@ -535,10 +536,12 @@ Below are all available configuration options with their default values:
535536
-- see config/prompts.lua for implementation
536537
prompts = {
537538
Explain = {
538-
prompt = '> /COPILOT_EXPLAIN\n\nWrite an explanation for the selected code as paragraphs of text.',
539+
prompt = 'Write an explanation for the selected code as paragraphs of text.',
540+
sticky = '/COPILOT_EXPLAIN',
539541
},
540542
Review = {
541-
prompt = '> /COPILOT_REVIEW\n\nReview the selected code.',
543+
prompt = 'Review the selected code.',
544+
sticky = '/COPILOT_REVIEW',
542545
},
543546
Fix = {
544547
prompt = 'There is a problem in this code. Identify the issues and rewrite the code with fixes. Explain what was wrong and how your changes address the problems.',
@@ -553,7 +556,8 @@ Below are all available configuration options with their default values:
553556
prompt = 'Please generate tests for my code.',
554557
},
555558
Commit = {
556-
prompt = '> #git:staged\n\nWrite commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block.',
559+
prompt = 'Write commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block.',
560+
sticky = '#git:staged',
557561
},
558562
},
559563

lua/CopilotChat/client.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
---@class CopilotChat.Client.agent : CopilotChat.Provider.agent
1515
---@field provider string
1616

17-
local async = require('plenary.async')
1817
local log = require('plenary.log')
1918
local tiktoken = require('CopilotChat.tiktoken')
2019
local notify = require('CopilotChat.notify')

lua/CopilotChat/config.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ local select = require('CopilotChat.select')
2222
---@field temperature number?
2323
---@field headless boolean?
2424
---@field callback fun(response: string, source: CopilotChat.source)?
25-
---@field selection false|nil|fun(source: CopilotChat.source):CopilotChat.select.selection?
2625
---@field window CopilotChat.config.window?
2726
---@field show_help boolean?
2827
---@field show_folds boolean?
@@ -47,6 +46,7 @@ local select = require('CopilotChat.select')
4746
---@field answer_header string?
4847
---@field error_header string?
4948
---@field separator string?
49+
---@field selection false|nil|fun(source: CopilotChat.source):CopilotChat.select.selection?
5050
---@field providers table<string, CopilotChat.Provider>?
5151
---@field contexts table<string, CopilotChat.config.context>?
5252
---@field prompts table<string, CopilotChat.config.prompt|string>?
@@ -66,11 +66,6 @@ return {
6666
headless = false, -- Do not write to chat buffer and use history(useful for using callback for custom processing)
6767
callback = nil, -- Callback to use when ask response is received
6868

69-
-- default selection
70-
selection = function(source)
71-
return select.visual(source) or select.buffer(source)
72-
end,
73-
7469
-- default window options
7570
window = {
7671
layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace'
@@ -113,6 +108,11 @@ return {
113108
error_header = '## Error ', -- Header to use for errors
114109
separator = '───', -- Separator to use in chat
115110

111+
-- default selection
112+
selection = function(source)
113+
return select.visual(source) or select.buffer(source)
114+
end,
115+
116116
-- default providers
117117
providers = require('CopilotChat.config.providers'),
118118

lua/CopilotChat/config/mappings.lua

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local utils = require('CopilotChat.utils')
55
---@param chat CopilotChat.ui.Chat
66
---@return CopilotChat.ui.Diff.Diff?
77
local function get_diff(chat)
8-
local config = chat.config
98
local block = chat:get_closest_block()
109

1110
-- If no block found, return nil
@@ -15,7 +14,7 @@ local function get_diff(chat)
1514

1615
-- Initialize variables with selection if available
1716
local header = block.header
18-
local selection = copilot.get_selection(config)
17+
local selection = copilot.get_selection()
1918
local reference = selection and selection.content
2019
local start_line = selection and selection.start_line
2120
local end_line = selection and selection.end_line
@@ -64,35 +63,15 @@ local function get_diff(chat)
6463
}
6564
end
6665

67-
---@param winnr number
68-
---@param bufnr number
69-
---@param start_line number
70-
---@param end_line number
71-
---@param config CopilotChat.config.shared
72-
local function jump_to_diff(winnr, bufnr, start_line, end_line, config)
73-
pcall(vim.api.nvim_buf_set_mark, bufnr, '<', start_line, 0, {})
74-
pcall(vim.api.nvim_buf_set_mark, bufnr, '>', end_line, 0, {})
75-
pcall(vim.api.nvim_buf_set_mark, bufnr, '[', start_line, 0, {})
76-
pcall(vim.api.nvim_buf_set_mark, bufnr, ']', end_line, 0, {})
77-
pcall(vim.api.nvim_win_set_cursor, winnr, { start_line, 0 })
78-
copilot.update_selection(config)
79-
end
80-
8166
---@param diff CopilotChat.ui.Diff.Diff?
82-
---@param config CopilotChat.config.shared
83-
local function apply_diff(diff, config)
67+
local function apply_diff(diff)
8468
if not diff or not diff.bufnr then
8569
return
8670
end
8771

88-
local winnr = vim.fn.win_findbuf(diff.bufnr)[1]
89-
if not winnr then
90-
return
91-
end
92-
9372
local lines = vim.split(diff.change, '\n', { trimempty = false })
9473
vim.api.nvim_buf_set_lines(diff.bufnr, diff.start_line - 1, diff.end_line, false, lines)
95-
jump_to_diff(winnr, diff.bufnr, diff.start_line, diff.start_line + #lines - 1, config)
74+
copilot.set_selection(diff.bufnr, diff.start_line, diff.start_line + #lines - 1)
9675
end
9776

9877
---@class CopilotChat.config.mapping
@@ -208,7 +187,7 @@ return {
208187
normal = '<C-y>',
209188
insert = '<C-y>',
210189
callback = function(overlay, diff, chat, source)
211-
apply_diff(get_diff(chat), chat.config)
190+
apply_diff(get_diff(chat))
212191
end,
213192
},
214193

@@ -234,8 +213,7 @@ return {
234213

235214
source.bufnr = diff_bufnr
236215
vim.api.nvim_win_set_buf(source.winnr, diff_bufnr)
237-
238-
jump_to_diff(source.winnr, diff_bufnr, diff.start_line, diff.end_line, chat.config)
216+
copilot.set_selection(diff_bufnr, diff.start_line, diff.end_line)
239217
end,
240218
},
241219

@@ -278,7 +256,7 @@ return {
278256
quickfix_diffs = {
279257
normal = 'gqd',
280258
callback = function(overlay, diff, chat)
281-
local selection = copilot.get_selection(chat.config)
259+
local selection = copilot.get_selection()
282260
local items = {}
283261

284262
for _, section in ipairs(chat.sections) do
@@ -345,16 +323,16 @@ return {
345323
end
346324

347325
local lines = {}
348-
local prompt, config = copilot.resolve_prompts(section.content, chat.config)
326+
local config, prompt = copilot.resolve_prompt(section.content)
349327
local system_prompt = config.system_prompt
350328

351329
async.run(function()
352-
local _, selected_agent = pcall(copilot.resolve_agent, prompt, config)
353-
local _, selected_model = pcall(copilot.resolve_model, prompt, config)
330+
local selected_agent = copilot.resolve_agent(prompt, config)
331+
local selected_model = copilot.resolve_model(prompt, config)
354332

355333
utils.schedule_main()
356-
table.insert(lines, '**Logs**: `' .. chat.config.log_path .. '`')
357-
table.insert(lines, '**History**: `' .. chat.config.history_path .. '`')
334+
table.insert(lines, '**Logs**: `' .. copilot.config.log_path .. '`')
335+
table.insert(lines, '**History**: `' .. copilot.config.history_path .. '`')
358336
table.insert(lines, '**Temp Files**: `' .. vim.fn.fnamemodify(os.tmpname(), ':h') .. '`')
359337
table.insert(lines, '')
360338

@@ -393,7 +371,7 @@ return {
393371

394372
local lines = {}
395373

396-
local selection = copilot.get_selection(chat.config)
374+
local selection = copilot.get_selection()
397375
if selection then
398376
table.insert(lines, '**Selection**')
399377
table.insert(lines, '```' .. selection.filetype)
@@ -405,10 +383,7 @@ return {
405383
end
406384

407385
async.run(function()
408-
local embeddings = {}
409-
if section and not section.answer then
410-
embeddings = copilot.resolve_embeddings(section.content, chat.config)
411-
end
386+
local embeddings = copilot.resolve_context(section.content)
412387

413388
for _, embedding in ipairs(embeddings) do
414389
local embed_lines = vim.split(embedding.content, '\n')

lua/CopilotChat/config/prompts.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ return {
9696
},
9797

9898
Explain = {
99-
prompt = '> /COPILOT_EXPLAIN\n\nWrite an explanation for the selected code as paragraphs of text.',
99+
prompt = 'Write an explanation for the selected code as paragraphs of text.',
100+
sticky = '/COPILOT_EXPLAIN',
100101
},
101102

102103
Review = {
103-
prompt = '> /COPILOT_REVIEW\n\nReview the selected code.',
104+
prompt = 'Review the selected code.',
105+
sticky = '/COPILOT_REVIEW',
104106
callback = function(response, source)
105107
local diagnostics = {}
106108
for line in response:gmatch('[^\r\n]+') do
@@ -159,6 +161,7 @@ return {
159161
},
160162

161163
Commit = {
162-
prompt = '> #git:staged\n\nWrite commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block.',
164+
prompt = 'Write commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block.',
165+
sticky = '#git:staged',
163166
},
164167
}

0 commit comments

Comments
 (0)