aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2020-02-18 23:38:52 -0800
committerGitHub <noreply@github.com>2020-02-18 23:38:52 -0800
commite8f160c82f8d4809320699630e07a7e5f4537e77 (patch)
treed4b6c45adac6dc5cffe3623c298da7fdd673fc08 /runtime/lua/vim/lsp/util.lua
parent521b79c0f85625f99ff626935484a1225360f820 (diff)
parentf3d4ddd0f8b654d58fb4653d88ac7f652e3ad364 (diff)
downloadrneovim-e8f160c82f8d4809320699630e07a7e5f4537e77.tar.gz
rneovim-e8f160c82f8d4809320699630e07a7e5f4537e77.tar.bz2
rneovim-e8f160c82f8d4809320699630e07a7e5f4537e77.zip
Merge #11895 'lsp: fix textDocument/completion handling'
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua23
1 files changed, 22 insertions, 1 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 428874f2b7..6b12b37ec2 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -148,15 +148,36 @@ function M.get_current_line_to_cursor()
return line:sub(pos[2]+1)
end
+-- Sort by CompletionItem.sortText
+-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
+local function sort_completion_items(items)
+ if items[1] and items[1].sortText then
+ table.sort(items, function(a, b) return a.sortText < b.sortText
+ end)
+ end
+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.insertText or item.label
+ return vim.startswith(word, prefix)
+ end, items)
+end
+
--- Getting vim complete-items with incomplete flag.
-- @params CompletionItem[], CompletionList or nil (https://microsoft.github.io/language-server-protocol/specification#textDocument_completion)
-- @return { matches = complete-items table, incomplete = boolean }
-function M.text_document_completion_list_to_complete_items(result)
+function M.text_document_completion_list_to_complete_items(result, prefix)
local items = M.extract_completion_items(result)
if vim.tbl_isempty(items) then
return {}
end
+ items = remove_unmatch_completion_items(items, prefix)
+ sort_completion_items(items)
+
local matches = {}
for _, completion_item in ipairs(items) do