Skip to content

Commit 1ff0bb3

Browse files
authored
refactor!: move source/sticky logic to chat window (#1469)
This refactors the management of source and sticky prompt logic by moving it from the main module to the chat window implementation. The `CopilotChat.source` and sticky state are now encapsulated within the `CopilotChat.ui.chat.Chat` class, providing a cleaner separation of concerns and reducing global state. All related methods and type annotations have been updated accordingly. The sticky prompt insertion and retrieval logic is now handled via `get_sticky` and `set_sticky` methods on the chat window, and the source window/buffer is managed through `get_source` and `set_source` methods. This change also removes the sticky toggle mapping and updates documentation and function signatures for consistency. BREAKING CHANGE: get_source, and set_source methods moved to require('CopilotChat').chat
1 parent 4d92564 commit 1ff0bb3

File tree

9 files changed

+111
-193
lines changed

9 files changed

+111
-193
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,6 @@ chat.toggle(config) -- Toggle chat window visibility with optional con
408408
chat.reset() -- Reset the chat
409409
chat.stop() -- Stop current output
410410

411-
-- Source Management
412-
chat.get_source() -- Get the current source buffer and window
413-
chat.set_source(winnr) -- Set the source window
414-
415411
-- Prompt & Model Management
416412
chat.select_prompt(config) -- Open prompt selector with optional config
417413
chat.select_model() -- Open model selector
@@ -441,14 +437,17 @@ window:get_message(role, cursor) -- Get chat message by role, eith
441437
window:add_message({ role, content }, replace) -- Add or replace a message in chat
442438
window:remove_message(role, cursor) -- Remove chat message by role, either last or closest to cursor
443439
window:get_block(role, cursor) -- Get code block by role, either last or closest to cursor
444-
window:add_sticky(sticky) -- Add sticky prompt to chat message
445440

446441
-- Content Management
447442
window:append(text) -- Append text to chat window
448443
window:clear() -- Clear chat window content
449444
window:start() -- Start writing to chat window
450445
window:finish() -- Finish writing to chat window
451446

447+
-- Source Management
448+
window.get_source() -- Get the current source buffer and window
449+
window.set_source(winnr) -- Set the source window
450+
452451
-- Navigation
453452
window:follow() -- Move cursor to end of chat content
454453
window:focus() -- Focus the chat window

lua/CopilotChat/completion.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ end
131131
--- Trigger the completion for the chat window.
132132
---@param without_input boolean?
133133
function M.complete(without_input)
134-
local source = require('CopilotChat').get_source()
134+
local source = require('CopilotChat').chat:get_source()
135135
local info = M.info()
136136
local bufnr = vim.api.nvim_get_current_buf()
137137
local line = vim.api.nvim_get_current_line()

lua/CopilotChat/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
---@field language string?
2424
---@field temperature number?
2525
---@field headless boolean?
26-
---@field callback nil|fun(response: CopilotChat.client.Message, source: CopilotChat.source)
26+
---@field callback nil|fun(response: CopilotChat.client.Message, source: CopilotChat.ui.chat.Source)
2727
---@field remember_as_sticky boolean?
2828
---@field window CopilotChat.config.Window?
2929
---@field show_help boolean?

lua/CopilotChat/config/functions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ end
4545
---@field schema table?
4646
---@field group string?
4747
---@field uri string?
48-
---@field resolve fun(input: table, source: CopilotChat.source):CopilotChat.client.Resource[]
48+
---@field resolve fun(input: table, source: CopilotChat.ui.chat.Source):CopilotChat.client.Resource[]
4949

5050
---@type table<string, CopilotChat.config.functions.Function>
5151
return {

lua/CopilotChat/config/mappings.lua

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local files = require('CopilotChat.utils.files')
99

1010
--- Prepare a buffer for applying a diff
1111
---@param filename string?
12-
---@param source CopilotChat.source
12+
---@param source CopilotChat.ui.chat.Source
1313
---@return integer
1414
local function prepare_diff_buffer(filename, source)
1515
if not filename then
@@ -47,7 +47,7 @@ end
4747
---@class CopilotChat.config.mapping
4848
---@field normal string?
4949
---@field insert string?
50-
---@field callback fun(source: CopilotChat.source)
50+
---@field callback fun(source: CopilotChat.ui.chat.Source)
5151

5252
---@class CopilotChat.config.mapping.yank_diff : CopilotChat.config.mapping
5353
---@field register string?
@@ -57,7 +57,6 @@ end
5757
---@field close CopilotChat.config.mapping|false|nil
5858
---@field reset CopilotChat.config.mapping|false|nil
5959
---@field submit_prompt CopilotChat.config.mapping|false|nil
60-
---@field toggle_sticky CopilotChat.config.mapping|false|nil
6160
---@field accept_diff CopilotChat.config.mapping|false|nil
6261
---@field jump_to_diff CopilotChat.config.mapping|false|nil
6362
---@field quickfix_diffs CopilotChat.config.mapping|false|nil
@@ -103,37 +102,6 @@ return {
103102
end,
104103
},
105104

106-
toggle_sticky = {
107-
normal = 'grr',
108-
callback = function()
109-
local message = copilot.chat:get_message(constants.ROLE.USER)
110-
local section = message and message.section
111-
if not section then
112-
return
113-
end
114-
115-
local cursor = vim.api.nvim_win_get_cursor(copilot.chat.winnr)
116-
if cursor[1] < section.start_line or cursor[1] > section.end_line then
117-
return
118-
end
119-
120-
local current_line = vim.trim(vim.api.nvim_get_current_line())
121-
if current_line == '' then
122-
return
123-
end
124-
125-
local cur_line = cursor[1]
126-
vim.api.nvim_buf_set_lines(copilot.chat.bufnr, cur_line - 1, cur_line, false, {})
127-
128-
if vim.startswith(current_line, '> ') then
129-
return
130-
end
131-
132-
copilot.chat:add_sticky(current_line)
133-
vim.api.nvim_win_set_cursor(copilot.chat.winnr, cursor)
134-
end,
135-
},
136-
137105
accept_diff = {
138106
normal = '<C-y>',
139107
insert = '<C-y>',

lua/CopilotChat/functions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ end
199199

200200
--- Get input from the user based on the schema
201201
---@param schema table?
202-
---@param source CopilotChat.source
202+
---@param source CopilotChat.ui.chat.Source
203203
---@return string?
204204
function M.enter_input(schema, source)
205205
if not schema or not schema.properties then

0 commit comments

Comments
 (0)