Skip to content

Commit 3dcf837

Browse files
committed
ui: introduce horizontal layouts
this commit introduces two new layout options: "top", "bottom". when utilizing these layout options more space is available for details about the symbol. we now use this space to show the file the symbol is defined in. a few UI tweaks are made as well for aesthetics. closes #6 Signed-off-by: ldelossa <louis.delos@gmail.com>
1 parent 4a1eb95 commit 3dcf837

File tree

7 files changed

+80
-20
lines changed

7 files changed

+80
-20
lines changed

doc/calltree.txt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ This does not stop you from mapping *calltree-commands* to your own liking.
128128

129129
The default buffer mapping is set via lua and should feel familiar if you use vim folds.
130130

131-
local opts = {silent=true}
132-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "zo", ":CTExpand<CR>", opts)
133-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "zc", ":CTCollapse<CR>", opts)
134-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "<CR>", ":CTJump<CR>", opts)
135-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "f", ":CTFocus<CR>", opts)
136-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "i", ":CTHover<CR>", opts)
137-
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "s", ":CTSwitch<CR>", opts)
131+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "zo", ":CTExpand<CR>", opts)
132+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "zc", ":CTCollapse<CR>", opts)
133+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<CR>", ":CTJump<CR>", opts)
134+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "f", ":CTFocus<CR>", opts)
135+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "i", ":CTHover<CR>", opts)
136+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "s", ":CTSwitch<CR>", opts)
137+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "?", ":lua require('calltree.ui').help(true)<CR>", opts)
138138

139139
====================================================================================
140140
CONFIG *calltree-config*
@@ -148,6 +148,12 @@ The config table is described below:
148148
-- where the calltree ui will spawn
149149
-- "left" (default) - Spawn calltree as the left most vertical window.
150150
-- "right" - Spawn calltree as the left most vertical window.
151+
-- "top" - Spawn calltree as the bottom most horizontal window
152+
-- more details about the symbol will be shown with this
153+
-- layout.
154+
-- "bottom" - Spawn calltree as the top most horizontal window
155+
-- more details about the symbol will be shown with this
156+
-- layout.
151157
layout = "left",
152158
-- the initial size of the calltree ui
153159
-- int - An integer used in the initial "resize" command for

lua/calltree.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ function M.setup(user_config)
8888
end
8989

9090
-- sanatize the config
91-
if (M.config.layout ~= "left") and (M.config.layout ~= "left") then
91+
if (M.config.layout ~= "left")
92+
and (M.config.layout ~= "right")
93+
and (M.config.layout ~= "top")
94+
and (M.config.layout ~= "bottom")
95+
then
9296
M.config.layout = "left"
9397
end
9498
if M.config.layout_size < 10 then

lua/calltree/lsp/util.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ M.multi_client_request = function(clients, method, params, handler, bufnr)
1010
end
1111
end
1212

13+
function M.relative_path_from_uri(uri)
14+
local cwd = vim.fn.getcwd()
15+
local uri_path = vim.fn.substitute(uri, "file://", "", "")
16+
local idx = vim.fn.stridx(uri_path, cwd)
17+
if idx == -1 then
18+
-- we can't resolve a relative path, just give the
19+
-- full path to the file.
20+
return uri_path, false
21+
end
22+
return vim.fn.substitute(uri_path, cwd .. "/", "", ""), true
23+
end
24+
1325
return M

lua/calltree/ui/buffer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function M._setup_buffer(direction, buffer_handle)
5151
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "zc", ":CTCollapse<CR>", opts)
5252
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<CR>", ":CTJump<CR>", opts)
5353
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "f", ":CTFocus<CR>", opts)
54-
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "h", ":CTHover<CR>", opts)
54+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "i", ":CTHover<CR>", opts)
5555
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "s", ":CTSwitch<CR>", opts)
5656
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "?", ":lua require('calltree.ui').help(true)<CR>", opts)
5757

