@@ -275,6 +275,34 @@ function M.prompts(skip_system)
275275 return prompts_to_use
276276end
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