Skip to content

Commit c2c5f6f

Browse files
authored
fix(document_symbols): handle SymbolInformation responses (#1644)
1 parent ff33662 commit c2c5f6f

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

Diff for: lua/neo-tree/sources/document_symbols/lib/client_filters.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---Utilities function to filter the LSP servers
22
local utils = require("neo-tree.utils")
33

4-
---@alias LspRespRaw table<integer, { result: LspRespNode }>
4+
---@alias neotree.LspRespRaw table<integer,{err: lsp.ResponseError?, result: any}>
55
local M = {}
66

77
---@alias FilterFn fun(client_name: string): boolean
88

99
---Filter clients
1010
---@param filter_type "first" | "all"
1111
---@param filter_fn FilterFn
12-
---@param resp LspRespRaw
13-
---@return table<string, LspRespNode>
12+
---@param resp neotree.LspRespRaw
13+
---@return table<string, any>
1414
local filter_clients = function(filter_type, filter_fn, resp)
1515
if resp == nil or type(resp) ~= "table" then
1616
return {}
@@ -51,8 +51,8 @@ local ignore = function(ignore)
5151
end
5252

5353
---Main entry point for the filter
54-
---@param resp LspRespRaw
55-
---@return table<string, LspRespNode>
54+
---@param resp neotree.LspRespRaw
55+
---@return table<string, any>
5656
M.filter_resp = function(resp)
5757
return {}
5858
end

Diff for: lua/neo-tree/sources/document_symbols/lib/kinds.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ local kinds_id_to_name = {
3434

3535
local kinds_map = {}
3636

37+
---@class neotree.LspKindDisplay
38+
---@field name string Display name
39+
---@field icon string Icon to render
40+
---@field hl string Highlight for the node
41+
3742
---Get how the kind with kind_id should be rendered
3843
---@param kind_id integer the kind_id to be render
39-
---@return table res of the form { name = kind_display_name, icon = kind_icon, hl = kind_hl }
44+
---@return neotree.LspKindDisplay res
4045
M.get_kind = function(kind_id)
4146
local kind_name = kinds_id_to_name[kind_id]
4247
return vim.tbl_extend(

Diff for: lua/neo-tree/sources/document_symbols/lib/symbols_utils.lua

+26-30
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,23 @@ local M = {}
88
---@alias Loc integer[] a location in a buffer {row, col}, 0-indexed
99
---@alias LocRange { start: Loc, ["end"]: Loc } a range consisting of two loc
1010

11-
---@class SymbolExtra
11+
---@class neotree.SymbolExtra
1212
---@field bufnr integer the buffer containing the symbols,
13-
---@field kind string the kind of each symbol
13+
---@field kind neotree.LspKindDisplay the kind of each symbol
1414
---@field selection_range LocRange the symbol's location
1515
---@field position Loc start of symbol's definition
1616
---@field end_position Loc start of symbol's definition
1717

18-
---@class SymbolNode see
18+
---@class neotree.SymbolNode see
1919
---@field id string
2020
---@field name string name of symbol
2121
---@field path string buffer path - should all be the same
2222
---@field type "root"|"symbol"
23-
---@field children SymbolNode[]
24-
---@field extra SymbolExtra additional info
25-
26-
---@alias LspLoc { line: integer, character: integer}
27-
---@alias LspRange { start : LspLoc, ["end"]: LspLoc }
28-
---@class LspRespNode
29-
---@field name string
30-
---@field detail string?
31-
---@field kind integer
32-
---@field tags any
33-
---@field deprecated boolean?
34-
---@field range LspRange
35-
---@field selectionRange LspRange
36-
---@field children LspRespNode[]
37-
38-
---Parse the LspRange
39-
---@param range LspRange the LspRange object to parse
23+
---@field children neotree.SymbolNode[]
24+
---@field extra neotree.SymbolExtra additional info
25+
26+
---Parse the lsp.Range
27+
---@param range lsp.Range the lsp.Range object to parse
4028
---@return LocRange range the parsed range
4129
local parse_range = function(range)
4230
return {
@@ -95,9 +83,9 @@ end
9583
---Parse the LSP response into a tree. Each node on the tree follows
9684
---the same structure as a NuiTree node, with the extra field
9785
---containing additional information.
98-
---@param resp_node LspRespNode the LSP response node
86+
---@param resp_node lsp.DocumentSymbol|lsp.SymbolInformation the LSP response node
9987
---@param id string the id of the current node
100-
---@return SymbolNode symb_node the parsed tree
88+
---@return neotree.SymbolNode symb_node the parsed tree
10189
local function parse_resp(resp_node, id, state, parent_search_path)
10290
-- parse all children
10391
local children = {}
@@ -107,29 +95,37 @@ local function parse_resp(resp_node, id, state, parent_search_path)
10795
table.insert(children, child_node)
10896
end
10997

110-
-- parse current node
111-
local preview_range = parse_range(resp_node.range)
112-
local symb_node = {
98+
---@type neotree.SymbolNode
99+
local symbol_node = {
113100
id = id,
114101
name = resp_node.name,
115102
type = "symbol",
116103
path = state.path,
117104
children = children,
105+
---@diagnostic disable-next-line: missing-fields
118106
extra = {
119107
bufnr = state.lsp_bufnr,
120108
kind = kinds.get_kind(resp_node.kind),
121-
selection_range = parse_range(resp_node.selectionRange),
122109
search_path = search_path,
123110
-- detail = resp_node.detail,
124-
position = preview_range.start,
125-
end_position = preview_range["end"],
126111
},
127112
}
128-
return symb_node
113+
local preview_range = resp_node.range
114+
if preview_range then
115+
---@cast resp_node lsp.DocumentSymbol
116+
symbol_node.extra.selection_range = parse_range(resp_node.selectionRange)
117+
else
118+
---@cast resp_node lsp.SymbolInformation
119+
preview_range = resp_node.location.range
120+
symbol_node.extra.selection_range = parse_range(preview_range)
121+
end
122+
symbol_node.extra.position = preview_range.start
123+
symbol_node.extra.end_position = preview_range["end"]
124+
return symbol_node
129125
end
130126

131127
---Callback function for lsp request
132-
---@param lsp_resp LspRespRaw the response of the lsp client
128+
---@param lsp_resp neotree.LspRespRaw the response of the lsp client
133129
---@param state table the state of the source
134130
local on_lsp_resp = function(lsp_resp, state)
135131
if lsp_resp == nil or type(lsp_resp) ~= "table" then

0 commit comments

Comments
 (0)