Skip to content

Commit 0a3b32e

Browse files
committed
ui: details popups
this commit adds a popup menu over a symbol providing more information. this is useful if you enjoy a slim side bar but still want to get more details on the symbol. Signed-off-by: ldelossa <louis.delos@gmail.com>
1 parent 3dcf837 commit 0a3b32e

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

doc/calltree.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ Calltree exports several commands for manipulating the calltree UI.
106106
:CTHover
107107
Show hover info for the symbol
108108

109+
*CTDetails*
110+
:CTDetails
111+
Show symbol detail information in a popup.
112+
109113
*:CTClearHL*
110114
:CTClearHL
111115
Clears any highlights after jumping to location.
@@ -114,7 +118,6 @@ Calltree exports several commands for manipulating the calltree UI.
114118
is closed.
115119

116120
*:CTDumpTree*
117-
118121
:CTDumpTree
119122
Echos the current calltree in lua dictonary syntax.
120123
Useful for debugging.
@@ -133,6 +136,7 @@ The default buffer mapping is set via lua and should feel familiar if you use vi
133136
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<CR>", ":CTJump<CR>", opts)
134137
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "f", ":CTFocus<CR>", opts)
135138
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "i", ":CTHover<CR>", opts)
139+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "d", ":CTDetails<CR>", opts)
136140
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "s", ":CTSwitch<CR>", opts)
137141
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "?", ":lua require('calltree.ui').help(true)<CR>", opts)
138142

lua/calltree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function M.setup(user_config)
121121
vim.cmd("command! CTFocus lua require('calltree.ui').focus()")
122122
vim.cmd("command! CTJump lua require('calltree.ui').jump()")
123123
vim.cmd("command! CTHover lua require('calltree.ui').hover()")
124+
vim.cmd("command! CTDetails lua require('calltree.ui').details()")
124125
vim.cmd("command! CTClearHL lua require('calltree.ui.jumps').set_jump_hl(false)")
125126
vim.cmd("command! CTDumpTree lua require('calltree.tree').dump_tree()")
126127
end

lua/calltree/ui.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local ui_win = require('calltree.ui.window')
66
local help_buf = require('calltree.ui.help_buffer')
77
local marshal = require('calltree.ui.marshal')
88
local jumps = require('calltree.ui.jumps')
9+
local deets = require('calltree.ui.details')
910

1011
local M = {}
1112

@@ -290,4 +291,15 @@ M.hover = function()
290291
lsp_util.multi_client_request(M.active_lsp_clients, "textDocument/hover", params, nil, M.buffer_handle)
291292
end
292293

294+
-- details opens a popup window for the given symbol
295+
-- showing more information.
296+
M.details = function()
297+
local line = vim.api.nvim_get_current_line()
298+
local node = marshal.marshal_line(line)
299+
if node == nil then
300+
return
301+
end
302+
deets.details_popup(node, M.direction)
303+
end
304+
293305
return M

lua/calltree/ui/buffer.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function M._setup_buffer(direction, buffer_handle)
4444

4545
-- au to clear highlights on window close
4646
vim.cmd("au BufWinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui.jumps').set_jump_hl(false)")
47+
-- au to close popup with cursor moves or buffer is closed.
48+
vim.cmd("au CursorMoved,BufWinLeave,WinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui.details').close_details_popup()")
4749

4850
-- set buffer local keymaps
4951
local opts = {silent=true}
@@ -52,6 +54,7 @@ function M._setup_buffer(direction, buffer_handle)
5254
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<CR>", ":CTJump<CR>", opts)
5355
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "f", ":CTFocus<CR>", opts)
5456
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "i", ":CTHover<CR>", opts)
57+
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "d", ":CTDetails<CR>", opts)
5558
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "s", ":CTSwitch<CR>", opts)
5659
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "?", ":lua require('calltree.ui').help(true)<CR>", opts)
5760

lua/calltree/ui/details.lua

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
local lsp_util = require('calltree.lsp.util')
2+
3+
local M = {}
4+
5+
local direction_map = {
6+
from = "Incoming Calls: ",
7+
to = "Outgoing Calls: "
8+
}
9+
10+
local float_win = nil
11+
12+
-- close_details_popups closes the created popup window
13+
-- if it exists.
14+
function M.close_details_popup()
15+
if float_win ~= nil and
16+
vim.api.nvim_win_is_valid(float_win) then
17+
vim.api.nvim_win_close(float_win, true)
18+
float_win = nil
19+
end
20+
end
21+
22+
-- details_popup creates a popup window showing futher details
23+
-- about a symbol.
24+
--
25+
-- node : tree.Node - the node to show details for
26+
--
27+
-- direction : string - the current direction of the call tree
28+
-- must be "to" or "from"
29+
--
30+
-- calltree_buffer : buffer_handle - the buffer_handle for the
31+
-- current calltree.
32+
function M.details_popup(node, direction, calltree_buffer)
33+
local buf = vim.api.nvim_create_buf(false, false)
34+
if buf == 0 then
35+
vim.api.nvim_err_writeln("details_popup: could not create details buffer")
36+
return
37+
end
38+
vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete')
39+
vim.api.nvim_buf_set_option(buf, 'syntax', 'yaml')
40+
41+
local lines = {}
42+
table.insert(lines, "Symbol Details")
43+
table.insert(lines, "==============")
44+
table.insert(lines, "Name: " .. node.name)
45+
table.insert(lines, "Kind: " .. vim.lsp.protocol.SymbolKind[node.kind])
46+
47+
if node.expanded then
48+
table.insert(lines, direction_map[direction] .. #node.children)
49+
end
50+
51+
if node.references ~= nil then
52+
table.insert(lines, "References: " .. #node.references)
53+
end
54+
table.insert(lines, "File: " .. lsp_util.relative_path_from_uri(node.call_hierarchy_obj.uri))
55+
table.insert(lines, "Details: " .. node.call_hierarchy_obj.detail)
56+
57+
local width = 20
58+
for _, line in ipairs(lines) do
59+
local line_width = vim.fn.strdisplaywidth(line)
60+
if line_width > width then
61+
width = line_width
62+
end
63+
end
64+
65+
vim.api.nvim_buf_set_option(buf, 'modifiable', true)
66+
vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines)
67+
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
68+
local popup_conf = vim.lsp.util.make_floating_popup_options(
69+
width,
70+
#lines,
71+
{
72+
border= "rounded",
73+
focusable= false,
74+
zindex = 99,
75+
}
76+
)
77+
float_win = vim.api.nvim_open_win(buf, false, popup_conf)
78+
vim.api.nvim_win_set_option(float_win, 'winhighlight', 'NormalFloat:Normal')
79+
end
80+
81+
return M

lua/calltree/ui/help_buffer.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function M._setup_help_buffer(help_buf_handle)
3030
"f - focus the tree on this symbol",
3131
"s - switch the symbol from",
3232
" incoming/outgoing calls",
33-
"h - show hover info for symbol"
33+
"h - show hover info for symbol",
34+
"d - show symbol details"
3435
}
3536
vim.api.nvim_buf_set_lines(help_buf_handle, 0, #lines, false, lines)
3637
end

0 commit comments

Comments
 (0)