11local util = require (" copilot.util" )
22local format = require (" copilot_cmp.format" )
3+ local handler = require (" copilot.handlers" )
4+ local print_buf = require (" copilot.print_panel" )
35
46local panel = {
57 method = " getPanelCompletions" ,
68 usecmp = false ,
7- cache_line = true ,
89 buf = " " ,
910 uri = " " ,
1011}
@@ -18,36 +19,34 @@ panel.send_request = function (callback)
1819 vim .lsp .buf_request (0 , panel .method , completion_params , callback )
1920end
2021
21- panel .complete = vim .schedule_wrap (function (_ , params , callback )
22- local context = params .context
22+ local verify_existing = function (context )
2323 existing_matches [context .bufnr ] = existing_matches [context .bufnr ] or {}
2424 existing_matches [context .bufnr ][context .cursor .row ] = existing_matches [context .bufnr ][context .cursor .row ] or {}
25+ end
26+
27+ panel .complete = vim .schedule_wrap (function (_ , params , callback )
28+ local context = params .context
29+ verify_existing (context )
2530
2631 local add_completion = function (result )
27- if result then
28- result .text = result .displayText
29- local formatted = format .format_item (params , result )
30- existing_matches [context .bufnr ][context .cursor .row ][formatted .label ] = formatted
31- vim .schedule (function () callback ({
32- isIncomplete = true ,
33- items = vim .tbl_values (existing_matches [context .bufnr ][context .cursor .row ])
34- }) end )
35- end
32+ result .text = result .displayText
33+ local formatted = format .format_item (params , result )
34+ existing_matches [context .bufnr ][context .cursor .row ][formatted .label ] = formatted
35+ callback ({
36+ isIncomplete = true ,
37+ items = vim .tbl_values (existing_matches [context .bufnr ][context .cursor .row ])
38+ })
3639 end
3740
3841 local completed = function ()
39- vim . schedule ( function () callback ({
42+ callback ({
4043 isIncomplete = false ,
4144 items = vim .tbl_values (existing_matches [context .bufnr ][context .cursor .row ])
42- }) end )
43- if not panel .cache_line then
44- existing_matches [context .bufnr ][context .cursor .row ] = {}
45- end
45+ })
4646 end
4747
48- local handler = require (" copilot.handlers" ).add_handler_callback
49- handler (" PanelSolution" , " cmp" , add_completion )
50- handler (" PanelSolutionsDone" , " cmp" , completed )
48+ handler .add_handler_callback (" PanelSolution" , " cmp" , add_completion )
49+ handler .add_handler_callback (" PanelSolutionsDone" , " cmp" , completed )
5150
5251 panel .send_request ()
5352
@@ -59,19 +58,38 @@ function panel.create (opts)
5958 panel .buf = type (panel .uri ) == " number" or vim .api .nvim_create_buf (false , true )
6059 vim .api .nvim_buf_set_name (panel .buf , " copilot:///" .. tostring (panel .buf ))
6160 panel .uri = vim .uri_from_bufnr (panel .buf )
61+
6262 vim .api .nvim_create_user_command (" CopilotPanel" , function ()
63- local panel_suggestions = {}
64- local handlers = require (" copilot.handlers" )
65- handlers .add_handler_callback (" PanelSolution" , " print" , function (result )
66- table.insert (panel_suggestions , format .clean_insertion (result .displayText ))
63+ print_buf .create (panel .buf )
64+ local items = {}
65+ handler .add_handler_callback (" PanelSolution" , " pb" , function (result )
66+ local formatted = format .deindent (result .displayText )
67+ items [formatted ] = 1
6768 end )
68- handlers .add_handler_callback (" PanelSolutionsDone" , " print" , function ()
69- local print_buf = require (" copilot.print_buf" ).init ()
70- print_buf .print (panel_suggestions )
71- handlers .remove_handler_callback (" PanelSolution" , " print" )
72- handlers .remove_handler_callback (" PanelSolutionsDone" , " print" )
69+ handler .add_handler_callback (" PanelSolutionsDone" , " pb" , function ()
70+ local item_list = vim .tbl_add_reverse_lookup (vim .tbl_keys (items ))
71+ local result_text = vim .tbl_flatten (vim .tbl_map (function (v )
72+ local s = vim .fn .split (v , ' \n ' )
73+ local text = vim .tbl_map (function (t )
74+ local number_string = " [" .. item_list [v ] .. " ]"
75+ local str = (s [1 ] == t and number_string .. string.rep (' ' , vim .o .shiftwidth )) or string.rep (' ' , vim .o .shiftwidth + string.len (number_string ))
76+ return str .. t
77+ end , s )
78+ table.insert (text , ' ' )
79+ return text
80+ end , item_list ))
81+ print_buf .set_text (result_text )
82+ items = {}
7383 end )
74- panel .send_request ()
84+ vim .api .nvim_create_autocmd (" WinClosed" , {
85+ pattern = { tostring (print_buf .win ) },
86+ callback = function ()
87+ print (" CopilotPanel closed" )
88+ handler .remove_handler_callback (" PanelSolution" , " pb" )
89+ handler .remove_handler_callback (" PanelSolutionsDone" , " pb" )
90+ end ,
91+ once = true ,
92+ })
7593 end , {})
7694 return panel
7795end
0 commit comments