Skip to content

Commit

Permalink
ui: indentation improvemnts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ldelossa committed Nov 26, 2021
1 parent ad49205 commit db91089
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
5 changes: 4 additions & 1 deletion doc/calltree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ The config table is described below:
no_hls = false,
-- user provided icon highlight overrides.
-- see *calltree-icon-highlights*
icon_highlights = {}
icon_highlights = {},
-- bool which enables or diables indentation guides in the UI.
-- defaults to true.
indent_guides = true,
-- user provided UI highlights.
-- see *calltree-ui-highlights*
hls = {}
Expand Down
5 changes: 5 additions & 0 deletions lua/calltree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ M.config = {
jump_mode = "invoking",
icons = "none",
no_hls = false,
indent_guides = true,
icon_highlights = {},
hls = {}
}
Expand Down Expand Up @@ -196,6 +197,8 @@ M.nerd = {
Unit = "",
Value = "",
Variable = "",
Expanded = "",
Collapsed = "",
}

M.codicons = {
Expand Down Expand Up @@ -232,6 +235,8 @@ M.codicons = {
Unit = "",
Value = "",
Variable = "",
Expanded = "",
Collapsed = "",
}

M.icon_hls = {
Expand Down
73 changes: 50 additions & 23 deletions lua/calltree/ui/marshal.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
local ct = require('calltree')
local config = require('calltree').config
local lsp_util = require('calltree.lsp.util')

local M = {}

M.glyphs = {
expanded= "",
collapsed= "",
separator = ""
separator = "",
guide = "",
end_guide = "",
t_guide = "",
space = " "

}

-- buf_line_map keeps a mapping between marshaled
Expand All @@ -23,20 +29,25 @@ M.buf_line_map = {}
-- string - a string for the collapsed or expanded symbol name
-- table - a virt_text chunk that can be passed directly to
-- vim.api.nvim_buf_set_extmark() via the virt_text option.
function M.marshal_node(node)
local str = ""
local glyph
function M.marshal_node(node, final)
local glyph = ""
if node.expanded then
glyph = M.glyphs["expanded"]
if ct.active_icon_set ~= nil then
glyph = ct.active_icon_set.Expanded
else
glyph = M.glyphs["expanded"]
end
else
glyph = M.glyphs["collapsed"]
if ct.active_icon_set ~= nil then
glyph = ct.active_icon_set.Collapsed
else
glyph = M.glyphs["collapsed"]
end
end

-- prefer using workspace symbol details if available.
-- fallback to callhierarchy object details.
local name = ""
local kind = ""
local detail = ""
local name, kind, detail, str = "", "", "", ""
if node.symbol ~= nil then
name = node.symbol.name
kind = vim.lsp.protocol.SymbolKind[node.symbol.kind]
Expand All @@ -62,30 +73,43 @@ function M.marshal_node(node)
if relative then
detail = file
elseif node.call_hierarchy_item.detail ~= nil then
detail = node.symbol.detail
detail = node.call_hierarchy_item.detail
end
end

local icon = ""
if kind ~= "" then
if ct.active_icon_set ~= nil then
icon = ct.active_icon_set[kind]
end
end

-- add spacing up to node's depth
for _=1, node.depth do
str = str .. " "
-- compute guides or spaces dependent on configs.
if config.indent_guides then
for i=1, node.depth do
if final and i == node.depth and #node.children == 0 then
str = str .. M.glyphs.end_guide .. M.glyphs.space
elseif final and i == node.depth and #node.children > 0 then
str = str .. M.glyphs.t_guide .. M.glyphs.space
else
str = str .. M.glyphs.guide .. M.glyphs.space
end
end
else
for _=1, node.depth do
str = str .. M.glyphs.space
end
end

-- ▶ Func1
str = str .. glyph
str = str .. glyph .. M.glyphs.space

if ct.config.icons ~= "none" then
-- ▶  Func1
str = str .. " " .. icon .. " " .. name
str = str .. M.glyphs.space .. icon .. M.glyphs.space .. M.glyphs.space .. name
else
-- ▶ [Function] Func1
str = str .. " " .. "[" .. kind .. "]" .. " " .. M.glyphs.separator .. " " .. name
str = str .. M.glyphs.space .. "[" .. kind .. "]" .. M.glyphs.space .. M.glyphs.separator .. M.glyphs.space .. name
end

-- return detail as virtual text chunk.
Expand Down Expand Up @@ -121,24 +145,27 @@ end
-- begin.
--
-- tree : tree_handle - a handle to a the tree we are marshaling.
function M.marshal_tree(buf_handle, lines, node, tree, virtual_text_lines)
function M.marshal_tree(buf_handle, lines, node, tree, virtual_text_lines, final)
if node.depth == 0 then
if virtual_text_lines == nil then
virtual_text_lines = {}
end
virtual_text_lines = {}
-- create a new line mapping
M.buf_line_map[tree] = {}
end
local line, virtual_text = M.marshal_node(node)
local line, virtual_text = M.marshal_node(node, final)
table.insert(lines, line)
table.insert(virtual_text_lines, virtual_text)
M.buf_line_map[tree][#lines] = node

-- if we are an expanded node or we are the root (always expand)
-- recurse
if node.expanded or node.depth == 0 then
for _, child in ipairs(node.children) do
M.marshal_tree(buf_handle, lines, child, tree, virtual_text_lines)
for i, child in ipairs(node.children) do
if i == #node.children then
final = true
else
final = false
end
M.marshal_tree(buf_handle, lines, child, tree, virtual_text_lines, final)
end
end

Expand Down

0 comments on commit db91089

Please sign in to comment.