lsp: make functions private and use filter function

This commit is contained in:
Hirokazu Hata 2020-02-19 07:39:56 +09:00
parent c1bfc8093f
commit f3d4ddd0f8
No known key found for this signature in database
GPG Key ID: 9AA9860369AE0DE2

View File

@ -129,28 +129,6 @@ function M.extract_completion_items(result)
end
end
-- Sort by CompletionItem.sortText
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
function M.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.
function M.remove_unmatch_completion_items(items, prefix)
local matched_items = {}
for _, item in ipairs(items) do
local word = item.insertText or item.label
if vim.startswith(word, prefix) then
table.insert(matched_items, item)
end
end
return matched_items
end
--- Apply the TextDocumentEdit response.
-- @params TextDocumentEdit [table] see https://microsoft.github.io/language-server-protocol/specification
function M.apply_text_document_edit(text_document_edit)
@ -170,6 +148,24 @@ 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 }
@ -179,8 +175,8 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
return {}
end
items = M.remove_unmatch_completion_items(items, prefix)
M.sort_completion_items(items)
items = remove_unmatch_completion_items(items, prefix)
sort_completion_items(items)
local matches = {}