Skip to content

Commit db91089

Browse files
committed
ui: indentation improvemnts
this commit adds adds indentation guides and further padding between the expanded/collapsed glyph and the symbol type icon/text. this commit also fixes a bug where node.symbol was being checked instead of node.call_hierarchy_item. closes #24 Signed-off-by: ldelossa <louis.delos@gmail.com>
1 parent ad49205 commit db91089

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

doc/calltree.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ The config table is described below:
197197
no_hls = false,
198198
-- user provided icon highlight overrides.
199199
-- see *calltree-icon-highlights*
200-
icon_highlights = {}
200+
icon_highlights = {},
201+
-- bool which enables or diables indentation guides in the UI.
202+
-- defaults to true.
203+
indent_guides = true,
201204
-- user provided UI highlights.
202205
-- see *calltree-ui-highlights*
203206
hls = {}

lua/calltree.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ M.config = {
88
jump_mode = "invoking",
99
icons = "none",
1010
no_hls = false,
11+
indent_guides = true,
1112
icon_highlights = {},
1213
hls = {}
1314
}
@@ -196,6 +197,8 @@ M.nerd = {
196197
Unit = "",
197198
Value = "",
198199
Variable = "",
200+
Expanded = "",
201+
Collapsed = "",
199202
}
200203

201204
M.codicons = {
@@ -232,6 +235,8 @@ M.codicons = {
232235
Unit = "",
233236
Value = "",
234237
Variable = "",
238+
Expanded = "",
239+
Collapsed = "",
235240
}
236241

237242
M.icon_hls = {

lua/calltree/ui/marshal.lua

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
local ct = require('calltree')
2+
local config = require('calltree').config
23
local lsp_util = require('calltree.lsp.util')
34

45
local M = {}
56

67
M.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

Comments
 (0)