Skip to content

Commit

Permalink
ui: details popups
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ldelossa committed Nov 19, 2021
1 parent 3dcf837 commit 0a3b32e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
6 changes: 5 additions & 1 deletion doc/calltree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ Calltree exports several commands for manipulating the calltree UI.
:CTHover
Show hover info for the symbol

*CTDetails*
:CTDetails
Show symbol detail information in a popup.

*:CTClearHL*
:CTClearHL
Clears any highlights after jumping to location.
Expand All @@ -114,7 +118,6 @@ Calltree exports several commands for manipulating the calltree UI.
is closed.

*:CTDumpTree*

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

Expand Down
1 change: 1 addition & 0 deletions lua/calltree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function M.setup(user_config)
vim.cmd("command! CTFocus lua require('calltree.ui').focus()")
vim.cmd("command! CTJump lua require('calltree.ui').jump()")
vim.cmd("command! CTHover lua require('calltree.ui').hover()")
vim.cmd("command! CTDetails lua require('calltree.ui').details()")
vim.cmd("command! CTClearHL lua require('calltree.ui.jumps').set_jump_hl(false)")
vim.cmd("command! CTDumpTree lua require('calltree.tree').dump_tree()")
end
Expand Down
12 changes: 12 additions & 0 deletions lua/calltree/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local ui_win = require('calltree.ui.window')
local help_buf = require('calltree.ui.help_buffer')
local marshal = require('calltree.ui.marshal')
local jumps = require('calltree.ui.jumps')
local deets = require('calltree.ui.details')

local M = {}

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

-- details opens a popup window for the given symbol
-- showing more information.
M.details = function()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
if node == nil then
return
end
deets.details_popup(node, M.direction)
end

return M
3 changes: 3 additions & 0 deletions lua/calltree/ui/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function M._setup_buffer(direction, buffer_handle)

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

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

Expand Down
81 changes: 81 additions & 0 deletions lua/calltree/ui/details.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local lsp_util = require('calltree.lsp.util')

local M = {}

local direction_map = {
from = "Incoming Calls: ",
to = "Outgoing Calls: "
}

local float_win = nil

-- close_details_popups closes the created popup window
-- if it exists.
function M.close_details_popup()
if float_win ~= nil and
vim.api.nvim_win_is_valid(float_win) then
vim.api.nvim_win_close(float_win, true)
float_win = nil
end
end

-- details_popup creates a popup window showing futher details
-- about a symbol.
--
-- node : tree.Node - the node to show details for
--
-- direction : string - the current direction of the call tree
-- must be "to" or "from"
--
-- calltree_buffer : buffer_handle - the buffer_handle for the
-- current calltree.
function M.details_popup(node, direction, calltree_buffer)
local buf = vim.api.nvim_create_buf(false, false)
if buf == 0 then
vim.api.nvim_err_writeln("details_popup: could not create details buffer")
return
end
vim.api.nvim_buf_set_option(buf, 'bufhidden', 'delete')
vim.api.nvim_buf_set_option(buf, 'syntax', 'yaml')

local lines = {}
table.insert(lines, "Symbol Details")
table.insert(lines, "==============")
table.insert(lines, "Name: " .. node.name)
table.insert(lines, "Kind: " .. vim.lsp.protocol.SymbolKind[node.kind])

if node.expanded then
table.insert(lines, direction_map[direction] .. #node.children)
end

if node.references ~= nil then
table.insert(lines, "References: " .. #node.references)
end
table.insert(lines, "File: " .. lsp_util.relative_path_from_uri(node.call_hierarchy_obj.uri))
table.insert(lines, "Details: " .. node.call_hierarchy_obj.detail)

local width = 20
for _, line in ipairs(lines) do
local line_width = vim.fn.strdisplaywidth(line)
if line_width > width then
width = line_width
end
end

vim.api.nvim_buf_set_option(buf, 'modifiable', true)
vim.api.nvim_buf_set_lines(buf, 0, #lines, false, lines)
vim.api.nvim_buf_set_option(buf, 'modifiable', false)
local popup_conf = vim.lsp.util.make_floating_popup_options(
width,
#lines,
{
border= "rounded",
focusable= false,
zindex = 99,
}
)
float_win = vim.api.nvim_open_win(buf, false, popup_conf)
vim.api.nvim_win_set_option(float_win, 'winhighlight', 'NormalFloat:Normal')
end

return M
3 changes: 2 additions & 1 deletion lua/calltree/ui/help_buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function M._setup_help_buffer(help_buf_handle)
"f - focus the tree on this symbol",
"s - switch the symbol from",
" incoming/outgoing calls",
"h - show hover info for symbol"
"h - show hover info for symbol",
"d - show symbol details"
}
vim.api.nvim_buf_set_lines(help_buf_handle, 0, #lines, false, lines)
end
Expand Down

0 comments on commit 0a3b32e

Please sign in to comment.