commit 42ed0ffad9851f3794a9dff080a2789c87c6d7c8
parent 5f95f1249f464e4f0ceed468ec5a1ba6e810da14
Author: glepnir <glephunter@gmail.com>
Date: Sat, 31 Aug 2024 02:23:49 +0800
fix(lsp): when prefix is non word add all result into matches (#30044)
Problem: prefix can be a symbol like period, the fuzzy matching can't
handle it correctly.
Solution: when prefix is empty or a symbol add all lsp completion
result into matches.
Diffstat:
2 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/runtime/lua/vim/lsp/completion.lua b/runtime/lua/vim/lsp/completion.lua
@@ -238,7 +238,7 @@ function M._lsp_to_complete_items(result, prefix, client_id)
---@type fun(item: lsp.CompletionItem):boolean
local matches
- if prefix == '' then
+ if not prefix:find('%w') then
matches = function(_)
return true
end
diff --git a/test/functional/plugin/lsp/completion_spec.lua b/test/functional/plugin/lsp/completion_spec.lua
@@ -18,35 +18,36 @@ local create_server_definition = t_lsp.create_server_definition
---@param candidates lsp.CompletionList|lsp.CompletionItem[]
---@param lnum? integer 0-based, defaults to 0
---@return {items: table[], server_start_boundary: integer?}
-local function complete(line, candidates, lnum)
+local function complete(line, candidates, lnum, server_boundary)
lnum = lnum or 0
-- nvim_win_get_cursor returns 0 based column, line:find returns 1 based
local cursor_col = line:find('|') - 1
line = line:gsub('|', '')
return exec_lua(
[[
- local line, cursor_col, lnum, result = ...
+ local line, cursor_col, lnum, result, server_boundary = ...
local line_to_cursor = line:sub(1, cursor_col)
local client_start_boundary = vim.fn.match(line_to_cursor, '\\k*$')
- local items, server_start_boundary = require("vim.lsp.completion")._convert_results(
+ local items, new_server_boundary = require("vim.lsp.completion")._convert_results(
line,
lnum,
cursor_col,
1,
client_start_boundary,
- nil,
+ server_boundary,
result,
"utf-16"
)
return {
items = items,
- server_start_boundary = server_start_boundary
+ server_start_boundary = new_server_boundary
}
]],
line,
cursor_col,
lnum,
- candidates
+ candidates,
+ server_boundary
)
end
@@ -162,6 +163,26 @@ describe('vim.lsp.completion: item conversion', function()
eq(expected, result)
end)
+ it('works on non word prefix', function()
+ local completion_list = {
+ { label = ' foo', insertText = '->foo' },
+ }
+ local result = complete('wp.|', completion_list, 0, 2)
+ local expected = {
+ {
+ abbr = ' foo',
+ word = '->foo',
+ },
+ }
+ result = vim.tbl_map(function(x)
+ return {
+ abbr = x.abbr,
+ word = x.word,
+ }
+ end, result.items)
+ eq(expected, result)
+ end)
+
it('trims trailing newline or tab from textEdit', function()
local range0 = {
start = { line = 0, character = 0 },