Skip to content

Commit 7538816

Browse files
committed
add context path expansion at the input with both @ and #
1 parent 0c33286 commit 7538816

File tree

6 files changed

+91
-50
lines changed

6 files changed

+91
-50
lines changed

lua/eca/completion/blink/context.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ end
2020

2121
-- (Optional) Non-alphanumeric characters that trigger the source
2222
function source:get_trigger_characters()
23-
return { "@" }
23+
return { "@", "#" }
2424
end
2525

2626
---@param context eca.ChatContext
@@ -31,13 +31,13 @@ local function as_completion_item(context)
3131
---@diagnostic disable-next-line: missing-fields
3232
local item = {}
3333
if context.type == "file" then
34-
item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":."))
34+
item.label = vim.fn.fnamemodify(context.path, ":.")
3535
item.kind = kinds.File
3636
item.data = {
3737
context_item = context,
3838
}
3939
elseif context.type == "directory" then
40-
item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":."))
40+
item.label = vim.fn.fnamemodify(context.path, ":.")
4141
item.kind = kinds.Folder
4242
elseif context.type == "web" then
4343
item.label = context.url

lua/eca/completion/cmp/context.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ local function as_completion_item(context)
77
---@diagnostic disable-next-line: missing-fields
88
local item = {}
99
if context.type == "file" then
10-
item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":."))
10+
item.label = vim.fn.fnamemodify(context.path, ":.")
1111
item.kind = cmp.lsp.CompletionItemKind.File
1212
item.data = {
1313
context_item = context,
1414
}
1515
elseif context.type == "directory" then
16-
item.label = string.format("@%s", vim.fn.fnamemodify(context.path, ":."))
16+
item.label = vim.fn.fnamemodify(context.path, ":.")
1717
item.kind = cmp.lsp.CompletionItemKind.Folder
1818
elseif context.type == "web" then
1919
item.label = context.url
@@ -40,7 +40,7 @@ function source.new()
4040
end
4141

4242
function source:get_trigger_characters()
43-
return { "@" }
43+
return { "@", "#" }
4444
end
4545

4646
function source:get_keyword_pattern()

lua/eca/completion/commands.lua

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@ end
1010
---@param as_completion_item fun(eca.ChatCommand): lsp.CompletionItem
1111
---@param callback fun(resp: {items: lsp.CompletionItem[], isIncomplete?: boolean, is_incomplete_forward?: boolean, is_incomplete_backward?: boolean})
1212
function M.get_completion_candidates(query, as_completion_item, callback)
13-
local server = require("eca").server
14-
server:send_request("chat/queryCommands", { query = query }, function(err, result)
15-
if err then
16-
---@diagnostic disable-next-line: missing-fields
17-
callback({ items = {} })
18-
end
13+
local eca = require("eca")
14+
local chat = eca.get()
1915

20-
if result and result.commands then
21-
local items = vim.iter(result.commands):map(as_completion_item):totable()
22-
callback({ items = items })
23-
else
24-
callback({ items = {} })
25-
end
26-
end)
16+
if not chat or not chat.mediator then
17+
Logger.notify("No active ECA sidebar", vim.log.levels.WARN)
18+
return
19+
end
20+
21+
chat.mediator:send("chat/queryCommands", {
22+
chatId = chat.mediator:id(),
23+
query = query,
24+
},
25+
function(err, result)
26+
if err then
27+
---@diagnostic disable-next-line: missing-fields
28+
callback({ items = {} })
29+
end
30+
31+
if result and result.commands then
32+
local items = vim.iter(result.commands):map(as_completion_item):totable()
33+
callback({ items = items })
34+
else
35+
callback({ items = {} })
36+
end
37+
end)
2738
end
2839

2940
return M

lua/eca/completion/context.lua

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function M.get_query(cursor_line, cursor_position)
77
local before_cursor = cursor_line:sub(1, cursor_position.col)
88
---@type string[]
99
local matches = {}
10-
local it = before_cursor:gmatch("@([%w%./_\\%-~]*)")
10+
local it = before_cursor:gmatch("[@#]([%w%./_\\%-~]*)")
1111
for match in it do
1212
table.insert(matches, match)
1313
end
@@ -18,20 +18,32 @@ end
1818
---@param as_completion_item fun(eca.ChatContext): lsp.CompletionItem
1919
---@param callback fun(resp: {items: lsp.CompletionItem[], isIncomplete?: boolean, is_incomplete_forward?: boolean, is_incomplete_backward?: boolean})
2020
function M.get_completion_candidates(query, as_completion_item, callback)
21-
local server = require("eca").server
22-
server:send_request("chat/queryContext", { query = query }, function(err, result)
23-
if err then
24-
callback({ items = {} })
25-
return
26-
end
21+
local eca = require("eca")
22+
local chat = eca.get()
2723

28-
if result and result.contexts then
29-
local items = vim.iter(result.contexts):map(as_completion_item):totable()
30-
callback({ items = items })
31-
else
32-
callback({ items = {} })
33-
end
34-
end)
24+
if not chat or not chat.mediator then
25+
Logger.notify("No active ECA sidebar", vim.log.levels.WARN)
26+
return
27+
end
28+
29+
chat.mediator:send("chat/queryContext", {
30+
chatId = chat.mediator:id(),
31+
query = query,
32+
contexts = chat.mediator:contexts() or {},
33+
},
34+
function(err, result)
35+
if err then
36+
callback({ items = {} })
37+
return
38+
end
39+
40+
if result and result.contexts then
41+
local items = vim.iter(result.contexts):map(as_completion_item):totable()
42+
callback({ items = items })
43+
else
44+
callback({ items = {} })
45+
end
46+
end)
3547
end
3648

