diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2025-02-11 03:38:07 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-11 03:38:07 -0800 |
commit | 891d2f4029259b2790b9aa3ca71ebca0ff3d7eba (patch) | |
tree | 136651f6825b8d530f0e607e48674368608dc5ef /runtime/lua/vim/lsp/util.lua | |
parent | 3abfaafad255079f39a0843fb1b601db00d739af (diff) | |
parent | e8b5dd1e89bfa984e7b943443a291484cba23fac (diff) | |
download | rneovim-891d2f4029259b2790b9aa3ca71ebca0ff3d7eba.tar.gz rneovim-891d2f4029259b2790b9aa3ca71ebca0ff3d7eba.tar.bz2 rneovim-891d2f4029259b2790b9aa3ca71ebca0ff3d7eba.zip |
Merge #30860 LSP: symbols_to_items()
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index e16a905c44..86c0a2b3db 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -1775,39 +1775,57 @@ 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 +---@param position_encoding? 'utf-8'|'utf-16'|'utf-32' +--- default to first client of buffer ---@return vim.quickfix.entry[] # See |setqflist()| for the format -function M.symbols_to_items(symbols, bufnr) - bufnr = bufnr or 0 +function M.symbols_to_items(symbols, bufnr, position_encoding) + bufnr = vim._resolve_bufnr(bufnr) + if position_encoding == nil then + vim.notify_once( + 'symbols_to_items must be called with valid position encoding', + vim.log.levels.WARN + ) + position_encoding = vim.lsp.get_clients({ bufnr = 0 })[1].offset_encoding + end + 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 = get_line_byte_from_position(bufnr, range['start'], position_encoding) + 1 + local end_lnum = range['end'].line + 1 + local end_col = get_line_byte_from_position(bufnr, range['end'], position_encoding) + 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, } end if symbol.children then - list_extend(items, M.symbols_to_items(symbol.children, bufnr)) + list_extend(items, M.symbols_to_items(symbol.children, bufnr, position_encoding)) end end |