aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
authorAndrey Avramenko <andreyavr@gmail.com>2020-04-20 01:59:09 +0300
committerAndrey Avramenko <andreyavr@gmail.com>2020-04-20 14:09:13 +0300
commite6cfc1b158506f8d2a4b598d07051b7f3de5f964 (patch)
tree8a074cca57e7ffbf497cd363a842044aee0b73e1 /runtime/lua/vim/lsp/util.lua
parentccb038dc6aaf72e052ef8afb810f84a331d4ccec (diff)
downloadrneovim-e6cfc1b158506f8d2a4b598d07051b7f3de5f964.tar.gz
rneovim-e6cfc1b158506f8d2a4b598d07051b7f3de5f964.tar.bz2
rneovim-e6cfc1b158506f8d2a4b598d07051b7f3de5f964.zip
LSP/completion: Add completion text helper function
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua16
1 files changed, 14 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 533bcf2779..5217464c22 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -157,11 +157,23 @@ local function sort_completion_items(items)
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.
-- So we exclude completion candidates whose prefix does not match.
local function remove_unmatch_completion_items(items, prefix)
return vim.tbl_filter(function(item)
- local word = (item.textEdit and item.textEdit.newText) or item.insertText or item.label
+ local word = get_completion_word(item)
return vim.startswith(word, prefix)
end, items)
end
@@ -193,7 +205,7 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
end
end
- local word = (completion_item.textEdit and completion_item.textEdit.newText) or completion_item.insertText or completion_item.label
+ local word = get_completion_word(completion_item)
table.insert(matches, {
word = word,
abbr = completion_item.label,