1- --- @param commands eca.ChatCommand
2- --- @return lsp.CompletionItem[]
3- local function create_completion_items (commands )
4- --- @type lsp.CompletionItem[]
5- local items = {}
6-
7- if commands then
8- for _ , command in ipairs (commands ) do
9- --- @type lsp.CompletionItem
10- local item = {
11- label = command .name or command .command ,
12- kind = vim .lsp .protocol .CompletionItemKind .Function ,
13- detail = command .description or (" ECA command: " .. (command .name or command .command )),
14- documentation = command .help and {
15- kind = " markdown" ,
16- value = command .help ,
17- } or nil ,
18- insertText = command .name or command .command ,
19- }
20- table.insert (items , item )
21- end
22- end
23-
24- return items
25- end
26-
27- --- @param s string
28- --- @return string
29- local function get_query (s )
30- local match = s :match (" ^>?%s*/(.*)$" )
31- return match
32- end
33-
34- -- Query server for available commands
35- local function query_server_commands (query , callback )
36- local eca = require (" eca" )
37- if not eca .server or not eca .server :is_running () then
38- callback ({})
39- return
40- end
41-
42- eca .server :send_request (" chat/queryCommands" , { query = query }, function (err , result )
43- if err then
44- callback ({})
45- return
46- end
47-
48- local items = create_completion_items (result .commands )
49- callback (items )
50- end )
1+ --- @param command eca.ChatCommand
2+ --- @return lsp.CompletionItem
3+ local function as_completion_item (command )
4+ local cmp = require (" cmp" )
5+ --- @type lsp.CompletionItem
6+ return {
7+ label = command .name ,
8+ kind = cmp .lsp .CompletionItemKind .Function ,
9+ detail = command .description or (" ECA command: " .. command .name ),
10+ documentation = command .help and {
11+ kind = " markdown" ,
12+ value = command .help ,
13+ } or nil ,
14+ }
5115end
5216
5317local source = {}
@@ -56,41 +20,21 @@ source.new = function()
5620 return setmetatable ({ cache = {} }, { __index = source })
5721end
5822
59- source . get_trigger_characters = function ()
23+ function source : get_trigger_characters ()
6024 return { " /" }
6125end
6226
63- source . is_available = function ()
27+ function source : is_available ()
6428 return vim .bo .filetype == " eca-input"
6529end
6630
6731--- @diagnostic disable-next-line : unused-local
68- source . complete = function ( self , _ , callback )
32+ function source : complete ( params , callback )
6933 -- Only complete if we're typing a command (starts with /)
70- local line = vim .api .nvim_get_current_line ()
71- local query = get_query (line )
72-
73- -- Only provide command completions when we have / followed by word characters or at word boundary
34+ local commands = require (" eca.completion.commands" )
35+ local query = commands .get_query (params .context .cursor_line )
7436 if query then
75- local bufnr = vim .api .nvim_get_current_buf ()
76-
77- if self .cache [bufnr ] and self .cache [bufnr ][query ] then
78- callback ({ items = self .cache [bufnr ][query ], isIncomplete = false })
79- else
80- query_server_commands (query , function (items )
81- callback ({
82- items = items ,
83- isIncomplete = false ,
84- })
85- self .cache [bufnr ] = {}
86- self .cache [bufnr ][query ] = items
87- end )
88- end
89- else
90- callback ({
91- items = {},
92- isIncomplete = false ,
93- })
37+ commands .get_completion_candidates (query , as_completion_item , callback )
9438 end
9539end
9640return source
0 commit comments