Skip to content

Commit 5d6629e

Browse files
committed
chore(completion): general cleanup
1 parent 774c5ce commit 5d6629e

File tree

5 files changed

+42
-52
lines changed

5 files changed

+42
-52
lines changed

lua/opencode/context.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Gathers editor context
22

33
local util = require('opencode.util')
4-
local config = require('opencode.config')
4+
local config = require('opencode.config') --[[@as OpencodeConfig]]
55
local state = require('opencode.state')
66

77
local M = {}

lua/opencode/ui/completion/context.lua

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ local function add_cursor_data_item(ctx)
199199
)
200200
end
201201

202+
--- Remove the inserted completion text from the input buffer
203+
---@param item CompletionItem
204+
local function remove_inserted_text(item)
205+
local input_win = require('opencode.ui.input_window')
206+
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n')
207+
208+
local _, col = unpack(vim.api.nvim_win_get_cursor(0))
209+
local line = vim.api.nvim_get_current_line()
210+
local key = config.get_key_for_function('input_window', 'context_items')
211+
212+
local completion_text = key .. (item.label or item.insert_text or '')
213+
local text_start = col - #completion_text
214+
215+
if text_start >= 0 and line:sub(text_start + 1, col) == completion_text then
216+
line = line:sub(1, text_start) .. line:sub(col + 1)
217+
input_win.set_current_line(line)
218+
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_win_get_cursor(0)[1], text_start })
219+
elseif col > 0 and line:sub(col, col) == key then
220+
line = line:sub(1, col - 1) .. line:sub(col + 1)
221+
input_win.set_current_line(line)
222+
end
223+
224+
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('a', true, false, true), 'n')
225+
end
226+
202227
---@type CompletionSource
203228
local context_source = {
204229
name = 'context',
@@ -260,27 +285,8 @@ local context_source = {
260285
context.remove_selection(item.data.additional_data --[[@as OpencodeContextSelection]])
261286
end
262287

263-
-- Remove the inserted completion text from the input buffer
264288
vim.schedule(function()
265-
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n')
266-
267-
local _, col = unpack(vim.api.nvim_win_get_cursor(0))
268-
local line = vim.api.nvim_get_current_line()
269-
local key = config.get_key_for_function('input_window', 'context_items')
270-
271-
local completion_text = key .. (item.label or item.insert_text or '')
272-
local text_start = col - #completion_text
273-
274-
if text_start >= 0 and line:sub(text_start + 1, col) == completion_text then
275-
line = line:sub(1, text_start) .. line:sub(col + 1)
276-
input_win.set_current_line(line)
277-
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_win_get_cursor(0)[1], text_start })
278-
elseif col > 0 and line:sub(col, col) == key then
279-
line = line:sub(1, col - 1) .. line:sub(col + 1)
280-
input_win.set_current_line(line)
281-
end
282-
283-
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('a', true, false, true), 'n')
289+
remove_inserted_text(item)
284290
end)
285291
end,
286292
}

lua/opencode/ui/completion/engines/blink_cmp.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ function Source:get_trigger_characters()
1414
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
1515
local context_key = config.get_key_for_function('input_window', 'context_items')
1616
local triggers = {}
17-
if mention_key then
18-
table.insert(triggers, mention_key)
19-
end
20-
if slash_key then
21-
table.insert(triggers, slash_key)
22-
end
23-
if context_key then
24-
table.insert(triggers, context_key)
25-
end
26-
return triggers
17+
return {
18+
slash_key or '',
19+
mention_key or '',
20+
context_key or '',
21+
}
2722
end
2823

2924
function Source:is_available()

lua/opencode/ui/completion/engines/nvim_cmp.lua

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@ function M.setup(completion_sources)
1616
local mention_key = config.get_key_for_function('input_window', 'mention')
1717
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
1818
local context_key = config.get_key_for_function('input_window', 'context_items')
19-
local triggers = {}
20-
if mention_key then
21-
table.insert(triggers, mention_key)
22-
end
23-
if slash_key then
24-
table.insert(triggers, slash_key)
25-
end
26-
if context_key then
27-
table.insert(triggers, context_key)
28-
end
29-
return triggers
19+
return {
20+
slash_key or '',
21+
mention_key or '',
22+
context_key or '',
23+
}
3024
end
3125

3226
function source:is_available()

lua/opencode/ui/completion/engines/vim_complete.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ function M._get_trigger(before_cursor)
3131
local mention_key = config.get_key_for_function('input_window', 'mention')
3232
local slash_key = config.get_key_for_function('input_window', 'slash_commands')
3333
local context_key = config.get_key_for_function('input_window', 'context_items')
34-
local triggers = {}
35-
if mention_key then
36-
table.insert(triggers, mention_key)
37-
end
38-
if slash_key then
39-
table.insert(triggers, slash_key)
40-
end
41-
if context_key then
42-
table.insert(triggers, context_key)
43-
end
34+
local triggers = {
35+
slash_key or '',
36+
mention_key or '',
37+
context_key or '',
38+
}
4439
local trigger_chars = table.concat(vim.tbl_map(vim.pesc, triggers), '')
4540
local trigger_char, trigger_match = before_cursor:match('.*([' .. trigger_chars .. '])([%w_%-%.]*)')
4641
return trigger_char, trigger_match

0 commit comments

Comments
 (0)