Skip to content

Commit 0dea26f

Browse files
committed
refactor: use sticky instead of stored config per chat
Instead of storing config passed to .ask to chat window, store stuff like model, agent and context into sticky so its clearly visible Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent bdddfeb commit 0dea26f

File tree

5 files changed

+197
-109
lines changed

5 files changed

+197
-109
lines changed

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/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_embeddings(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)