fix(lsp): prefix value sub index and remove symbols in word

Problem:
1. comletion prefix value sub index not correct

2. wrong word generate this will include symbol like -> , it will make compl_match_array build failed

Solution:
1. use max value of server_bounary, word_boundary
2. use vimregex keyword remove unncessary symbols.
This commit is contained in:
glepnir 2024-09-09 18:05:25 +08:00
parent b40ec083ae
commit 2648c67c98
2 changed files with 53 additions and 4 deletions

View File

@ -154,8 +154,13 @@ local function get_completion_word(item)
return item.label return item.label
end end
elseif item.textEdit then elseif item.textEdit then
local word = item.textEdit.newText local word = item.textEdit.newText:gsub('%s+$', '')
return word:match('^(%S*)') or word if word:match('^%W') then
-- remove unnecessary symbols in label like "->w_locked" => "w_locked"
local matched_word = vim.fn.matchstr(word, '\\k\\+')
return matched_word ~= '' and matched_word or word
end
return word
elseif item.insertText and item.insertText ~= '' then elseif item.insertText and item.insertText ~= '' then
return item.insertText return item.insertText
end end
@ -365,7 +370,8 @@ function M._convert_results(
elseif curstartbyte ~= nil and curstartbyte ~= server_start_boundary then elseif curstartbyte ~= nil and curstartbyte ~= server_start_boundary then
server_start_boundary = client_start_boundary server_start_boundary = client_start_boundary
end end
local prefix = line:sub((server_start_boundary or client_start_boundary) + 1, cursor_col) local prefix =
line:sub(math.max(server_start_boundary or 0, client_start_boundary) + 1, cursor_col)
local matches = M._lsp_to_complete_items(result, prefix, client_id) local matches = M._lsp_to_complete_items(result, prefix, client_id)
return matches, server_start_boundary return matches, server_start_boundary
end end
@ -461,7 +467,7 @@ local function trigger(bufnr, clients)
vim.list_extend(matches, client_matches) vim.list_extend(matches, client_matches)
end end
end end
local start_col = (server_start_boundary or word_boundary) + 1 local start_col = math.max(server_start_boundary or 0, word_boundary) + 1
vim.fn.complete(start_col, matches) vim.fn.complete(start_col, matches)
end) end)

View File

@ -218,6 +218,49 @@ describe('vim.lsp.completion: item conversion', function()
eq(expected, result) eq(expected, result)
end) end)
it('removes unnecessary symbols in label', function()
local items = {
{
detail = 'colnr_T',
filterText = 'w_virtcol',
insertText = '->w_virtcol',
insertTextFormat = 1,
kind = 5,
label = ' w_virtcol',
score = 0.59581798315048,
sortText = '40e77879w_virtcol',
textEdit = {
newText = '->w_virtcol',
range = {
['end'] = {
character = 0,
line = 0,
},
start = {
character = 0,
line = 0,
},
},
},
},
}
local result = complete('|', items)
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
local expected = {
{
abbr = ' w_virtcol',
word = 'w_virtcol',
},
}
eq(expected, result)
end)
it('prefers wordlike components for snippets', function() it('prefers wordlike components for snippets', function()
-- There are two goals here: -- There are two goals here:
-- --