@@ -44,22 +44,6 @@ local state = {
4444 help = nil ,
4545}
4646
47- local function blend_color_with_neovim_bg (color_name , blend )
48- local color_int = vim .api .nvim_get_hl (0 , { name = color_name }).fg
49- local bg_int = vim .api .nvim_get_hl (0 , { name = ' Normal' }).bg
50-
51- if not color_int or not bg_int then
52- return
53- end
54-
55- local color = { (color_int / 65536 ) % 256 , (color_int / 256 ) % 256 , color_int % 256 }
56- local bg = { (bg_int / 65536 ) % 256 , (bg_int / 256 ) % 256 , bg_int % 256 }
57- local r = math.floor ((color [1 ] * blend + bg [1 ] * (100 - blend )) / 100 )
58- local g = math.floor ((color [2 ] * blend + bg [2 ] * (100 - blend )) / 100 )
59- local b = math.floor ((color [3 ] * blend + bg [3 ] * (100 - blend )) / 100 )
60- return string.format (' #%02x%02x%02x' , r , g , b )
61- end
62-
6347local function find_lines_between_separator (
6448 lines ,
6549 current_line ,
@@ -260,6 +244,47 @@ local function key_to_info(name, key, surround)
260244 return out
261245end
262246
247+ local function trigger_complete ()
248+ local info = M .complete_info ()
249+ local bufnr = vim .api .nvim_get_current_buf ()
250+ local line = vim .api .nvim_get_current_line ()
251+ local cursor = vim .api .nvim_win_get_cursor (0 )
252+ local row = cursor [1 ]
253+ local col = cursor [2 ]
254+ if col == 0 or # line == 0 then
255+ return
256+ end
257+
258+ local prefix , cmp_start = unpack (vim .fn .matchstrpos (line :sub (1 , col ), info .pattern ))
259+ if not prefix then
260+ return
261+ end
262+
263+ if vim .startswith (prefix , ' #' ) and vim .endswith (prefix , ' :' ) then
264+ local found_context = M .config .contexts [prefix :sub (2 , - 2 )]
265+ if found_context and found_context .input then
266+ found_context .input (function (value )
267+ if not value then
268+ return
269+ end
270+
271+ vim .api .nvim_buf_set_text (bufnr , row - 1 , col , row - 1 , col , { tostring (value ) })
272+ end )
273+ end
274+
275+ return
276+ end
277+
278+ M .complete_items (function (items )
279+ vim .fn .complete (
280+ cmp_start + 1 ,
281+ vim .tbl_filter (function (item )
282+ return vim .startswith (item .word :lower (), prefix :lower ())
283+ end , items )
284+ )
285+ end )
286+ end
287+
263288--- Get the completion info for the chat window, for use with custom completion providers
264289--- @return table
265290function M .complete_info ()
@@ -275,8 +300,8 @@ function M.complete_items(callback)
275300 async .run (function ()
276301 local models = state .copilot :list_models ()
277302 local agents = state .copilot :list_agents ()
278- local items = {}
279303 local prompts_to_use = M .prompts ()
304+ local items = {}
280305
281306 for name , prompt in pairs (prompts_to_use ) do
282307 items [# items + 1 ] = {
@@ -323,6 +348,10 @@ function M.complete_items(callback)
323348 }
324349 end
325350
351+ table.sort (items , function (a , b )
352+ return a .kind < b .kind
353+ end )
354+
326355 vim .schedule (function ()
327356 callback (items )
328357 end )
@@ -784,9 +813,17 @@ function M.setup(config)
784813 end
785814
786815 local hl_ns = vim .api .nvim_create_namespace (' copilot-chat-highlights' )
787- vim .api .nvim_set_hl (hl_ns , ' @diff.plus' , { bg = blend_color_with_neovim_bg (' DiffAdd' , 20 ) })
788- vim .api .nvim_set_hl (hl_ns , ' @diff.minus' , { bg = blend_color_with_neovim_bg (' DiffDelete' , 20 ) })
789- vim .api .nvim_set_hl (hl_ns , ' @diff.delta' , { bg = blend_color_with_neovim_bg (' DiffChange' , 20 ) })
816+ vim .api .nvim_set_hl (hl_ns , ' @diff.plus' , { bg = utils .blend_color_with_neovim_bg (' DiffAdd' , 20 ) })
817+ vim .api .nvim_set_hl (
818+ hl_ns ,
819+ ' @diff.minus' ,
820+ { bg = utils .blend_color_with_neovim_bg (' DiffDelete' , 20 ) }
821+ )
822+ vim .api .nvim_set_hl (
823+ hl_ns ,
824+ ' @diff.delta' ,
825+ { bg = utils .blend_color_with_neovim_bg (' DiffChange' , 20 ) }
826+ )
790827 vim .api .nvim_set_hl (0 , ' CopilotChatSpinner' , { link = ' CursorColumn' , default = true })
791828 vim .api .nvim_set_hl (0 , ' CopilotChatHelp' , { link = ' DiagnosticInfo' , default = true })
792829 vim .api .nvim_set_hl (0 , ' CopilotChatSelection' , { link = ' Visual' , default = true })
@@ -906,46 +943,23 @@ function M.setup(config)
906943
907944 map_key (M .config .mappings .reset , bufnr , M .reset )
908945 map_key (M .config .mappings .close , bufnr , M .close )
946+ map_key (M .config .mappings .complete , bufnr , trigger_complete )
909947
910- map_key (M .config .mappings .complete , bufnr , function ()
911- local info = M .complete_info ()
912- local line = vim .api .nvim_get_current_line ()
913- local cursor = vim .api .nvim_win_get_cursor (0 )
914- local row = cursor [1 ]
915- local col = cursor [2 ]
916- if col == 0 or # line == 0 then
917- return
918- end
919-
920- local prefix , cmp_start = unpack (vim .fn .matchstrpos (line :sub (1 , col ), info .pattern ))
921- if not prefix then
922- return
923- end
924-
925- if vim .startswith (prefix , ' #' ) and vim .endswith (prefix , ' :' ) then
926- local found_context = M .config .contexts [prefix :sub (2 , - 2 )]
927- if found_context and found_context .input then
928- found_context .input (function (value )
929- if not value then
930- return
931- end
932-
933- vim .api .nvim_buf_set_text (bufnr , row - 1 , col , row - 1 , col , { tostring (value ) })
934- end )
935- end
936-
937- return
938- end
948+ if M .config .chat_autocomplete then
949+ vim .api .nvim_create_autocmd (' TextChangedI' , {
950+ buffer = bufnr ,
951+ callback = function ()
952+ local line = vim .api .nvim_get_current_line ()
953+ local cursor = vim .api .nvim_win_get_cursor (0 )
954+ local col = cursor [2 ]
955+ local char = line :sub (col , col )
939956
940- M .complete_items (function (items )
941- vim .fn .complete (
942- cmp_start + 1 ,
943- vim .tbl_filter (function (item )
944- return vim .startswith (item .word :lower (), prefix :lower ())
945- end , items )
946- )
947- end )
948- end )
957+ if vim .tbl_contains (M .complete_info ().triggers , char ) then
958+ utils .debounce (trigger_complete , 100 )
959+ end
960+ end ,
961+ })
962+ end
949963
950964 map_key (M .config .mappings .submit_prompt , bufnr , function ()
951965 local chat_lines = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
0 commit comments