lua/calltree/ui/jumps.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ function M.jump_neighbor(location, layout, node)
2323
local cur_win = vim.api.nvim_get_current_win()
2424
if layout == "left" then
2525
vim.cmd('wincmd l')
26-
else
26+
elseif layout == "right" then
2727
vim.cmd('wincmd h')
28+
elseif layout == "top" then
29+
vim.cmd('wincmd j')
30+
elseif layout == "bottom" then
31+
vim.cmd('wincmd k')
2832
end
2933
if cur_win == vim.api.nvim_get_current_win() then
3034
if layout == "left" then
3135
vim.cmd("botright vsplit")
32-
else
36+
elseif layout == "right" then
3337
vim.cmd("topleft vsplit")
38+
elseif layout == "top" then
39+
vim.cmd("botleft split")
40+
elseif layout == "bottom" then
41+
vim.cmd("botleft split")
3442
end
3543
end
3644
vim.lsp.util.jump_to_location(location)

lua/calltree/ui/marshal.lua

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
local tree = require('calltree.tree')
22
local ct = require('calltree')
3+
local lsp_util = require('calltree.lsp.util')
34

45
local M = {}
56

67
M.glyphs = {
78
expanded= "",
8-
collapsed= ""
9+
collapsed= "",
10+
separator = ""
911
}
1012

1113
-- marshal_node takes a node and marshals
@@ -32,12 +34,26 @@ function M.marshal_node(node)
3234
str = str .. " "
3335
end
3436

35-
str = str .. glyph .. " " .. node.name .. " "
37+
-- ▶ Func1
38+
str = str .. glyph .. " " .. node.name
3639
if ct.config.icons ~= "none" then
37-
str = str .. ct.active_icon_set[kind]
40+
-- ▶ Func1[]
41+
str = str .. "[" .. ct.active_icon_set[kind] .. "]" .. " "
3842
else
39-
str = str .. "" .. " " .. kind
43+
-- ▶ Func1 • [Function]
44+
str = str .. M.glyphs.separator .. " " .. "[" .. kind .. "]" .. " "
4045
end
46+
47+
if ct.config.layout == "bottom" or
48+
ct.config.layout == "top" then
49+
-- now we got all the room in the world, add detail
50+
path = lsp_util.relative_path_from_uri(node.call_hierarchy_obj.uri)
51+
-- ▶ Func1[] • relative/path/to/file
52+
-- or
53+
-- ▶ Func1 • [Function] • relative/path/to/file
54+
str = str .. M.glyphs.separator .. " " .. path
55+
end
56+
4157
return str
4258
end
4359

@@ -61,7 +77,7 @@ function M.marshal_line(line)
6177
-- just your normal string parsing to carve out the symbol portion
6278
-- of the line.
6379
local symbol_and_type = vim.fn.strcharpart(line, depth+2)
64-
local symbol_end_idx = vim.fn.stridx(symbol_and_type, " ")
80+
local symbol_end_idx = vim.fn.stridx(symbol_and_type, "[")
6581
local symbol = vim.fn.strpart(symbol_and_type, 0, symbol_end_idx)
6682

6783
for _, node in ipairs(tree.depth_table[depth]) do

lua/calltree/ui/window.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,27 @@ function M._setup_window(buf_handle, win_handle, win_tabpage, config)
3535

3636
if config.layout == "left" then
3737
vim.cmd("topleft vsplit")
38-
else
38+
vim.cmd("vertical resize " ..
39+
config.layout_size)
40+
elseif config.layout == "right" then
3941
vim.cmd("botright vsplit")
42+
vim.cmd("vertical resize " ..
43+
config.layout_size)
44+
elseif config.layout == "top" then
45+
vim.cmd("topleft split")
46+
vim.cmd("resize " ..
47+
config.layout_size)
48+
elseif config.layout == "bottom" then
49+
vim.cmd("botright split")
50+
vim.cmd("resize " ..
51+
config.layout_size)
52+
else
53+
-- we should never get here but what ever.
54+
vim.cmd("topleft vsplit")
55+
vim.cmd("vertical resize " ..
56+
config.layout_size)
4057
end
4158

42-
vim.cmd("vertical resize " ..
43-
config.layout_size)
44-
4559
win_handle = vim.api.nvim_get_current_win()
4660
win_tabpage = vim.api.nvim_win_get_tabpage(win_handle)
4761
vim.api.nvim_win_set_buf(win_handle, buf_handle)

0 commit comments

Comments
 (0)