@@ -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 = {}
193193end
194194
195195function M :new_chat ()
@@ -382,18 +382,25 @@ end
382382--- @param container NuiSplit
383383function 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