11local ct = require (' calltree' )
2+ local config = require (' calltree' ).config
23local lsp_util = require (' calltree.lsp.util' )
34
45local M = {}
56
67M .glyphs = {
78 expanded = " ▼" ,
89 collapsed = " ▶" ,
9- separator = " •"
10+ separator = " •" ,
11+ guide = " │" ,
12+ end_guide = " ╰" ,
13+ t_guide = " ├" ,
14+ space = " "
15+
1016}
1117
1218-- buf_line_map keeps a mapping between marshaled
@@ -23,20 +29,25 @@ M.buf_line_map = {}
2329-- string - a string for the collapsed or expanded symbol name
2430-- table - a virt_text chunk that can be passed directly to
2531-- vim.api.nvim_buf_set_extmark() via the virt_text option.
26- function M .marshal_node (node )
27- local str = " "
28- local glyph
32+ function M .marshal_node (node , final )
33+ local glyph = " "
2934 if node .expanded then
30- glyph = M .glyphs [" expanded" ]
35+ if ct .active_icon_set ~= nil then
36+ glyph = ct .active_icon_set .Expanded
37+ else
38+ glyph = M .glyphs [" expanded" ]
39+ end
3140 else
32- glyph = M .glyphs [" collapsed" ]
41+ if ct .active_icon_set ~= nil then
42+ glyph = ct .active_icon_set .Collapsed
43+ else
44+ glyph = M .glyphs [" collapsed" ]
45+ end
3346 end
3447
3548 -- prefer using workspace symbol details if available.
3649 -- fallback to callhierarchy object details.
37- local name = " "
38- local kind = " "
39- local detail = " "
50+ local name , kind , detail , str = " " , " " , " " , " "
4051 if node .symbol ~= nil then
4152 name = node .symbol .name
4253 kind = vim .lsp .protocol .SymbolKind [node .symbol .kind ]
@@ -62,30 +73,43 @@ function M.marshal_node(node)
6273 if relative then
6374 detail = file
6475 elseif node .call_hierarchy_item .detail ~= nil then
65- detail = node .symbol .detail
76+ detail = node .call_hierarchy_item .detail
6677 end
6778 end
79+
6880 local icon = " "
6981 if kind ~= " " then
7082 if ct .active_icon_set ~= nil then
7183 icon = ct .active_icon_set [kind ]
7284 end
7385 end
7486
75- -- add spacing up to node's depth
76- for _ = 1 , node .depth do
77- str = str .. " "
87+ -- compute guides or spaces dependent on configs.
88+ if config .indent_guides then
89+ for i = 1 , node .depth do
90+ if final and i == node .depth and # node .children == 0 then
91+ str = str .. M .glyphs .end_guide .. M .glyphs .space
92+ elseif final and i == node .depth and # node .children > 0 then
93+ str = str .. M .glyphs .t_guide .. M .glyphs .space
94+ else
95+ str = str .. M .glyphs .guide .. M .glyphs .space
96+ end
97+ end
98+ else
99+ for _ = 1 , node .depth do
100+ str = str .. M .glyphs .space
101+ end
78102 end
79103
80104 -- ▶ Func1
81- str = str .. glyph
105+ str = str .. glyph .. M . glyphs . space
82106
83107 if ct .config .icons ~= " none" then
84108 -- ▶ Func1
85- str = str .. " " .. icon .. " " .. name
109+ str = str .. M . glyphs . space .. icon .. M . glyphs . space .. M . glyphs . space .. name
86110 else
87111 -- ▶ [Function] Func1
88- str = str .. " " .. " [" .. kind .. " ]" .. " " .. M .glyphs .separator .. " " .. name
112+ str = str .. M . glyphs . space .. " [" .. kind .. " ]" .. M . glyphs . space .. M .glyphs .separator .. M . glyphs . space .. name
89113 end
90114
91115 -- return detail as virtual text chunk.
@@ -121,24 +145,27 @@ end
121145-- begin.
122146--
123147-- tree : tree_handle - a handle to a the tree we are marshaling.
124- function M .marshal_tree (buf_handle , lines , node , tree , virtual_text_lines )
148+ function M .marshal_tree (buf_handle , lines , node , tree , virtual_text_lines , final )
125149 if node .depth == 0 then
126- if virtual_text_lines == nil then
127- virtual_text_lines = {}
128- end
150+ virtual_text_lines = {}
129151 -- create a new line mapping
130152 M .buf_line_map [tree ] = {}
131153 end
132- local line , virtual_text = M .marshal_node (node )
154+ local line , virtual_text = M .marshal_node (node , final )
133155 table.insert (lines , line )
134156 table.insert (virtual_text_lines , virtual_text )
135157 M .buf_line_map [tree ][# lines ] = node
136158
137159 -- if we are an expanded node or we are the root (always expand)
138160 -- recurse
139161 if node .expanded or node .depth == 0 then
140- for _ , child in ipairs (node .children ) do
141- M .marshal_tree (buf_handle , lines , child , tree , virtual_text_lines )
162+ for i , child in ipairs (node .children ) do
163+ if i == # node .children then
164+ final = true
165+ else
166+ final = false
167+ end
168+ M .marshal_tree (buf_handle , lines , child , tree , virtual_text_lines , final )
142169 end
143170 end
144171
0 commit comments