mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
Merge pull request #11989 from Weypare/textEdit
LSP/completion: add textEdit support
This commit is contained in:
@@ -157,11 +157,23 @@ local function sort_completion_items(items)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Returns text that should be inserted when selecting completion item. The precedence is as follows:
|
||||||
|
-- textEdit.newText > insertText > label
|
||||||
|
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
|
||||||
|
local function get_completion_word(item)
|
||||||
|
if item.textEdit ~= nil and item.textEdit.newText ~= nil then
|
||||||
|
return item.textEdit.newText
|
||||||
|
elseif item.insertText ~= nil then
|
||||||
|
return item.insertText
|
||||||
|
end
|
||||||
|
return item.label
|
||||||
|
end
|
||||||
|
|
||||||
-- Some lanuguage servers return complementary candidates whose prefixes do not match are also returned.
|
-- Some lanuguage servers return complementary candidates whose prefixes do not match are also returned.
|
||||||
-- So we exclude completion candidates whose prefix does not match.
|
-- So we exclude completion candidates whose prefix does not match.
|
||||||
local function remove_unmatch_completion_items(items, prefix)
|
local function remove_unmatch_completion_items(items, prefix)
|
||||||
return vim.tbl_filter(function(item)
|
return vim.tbl_filter(function(item)
|
||||||
local word = item.insertText or item.label
|
local word = get_completion_word(item)
|
||||||
return vim.startswith(word, prefix)
|
return vim.startswith(word, prefix)
|
||||||
end, items)
|
end, items)
|
||||||
end
|
end
|
||||||
@@ -193,7 +205,7 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local word = completion_item.insertText or completion_item.label
|
local word = get_completion_word(completion_item)
|
||||||
table.insert(matches, {
|
table.insert(matches, {
|
||||||
word = word,
|
word = word,
|
||||||
abbr = completion_item.label,
|
abbr = completion_item.label,
|
||||||
|
@@ -812,4 +812,37 @@ describe('LSP', function()
|
|||||||
}, buf_lines(1))
|
}, buf_lines(1))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('completion_list_to_complete_items', function()
|
||||||
|
-- Completion option precedence:
|
||||||
|
-- textEdit.newText > insertText > label
|
||||||
|
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
|
||||||
|
it('should choose right completion option', function ()
|
||||||
|
local prefix = 'foo'
|
||||||
|
local completion_list = {
|
||||||
|
-- resolves into label
|
||||||
|
{ label='foobar' },
|
||||||
|
{ label='foobar', textEdit={} },
|
||||||
|
-- resolves into insertText
|
||||||
|
{ label='foocar', insertText='foobar' },
|
||||||
|
{ label='foocar', insertText='foobar', textEdit={} },
|
||||||
|
-- resolves into textEdit.newText
|
||||||
|
{ label='foocar', insertText='foodar', textEdit={newText='foobar'} },
|
||||||
|
{ label='foocar', textEdit={newText='foobar'} }
|
||||||
|
}
|
||||||
|
local completion_list_items = {items=completion_list}
|
||||||
|
local expected = {
|
||||||
|
{ abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
{ abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar'},
|
||||||
|
}
|
||||||
|
|
||||||
|
eq(expected, exec_lua([[return vim.lsp.util.text_document_completion_list_to_complete_items(...)]], completion_list, prefix))
|
||||||
|
eq(expected, exec_lua([[return vim.lsp.util.text_document_completion_list_to_complete_items(...)]], completion_list_items, prefix))
|
||||||
|
eq({}, exec_lua([[return vim.lsp.util.text_document_completion_list_to_complete_items(...)]], {}, prefix))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user