feat(lsp.util): refactor symbols_to_items()

- Remove the trivial function vim.lsp.util._get_symbol_kind_name()
  and its tests.
This commit is contained in:
Lewis Russell 2024-10-16 09:58:10 +01:00
parent 5bec7288a5
commit f0973d4227
2 changed files with 31 additions and 81 deletions

View File

@ -1838,57 +1838,45 @@ function M.locations_to_items(locations, offset_encoding)
return items return items
end end
-- According 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
--- Converts symbols to quickfix list items. --- Converts symbols to quickfix list items.
--- ---
---@param symbols lsp.DocumentSymbol[]|lsp.SymbolInformation[] ---@param symbols lsp.DocumentSymbol[]|lsp.SymbolInformation[]
---@param bufnr? integer ---@param bufnr? integer
---@return vim.quickfix.entry[] # See |setqflist()| for the format ---@return vim.quickfix.entry[] # See |setqflist()| for the format
function M.symbols_to_items(symbols, bufnr) function M.symbols_to_items(symbols, bufnr)
---@param _symbols lsp.DocumentSymbol[]|lsp.SymbolInformation[] bufnr = bufnr or 0
---@param _items vim.quickfix.entry[] local items = {} --- @type vim.quickfix.entry[]
---@param _bufnr integer for _, symbol in ipairs(symbols) do
---@return vim.quickfix.entry[] --- @type string?, lsp.Position?
local function _symbols_to_items(_symbols, _items, _bufnr) local filename, pos
for _, symbol in ipairs(_symbols) do
if symbol.location then -- SymbolInformation type if symbol.location then
local range = symbol.location.range --- @cast symbol lsp.SymbolInformation
local kind = M._get_symbol_kind_name(symbol.kind) filename = vim.uri_to_fname(symbol.location.uri)
_items[#_items + 1] = { pos = symbol.location.range.start
filename = vim.uri_to_fname(symbol.location.uri), elseif symbol.selectionRange then
lnum = range.start.line + 1, --- @cast symbol lsp.DocumentSymbol
col = range.start.character + 1, filename = api.nvim_buf_get_name(bufnr)
kind = kind, pos = symbol.selectionRange.start
text = '[' .. kind .. '] ' .. symbol.name, end
}
elseif symbol.selectionRange then -- DocumentSymbole type if filename and pos then
local kind = M._get_symbol_kind_name(symbol.kind) local kind = protocol.SymbolKind[symbol.kind] or 'Unknown'
_items[#_items + 1] = { items[#items + 1] = {
-- bufnr = _bufnr, filename = filename,
filename = api.nvim_buf_get_name(_bufnr), lnum = pos.line + 1,
lnum = symbol.selectionRange.start.line + 1, col = pos.character + 1,
col = symbol.selectionRange.start.character + 1,
kind = kind, kind = kind,
text = '[' .. kind .. '] ' .. symbol.name, text = '[' .. kind .. '] ' .. symbol.name,
} }
end
if symbol.children then if symbol.children then
for _, v in ipairs(_symbols_to_items(symbol.children, _items, _bufnr)) do list_extend(items, M.symbols_to_items(symbol.children, bufnr))
for _, s in ipairs(v) do
table.insert(_items, s)
end end
end end
end
end return items
end
return _items
end
return _symbols_to_items(symbols, {}, bufnr or 0)
end end
--- Removes empty lines from the beginning and end. --- Removes empty lines from the beginning and end.

View File

@ -3134,44 +3134,6 @@ describe('LSP', function()
end) end)
end) end)
describe('lsp.util._get_symbol_kind_name', function()
it('returns the name specified by protocol', function()
eq(
'File',
exec_lua(function()
return vim.lsp.util._get_symbol_kind_name(1)
end)
)
eq(
'TypeParameter',
exec_lua(function()
return vim.lsp.util._get_symbol_kind_name(26)
end)
)
end)
it('returns the name not specified by protocol', function()
eq(
'Unknown',
exec_lua(function()
return vim.lsp.util._get_symbol_kind_name(nil)
end)
)
eq(
'Unknown',
exec_lua(function()
return vim.lsp.util._get_symbol_kind_name(vim.NIL)
end)
)
eq(
'Unknown',
exec_lua(function()
return vim.lsp.util._get_symbol_kind_name(1000)
end)
)
end)
end)
describe('lsp.util.jump_to_location', function() describe('lsp.util.jump_to_location', function()
local target_bufnr --- @type integer local target_bufnr --- @type integer