3749
--- Taken from https://github.com/hrsh7th/cmp-path/blob/9a16c8e5d0be845f1d1b64a0331b155a9fe6db4d/lua/cmp_path/init.lua
@@ -89,7 +101,10 @@ function M.execute(completion_item, callback)
89101
if completion_item.data then
90102
vim.api.nvim_exec_autocmds("User", {
91103
pattern = { "CompletionItemSelected" },
92-
data = completion_item.data.context_item,
104+
data = {
105+
context_item = completion_item.data.context_item,
106+
label = completion_item.label,
107+
}
93108
})
94109
callback(completion_item)
95110
end

lua/eca/mediator.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,30 @@ function mediator:send(method, params, callback)
111111
require("eca.logger").notify("Server is not rnning, please start the server", vim.log.levels.WARN)
112112
end
113113

114-
local contexts = {}
114+
if params.contexts then
115+
local contexts = {}
115116

116-
for _, context in pairs(params.contexts) do
117-
local adapted = context_adapter(context)
118-
if adapted then
119-
table.insert(contexts, adapted)
117+
for _, context in pairs(params.contexts) do
118+
local adapted = context_adapter(context)
119+
if adapted then
120+
table.insert(contexts, adapted)
121+
end
120122
end
123+
124+
params.contexts = contexts
121125
end
122126

123-
params.contexts = contexts
127+
if params.message and type(params.message) == "string" then
128+
local message = params.message:gsub("([@#])([%w%._%-%/]+)", function(prefix, path)
129+
-- expand ~
130+
if path:sub(1,1) == "~" then
131+
path = vim.fn.expand(path)
132+
end
133+
return prefix .. vim.fn.fnamemodify(path, ":p")
134+
end)
135+
136+
params.message = message
137+
end
124138

125139
self.server:send_request(method, params, callback)
126140
end

lua/eca/sidebar.lua

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,16 @@ function M:_setup_input_events(container)
384384
vim.api.nvim_create_autocmd("User", {
385385
pattern = { "CompletionItemSelected" },
386386
callback = function(event)
387-
if not event.data or not event.data.path or not event.data.type then
387+
if not event.data or not event.data.context_item or not event.data.label then
388388
return
389389
end
390390

391391
if self._contexts then
392392
self._contexts.to_add = {
393-
name = vim.fn.fnamemodify(event.data.path, ":."),
394-
type = event.data.type,
393+
name = event.data.label,
394+
type = event.data.context_item.type,
395395
data = {
396-
path = event.data.path
396+
path = event.data.context_item.path
397397
}
398398
}
399399
end
@@ -404,10 +404,6 @@ function M:_setup_input_events(container)
404404
vim.api.nvim_buf_attach(container.bufnr, false, {
405405
on_lines = function(_, buf, _changedtick, first, _last, _new_last, _bytecount)
406406
vim.schedule(function()
407-
if first ~= 0 and first ~= 1 then
408-
return
409-
end
410-
411407
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
412408

413409
-- handle empty buffer
@@ -431,7 +427,10 @@ function M:_setup_input_events(container)
431427
end
432428

433429
local prefix_mark = vim.api.nvim_buf_get_extmark_by_id(buf, prefix_ns, prefix_id, {})
434-
local prefix_row = unpack(prefix_mark)
430+
local prefix_row = 1
431+
if prefix_mark and type(prefix_mark) == "table" and prefix_mark[1] ~= nil then
432+
prefix_row = tonumber(prefix_mark[1]) or 1
433+
end
435434
local contexts_row = 0
436435

437436
local prefix_line = lines[prefix_row + 1] or nil
@@ -485,6 +484,7 @@ function M:_setup_input_events(container)
485484
for i = 1, #placeholders do
486485
if context_to_add.name and context_to_add.name == placeholders[i] then
487486
self.mediator:add_context(context_to_add)
487+
self._contexts.to_add = {}
488488
end
489489
end
490490

@@ -494,6 +494,7 @@ function M:_setup_input_events(container)
494494
self:_update_input_display()
495495
return
496496
end
497+
497498
end)
498499
end
499500
})
@@ -750,7 +751,7 @@ function M:_update_input_display(opts)
750751

751752
for i, context_name in ipairs(contexts_name) do
752753
self.extmarks.contexts._id[i] = vim.api.nvim_buf_set_extmark(
753-
input.bufnr,
754+
input.bufnr,
754755
self.extmarks.contexts._ns,
755756
0,
756757
i,

0 commit comments

Comments
 (0)