11local cmp = require (" cmp" )
2+ --- @module ' cmp'
23--- @param context eca.ChatContext
3- --- @return lsp .CompletionItem
4+ --- @return cmp .CompletionItem
45local function as_completion_item (context )
56 --- @type lsp.CompletionItem
67 --- @diagnostic disable-next-line : missing-fields
@@ -32,27 +33,6 @@ local function as_completion_item(context)
3233 return item
3334end
3435
35- -- Query server for available contexts
36- -- @param query string
37- --
38- local function query_server_contexts (query , callback )
39- local eca = require (" eca" )
40- if not eca .server or not eca .server :is_running () then
41- callback ({})
42- return
43- end
44-
45- eca .server :send_request (" chat/queryContext" , { query = query }, function (err , result )
46- if err then
47- callback ({})
48- return
49- end
50-
51- local items = vim .iter (result .contexts ):map (as_completion_item ):totable ()
52- callback ({ items = items , isIncomplete = true })
53- end )
54- end
55-
5636local source = {}
5737
5838function source .new ()
@@ -71,75 +51,19 @@ function source:is_available()
7151 return vim .bo .filetype == " eca-input"
7252end
7353
74- --- @param cursor_line string
75- --- @param cursor_position lsp.Position | vim.Position
76- --- @return string
77- local function get_query (cursor_line , cursor_position )
78- local before_cursor = cursor_line :sub (1 , cursor_position .col )
79- --- @type string[]
80- local matches = {}
81- local it = before_cursor :gmatch (" @([%w%./_\\ %-~]*)" )
82- for match in it do
83- table.insert (matches , match )
84- end
85- return matches [# matches ]
86- end
87-
8854--- @param params cmp.SourceCompletionApiParams
8955--- @diagnostic disable-next-line : unused-local
9056function source :complete (params , callback )
91- local query = get_query (params .context .cursor_line , params .context .cursor )
57+ local context = require (" eca.completion.context" )
58+ local query = context .get_query (params .context .cursor_line , params .context .cursor )
9259 if query then
93- query_server_contexts (query , function (items )
94- callback (items )
95- end )
60+ context .get_completion_candidates (query , as_completion_item , callback )
9661 end
9762end
9863
99- --- Taken from https://github.com/hrsh7th/cmp-path/blob/9a16c8e5d0be845f1d1b64a0331b155a9fe6db4d/lua/cmp_path/init.lua
100- --- Show a small preview of file contexft items in the documentation window.
101- --- @param data eca.ChatContext
102- --- @return lsp.MarkupContent
103- source ._get_documentation = function (_ , data , count )
104- if data and data .path then
105- local filename = data .path
106- local binary = assert (io.open (data .path , " rb" ))
107- local first_kb = binary :read (1024 )
108- if first_kb and first_kb :find (" \0 " ) then
109- return { kind = vim .lsp .protocol .MarkupKind .PlainText , value = " binary file" }
110- end
111-
112- local content = io.lines (data .path )
113-
114- --- Try to support line ranges, I don't know if this works or not yet
115- local start = data .lines_range and data .lines_range .start or 1
116- local last = data .lines_range and data .lines_range [" end" ] or count
117- local skip_lines = start - 1
118- local take_lines = last - start
119- local contents = vim .iter (content ):skip (skip_lines ):take (take_lines ):totable ()
120-
121- local filetype = vim .filetype .match ({ filename = filename })
122- if not filetype then
123- return { kind = vim .lsp .protocol .MarkupKind .PlainText , value = table.concat (contents , " \n " ) }
124- end
125-
126- table.insert (contents , 1 , " ```" .. filetype )
127- table.insert (contents , " ```" )
128- return { kind = vim .lsp .protocol .MarkupKind .Markdown , value = table.concat (contents , " \n " ) }
129- end
130- return {}
131- end
132-
13364--- @param completion_item lsp.CompletionItem
13465function source :resolve (completion_item , callback )
135- if completion_item .data then
136- local context_item = completion_item .data .context_item
137- --- @cast context_item eca.ChatContext
138- if context_item .type == " file" then
139- completion_item .documentation = self :_get_documentation (context_item , 20 )
140- end
141- callback (completion_item )
142- end
66+ require (" eca.completion.context" ).resolve_completion_item (completion_item , callback )
14367end
14468
14569return source
0 commit comments