lsp: Handle unknown CompletionItemKind and SymbolKind (#12257)

* lsp: handle kinds not specified in protocol

fix: #12200
If the client set "symbolKind.valueSet",
the client must handle it properly even if it receives a value outside the specification.

* test: add lsp.util.{get_completion_item_kind_name, get_symbol_kind_name} test case

* lsp: make lsp.util.{get_completion_item_kind_name, get_symbol_kind_name} private
This commit is contained in:
Hirokazu Hata 2020-05-08 05:23:25 +09:00 committed by GitHub
parent 1407899c32
commit 9a67b030d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 9 deletions

View File

@ -203,6 +203,13 @@ local function remove_unmatch_completion_items(items, prefix)
end, items) end, items)
end end
-- Acording to LSP spec, if the client set "completionItemKind.valueSet",
-- the client must handle it properly even if it receives a value outside the specification.
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
function M._get_completion_item_kind_name(completion_item_kind)
return protocol.CompletionItemKind[completion_item_kind] or "Unknown"
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 }
@ -234,7 +241,7 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
table.insert(matches, { table.insert(matches, {
word = word, word = word,
abbr = completion_item.label, abbr = completion_item.label,
kind = protocol.CompletionItemKind[completion_item.kind] or '', kind = M._get_completion_item_kind_name(completion_item.kind),
menu = completion_item.detail or '', menu = completion_item.detail or '',
info = info, info = info,
icase = 1, icase = 1,
@ -935,6 +942,13 @@ function M.set_qflist(items)
}) })
end end
-- Acording to LSP spec, if the client set "symbolKind.valueSet",
-- the client must handle it properly even if it receives a value outside the specification.
-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol
function M._get_symbol_kind_name(symbol_kind)
return protocol.SymbolKind[symbol_kind] or "Unknown"
end
--- Convert symbols to quickfix list items --- Convert symbols to quickfix list items
--- ---
--@symbols DocumentSymbol[] or SymbolInformation[] --@symbols DocumentSymbol[] or SymbolInformation[]
@ -943,7 +957,7 @@ function M.symbols_to_items(symbols, bufnr)
for _, symbol in ipairs(_symbols) do for _, symbol in ipairs(_symbols) do
if symbol.location then -- SymbolInformation type if symbol.location then -- SymbolInformation type
local range = symbol.location.range local range = symbol.location.range
local kind = protocol.SymbolKind[symbol.kind] local kind = M._get_symbol_kind_name(symbol.kind)
table.insert(_items, { table.insert(_items, {
filename = vim.uri_to_fname(symbol.location.uri), filename = vim.uri_to_fname(symbol.location.uri),
lnum = range.start.line + 1, lnum = range.start.line + 1,
@ -952,7 +966,7 @@ function M.symbols_to_items(symbols, bufnr)
text = '['..kind..'] '..symbol.name, text = '['..kind..'] '..symbol.name,
}) })
elseif symbol.range then -- DocumentSymbole type elseif symbol.range then -- DocumentSymbole type
local kind = protocol.SymbolKind[symbol.kind] local kind = M._get_symbol_kind_name(symbol.kind)
table.insert(_items, { table.insert(_items, {
-- bufnr = _bufnr, -- bufnr = _bufnr,
filename = vim.api.nvim_buf_get_name(_bufnr), filename = vim.api.nvim_buf_get_name(_bufnr),

View File

@ -934,12 +934,12 @@ describe('LSP', function()
} }
local completion_list_items = {items=completion_list} local completion_list_items = {items=completion_list}
local expected = { local expected = {
{ abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label = 'foobar' } } } } }, { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label = 'foobar' } } } } },
{ abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foobar', textEdit={} } } } } }, { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foobar', textEdit={} } } } } },
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar' } } } } }, { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar' } } } } },
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar', textEdit={} } } } } }, { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar', textEdit={} } } } } },
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foodar', textEdit={newText='foobar'} } } } } }, { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foodar', textEdit={newText='foobar'} } } } } },
{ abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', textEdit={newText='foobar'} } } } } }, { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', textEdit={newText='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, prefix))
@ -1239,4 +1239,28 @@ describe('LSP', function()
]]) ]])
end) end)
end) end)
describe('lsp.util._get_completion_item_kind_name', function()
describe('returns the name specified by protocol', function()
eq("Text", exec_lua("return vim.lsp.util._get_completion_item_kind_name(1)"))
eq("TypeParameter", exec_lua("return vim.lsp.util._get_completion_item_kind_name(25)"))
end)
describe('returns the name not specified by protocol', function()
eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(nil)"))
eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(vim.NIL)"))
eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(1000)"))
end)
end)
describe('lsp.util._get_symbol_kind_name', function()
describe('returns the name specified by protocol', function()
eq("File", exec_lua("return vim.lsp.util._get_symbol_kind_name(1)"))
eq("TypeParameter", exec_lua("return vim.lsp.util._get_symbol_kind_name(26)"))
end)
describe('returns the name not specified by protocol', function()
eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(nil)"))
eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(vim.NIL)"))
eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(1000)"))
end)
end)
end) end)