mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
lsp: make functions private and use filter function
This commit is contained in:
@@ -129,28 +129,6 @@ function M.extract_completion_items(result)
|
|||||||
end
|
end
|
||||||
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.
|
--- Apply the TextDocumentEdit response.
|
||||||
-- @params TextDocumentEdit [table] see https://microsoft.github.io/language-server-protocol/specification
|
-- @params TextDocumentEdit [table] see https://microsoft.github.io/language-server-protocol/specification
|
||||||
function M.apply_text_document_edit(text_document_edit)
|
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)
|
return line:sub(pos[2]+1)
|
||||||
end
|
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.
|
--- Getting vim complete-items with incomplete flag.
|
||||||
-- @params CompletionItem[], CompletionList or nil (https://microsoft.github.io/language-server-protocol/specification#textDocument_completion)
|
-- @params CompletionItem[], CompletionList or nil (https://microsoft.github.io/language-server-protocol/specification#textDocument_completion)
|
||||||
-- @return { matches = complete-items table, incomplete = boolean }
|
-- @return { matches = complete-items table, incomplete = boolean }
|
||||||
@@ -179,8 +175,8 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
|
|||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
|
||||||
items = M.remove_unmatch_completion_items(items, prefix)
|
items = remove_unmatch_completion_items(items, prefix)
|
||||||
M.sort_completion_items(items)
|
sort_completion_items(items)
|
||||||
|
|
||||||
local matches = {}
|
local matches = {}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user