Skip to content

Commit 0c33286

Browse files
committed
add to contexts when completion on contexts area
1 parent 027a1bf commit 0c33286

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

lua/eca/completion/blink/context.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ function source:resolve(item, callback)
7373
require("eca.completion.context").resolve_completion_item(item, callback)
7474
end
7575

76+
---Executed after the item was selected
77+
---@param item lsp.CompletionItem
78+
---@param callback fun(any)
79+
function source:execute(item, callback)
80+
require("eca.completion.context").execute(item, callback)
81+
end
82+
7683
return source

lua/eca/completion/cmp/context.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ function source:resolve(completion_item, callback)
6666
require("eca.completion.context").resolve_completion_item(completion_item, callback)
6767
end
6868

69+
---Executed after the item was selected.
70+
---@param completion_item lsp.CompletionItem
71+
---@param callback fun(completion_item: lsp.CompletionItem|nil)
72+
function source:execute(completion_item, callback)
73+
require("eca.completion.context").execute(completion_item, callback)
74+
end
75+
6976
return source

lua/eca/completion/context.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ function M.resolve_completion_item(completion_item, callback)
7878
if context_item.type == "file" then
7979
completion_item.documentation = documentation(context_item, 20)
8080
end
81+
callback(completion_item)
82+
end
83+
end
84+
85+
---Executed after the item was selected
86+
---@param completion_item lsp.CompletionItem
87+
---@param callback fun(any)
88+
function M.execute(completion_item, callback)
89+
if completion_item.data then
8190
vim.api.nvim_exec_autocmds("User", {
82-
pattern = { "EcaChatContextUpdated" },
91+
pattern = { "CompletionItemSelected" },
8392
data = completion_item.data.context_item,
8493
})
8594
callback(completion_item)
8695
end
8796
end
97+
8898
return M

lua/eca/sidebar.lua

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function M.new(id, mediator)
6262
}
6363
instance._welcome_message_applied = false
6464
instance._contexts_placeholder_line = ""
65-
instance._contexts_to_resolve = {}
65+
instance._contexts = {}
6666

6767
require("eca.observer").subscribe("sidebar-" .. id, function(message)
6868
instance:handle_chat_content(message)
@@ -189,7 +189,7 @@ function M:reset()
189189
self._current_status = ""
190190
self._welcome_message_applied = false
191191
self._contexts_placeholder_line = ""
192-
self._contexts_to_resolve = {}
192+
self._contexts = {}
193193
end
194194

195195
function M:new_chat()
@@ -382,18 +382,25 @@ end
382382
---@param container NuiSplit
383383
function M:_setup_input_events(container)
384384
vim.api.nvim_create_autocmd("User", {
385-
pattern = { "EcaChatContextUpdated" },
385+
pattern = { "CompletionItemSelected" },
386386
callback = function(event)
387-
local context_to_resolve = {
388-
path = event.data.path,
389-
name = vim.fn.fnamemodify(event.data.path, ":.")
390-
}
387+
if not event.data or not event.data.path or not event.data.type then
388+
return
389+
end
391390

392-
table.insert(self._contexts_to_resolve, context_to_resolve)
391+
if self._contexts then
392+
self._contexts.to_add = {
393+
name = vim.fn.fnamemodify(event.data.path, ":."),
394+
type = event.data.type,
395+
data = {
396+
path = event.data.path
397+
}
398+
}
399+
end
393400
end,
394401
})
395402

396-
-- prevent contexts line or input prefix from being deleted
403+
-- contexts area and input handler
397404
vim.api.nvim_buf_attach(container.bufnr, false, {
398405
on_lines = function(_, buf, _changedtick, first, _last, _new_last, _bytecount)
399406
vim.schedule(function()
@@ -434,13 +441,11 @@ function M:_setup_input_events(container)
434441
if prefix_row == contexts_row then
435442
-- prefix line missing, restore
436443
if contexts_line == contexts_placeholder_line then
437-
-- Logger.test("prefix line missing, restore")
438444
self:_update_input_display()
439445
return
440446
end
441447

442448
-- we can consider that contexts were deleted
443-
-- Logger.test("contexts cleared")
444449
self.mediator:clear_contexts()
445450
return
446451
end
@@ -451,6 +456,14 @@ function M:_setup_input_events(container)
451456
return
452457
end
453458

459+
-- something wrong, restore
460+
if prefix_row - contexts_row ~= 1 then
461+
self:_update_input_display()
462+
return
463+
end
464+
465+
local context_to_add = self._contexts.to_add or {}
466+
454467
if contexts_line ~= contexts_placeholder_line then
455468
-- a context was removed
456469
if #contexts_line < #self._contexts_placeholder_line then
@@ -465,18 +478,16 @@ function M:_setup_input_events(container)
465478
end
466479
end
467480

468-
-- contexts line modified, restore
469-
if #contexts_line > #self._contexts_placeholder_line then
470-
local placeholders = vim.split(contexts_line, "@", { plain = true, trimempty = false })
481+
-- contexts line modified
482+
if #contexts_line > #self._contexts_placeholder_line then
483+
local placeholders = vim.split(contexts_line, "@", { plain = true, trimempty = true })
471484

472-
if #placeholders[#placeholders] < 1 then
473-
self:_update_input_display()
474-
return
485+
for i = 1, #placeholders do
486+
if context_to_add.name and context_to_add.name == placeholders[i] then
487+
self.mediator:add_context(context_to_add)
488+
end
475489
end
476490

477-
vim.notify("Contexts line: " .. contexts_line, vim.log.levels.DEBUG)
478-
vim.notify("Contexts to resolve: " .. vim.inspect(self._contexts_to_resolve), vim.log.levels.DEBUG)
479-
480491
return
481492
end
482493

0 commit comments

Comments
 (0)