feat(lsp): include end_col and end_lnum in vim.lsp.buf.symbols_to_items

This commit is contained in:
Yi Ming 2025-02-11 17:09:39 +08:00
parent f8cbdbb4a8
commit 6722149776
3 changed files with 25 additions and 13 deletions

View File

@ -2380,8 +2380,10 @@ symbols_to_items({symbols}, {bufnr}) *vim.lsp.util.symbols_to_items()*
Converts symbols to quickfix list items.
Parameters: ~
• {symbols} (`lsp.DocumentSymbol[]|lsp.SymbolInformation[]`)
• {bufnr} (`integer?`)
• {symbols} (`lsp.DocumentSymbol[]|lsp.SymbolInformation[]`) list of
symbols
• {bufnr} (`integer?`) buffer handle or 0 for current, defaults to
current
Return: ~
(`vim.quickfix.entry[]`) See |setqflist()| for the format

View File

@ -277,7 +277,8 @@ LSP
• `:checkhealth vim.lsp` displays the server version (if available).
• Completion side effects (including snippet expansion, execution of commands
and application of additional text edits) is now built-in.
• |vim.lsp.util.locations_to_items()| sets `end_col` and `end_lnum` fields.
• |vim.lsp.util.locations_to_items()| and |vim.lsp.util.symbols_to_items()| now
sets `end_col` and `end_lnum` fields.
• |vim.lsp.buf.format()| now supports passing a list of ranges
via the `range` parameter (this requires support for the
`textDocument/rangesFormatting` request).

View File

@ -1775,32 +1775,41 @@ end
--- Converts symbols to quickfix list items.
---
---@param symbols lsp.DocumentSymbol[]|lsp.SymbolInformation[]
---@param bufnr? integer
---@param symbols lsp.DocumentSymbol[]|lsp.SymbolInformation[] list of symbols
---@param bufnr? integer buffer handle or 0 for current, defaults to current
---@return vim.quickfix.entry[] # See |setqflist()| for the format
function M.symbols_to_items(symbols, bufnr)
bufnr = bufnr or 0
bufnr = vim._resolve_bufnr(bufnr)
local items = {} --- @type vim.quickfix.entry[]
for _, symbol in ipairs(symbols) do
--- @type string?, lsp.Position?
local filename, pos
--- @type string?, lsp.Range?
local filename, range
if symbol.location then
--- @cast symbol lsp.SymbolInformation
filename = vim.uri_to_fname(symbol.location.uri)
pos = symbol.location.range.start
range = symbol.location.range
elseif symbol.selectionRange then
--- @cast symbol lsp.DocumentSymbol
filename = api.nvim_buf_get_name(bufnr)
pos = symbol.selectionRange.start
range = symbol.selectionRange
end
if filename and pos then
if filename and range then
local kind = protocol.SymbolKind[symbol.kind] or 'Unknown'
local lnum = range['start'].line + 1
local col = range['start'].character + 1
local end_lnum = range['end'].line + 1
local end_col = range['end'].character + 1
items[#items + 1] = {
filename = filename,
lnum = pos.line + 1,
col = pos.character + 1,
lnum = lnum,
col = col,
end_lnum = end_lnum,
end_col = end_col,
kind = kind,
text = '[' .. kind .. '] ' .. symbol.name,
}