Skip to content

Commit 4870fc8

Browse files
committed
feat: highlight the selection in the source buffer when in the chat window
1 parent feca60c commit 4870fc8

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

lua/CopilotChat/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ local select = require('CopilotChat.select')
6969
---@field auto_follow_cursor boolean?
7070
---@field auto_insert_mode boolean?
7171
---@field clear_chat_on_new_prompt boolean?
72+
---@field highlight_selection boolean?
7273
---@field context string?
7374
---@field history_path string?
7475
---@field callback fun(response: string, source: CopilotChat.config.source)?
@@ -95,6 +96,7 @@ return {
9596
auto_follow_cursor = true, -- Auto-follow cursor in chat
9697
auto_insert_mode = false, -- Automatically enter insert mode when opening window and if auto follow cursor is enabled on new prompt
9798
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
99+
highlight_selection = true, -- Highlight selection
98100

99101
context = nil, -- Default context to use, 'buffers', 'buffer' or none (can be specified manually in prompt via @).
100102
history_path = vim.fn.stdpath('data') .. '/copilotchat_history', -- Default path to stored history

lua/CopilotChat/init.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,34 @@ function M.prompts(skip_system)
275275
return prompts_to_use
276276
end
277277

278+
local selection_ns = vim.api.nvim_create_namespace('copilot-chat-selection')
279+
280+
--- Highlights the selection in the source buffer.
281+
---@param opts? {clear?: boolean}
282+
function M.highlight_selection(opts)
283+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
284+
vim.api.nvim_buf_clear_namespace(buf, selection_ns, 0, -1)
285+
end
286+
if opts and opts.clear then
287+
return
288+
end
289+
local selection = get_selection()
290+
if selection then
291+
vim.api.nvim_buf_set_extmark(
292+
state.source.bufnr,
293+
selection_ns,
294+
selection.start_row - 1,
295+
selection.start_col - 1,
296+
{
297+
hl_group = 'CopilotChatSelection',
298+
end_row = selection.end_row - 1,
299+
end_col = selection.end_col,
300+
strict = false,
301+
}
302+
)
303+
end
304+
end
305+
278306
--- Open the chat window.
279307
---@param config CopilotChat.config|CopilotChat.config.prompt|nil
280308
---@param source CopilotChat.config.source?
@@ -302,6 +330,16 @@ function M.open(config, source, no_insert)
302330
state.chat:focus()
303331
state.chat:follow()
304332

333+
if config.highlight_selection then
334+
M.highlight_selection()
335+
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, {
336+
buffer = state.chat.bufnr,
337+
callback = function(ev)
338+
M.highlight_selection({ clear = ev.event == 'BufLeave' })
339+
end,
340+
})
341+
end
342+
305343
if
306344
not no_insert
307345
and not state.copilot:running()
@@ -583,6 +621,7 @@ function M.setup(config)
583621
vim.api.nvim_set_hl(hl_ns, '@diff.plus', { bg = blend_color_with_neovim_bg('DiffAdd', 20) })
584622
vim.api.nvim_set_hl(hl_ns, '@diff.minus', { bg = blend_color_with_neovim_bg('DiffDelete', 20) })
585623
vim.api.nvim_set_hl(hl_ns, '@diff.delta', { bg = blend_color_with_neovim_bg('DiffChange', 20) })
624+
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
586625

587626
local overlay_help = ''
588627
if M.config.mappings.close then

0 commit comments

Comments
 (0)