Skip to content

Commit f3e6bd3

Browse files
committed
refactor(lsp): clean up completion handler and add unit tests
1 parent ae4610a commit f3e6bd3

File tree

4 files changed

+1188
-50
lines changed

4 files changed

+1188
-50
lines changed

lua/opencode/lsp/opencode_completion_ls.lua

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ local function get_completion_context(params)
4343
local line = lines[1] or ''
4444
local line_to_cursor = line:sub(1, col)
4545

46-
-- Find the trigger character
4746
local triggers = completion.get_trigger_characters()
4847
for _, t in ipairs(triggers) do
4948
if t and line_to_cursor:match(vim.pesc(t) .. '[^%s]*$') then
@@ -100,7 +99,7 @@ end
10099

101100
---Completion handler - provides completion items
102101
---@param params lsp.CompletionParams
103-
---@param callback fun(err?: lsp.ResponseError, result: lsp.CompletionItem[])
102+
---@param callback fun(err?: lsp.ResponseError, result: lsp.CompletionItem[] | lsp.CompletionList)
104103
handlers[ms.textDocument_completion] = function(params, callback)
105104
local word, trigger_char, line = get_completion_context(params)
106105

@@ -124,8 +123,8 @@ handlers[ms.textDocument_completion] = function(params, callback)
124123
Promise.all(promises)
125124
:and_then(function(results)
126125
local all_items = {}
127-
128126
local is_incomplete = false
127+
129128
for i, items in ipairs(results) do
130129
for _, item in ipairs(items or {}) do
131130
local source = completion.get_source_by_name(item.source_name)
@@ -137,10 +136,7 @@ handlers[ms.textDocument_completion] = function(params, callback)
137136
end
138137
end
139138

140-
callback(nil, {
141-
isIncomplete = is_incomplete,
142-
items = all_items,
143-
})
139+
callback(nil, { isIncomplete = is_incomplete, items = all_items })
144140
completion.store_completion_items(all_items)
145141
end)
146142
:catch(function(err)
@@ -150,15 +146,6 @@ handlers[ms.textDocument_completion] = function(params, callback)
150146
end)
151147
end
152148

153-
---Resolve handler - provides additional documentation for completion items
154-
---@param params lsp.CompletionItem
155-
---@param callback fun(err?: lsp.ResponseError, result: lsp.CompletionItem)
156-
handlers[ms.completionItem_resolve] = function(params, callback)
157-
local item = vim.deepcopy(params)
158-
159-
callback(nil, item)
160-
end
161-
162149
---Create the LSP server configuration
163150
---@return vim.lsp.ClientConfig
164151
function M.create_config()

tests/unit/api_spec.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,40 +146,6 @@ describe('opencode.api', function()
146146
end)
147147
end)
148148

149-
describe('completion triggers', function()
150-
it('does not restore cursor position for context_items', function()
151-
local config = require('opencode.config')
152-
local original_get_key_for_function = config.get_key_for_function
153-
local original_completion = package.loaded['opencode.ui.completion']
154-
155-
config.get_key_for_function = function(scope, function_name)
156-
if scope == 'input_window' and function_name == 'context_items' then
157-
return '#'
158-
end
159-
return original_get_key_for_function(scope, function_name)
160-
end
161-
162-
local trigger_char = nil
163-
package.loaded['opencode.ui.completion'] = {
164-
trigger_completion = function(char)
165-
return function()
166-
trigger_char = char
167-
end
168-
end,
169-
}
170-
171-
stub(ui, 'focus_input')
172-
173-
api.context_items()
174-
175-
config.get_key_for_function = original_get_key_for_function
176-
package.loaded['opencode.ui.completion'] = original_completion
177-
178-
assert.stub(ui.focus_input).was_called_with({ restore_position = false, start_insert = true })
179-
assert.equal('#', trigger_char)
180-
end)
181-
end)
182-
183149
describe('run command argument parsing', function()
184150
it('parses agent prefix and passes to send_message', function()
185151
api.commands.run.fn({ 'agent=plan', 'analyze', 'this', 'code' }):wait()

0 commit comments

Comments
 (0)