Skip to content

Commit

Permalink
usability: various changes to help useability
Browse files Browse the repository at this point in the history
various changes which make the UI a little easier to use.

CTToggle is renamed to CTPanel.

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Dec 6, 2021
1 parent 5cfe069 commit 8b34809
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 57 deletions.
28 changes: 20 additions & 8 deletions doc/calltree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ A unified panel would feel similar to IDE's such as VSCode where you can "hide"
and "unhide" a persistent informational panel. Both the symbol tree and the call tree will
always be present in the toggled panel, unless you manually close a window.
Toggling the panel will recreate any closed windows. This functionality
is provided via the "CTToggle" command.
is provided via the "CTPanel" command.

If you'd rather use each individual component separately this is possible
too. Utilize the "CTOpen/CTClose" and "STOpen/STClose" commands. These commands
Expand All @@ -89,20 +89,25 @@ Calltree exports several commands for manipulating the calltree UI.
*:CTOpen*
:CTOpen
Open the calltree window with the most recent call tree present.
As a convenience, calling this again will jump back to the window
which opened the calltree.

*:CTClose*
:CTClose
Closes the calltree window.
*:STOpen*


*:CTToggle*
:CTToggle
*:CTPanel*
:CTPanel
Treats calltree as a persistent panel and toggles it open or close.
Using this command will always toggle both the symboltree and calltree
components as if they were a single unified panel.

*:STOpen*
:STOpen
Open the symboltree window with the most recent outline tree present.
As a convenience, calling this again will jump back to the window
which opened the symboltree.

*:STClose*
:STClose
Expand Down Expand Up @@ -155,8 +160,13 @@ Calltree exports several commands for manipulating the calltree UI.

*:CTDumpTree*
:CTDumpTree
Echos the current calltree in lua dictonary syntax.
Useful for debugging.
Only valid when inside a calltree window.
Dumps the tree data structure to a buffer for debugging.

*:CTDumpNode*
:CTDumpNode
Same as CTDumpTree but dumps the current node and it's children to
a buffer for debugging.

====================================================================================
MAPPINGS *calltree-mappings*
Expand Down Expand Up @@ -200,8 +210,6 @@ The config table is described below:
-- int - An integer used in the initial "resize" command for
-- the ui.
layout_size = 30,
-- whether to automatically open the calltree and symboltree ui on start.
auto_open = false,
-- the method used when jumping to a symbol in the calltree
-- "invoking" - Jumping to a symbol will use the window which the calltree
-- was initially invoked from.
Expand Down Expand Up @@ -245,6 +253,10 @@ The config table is described below:
-- if set to true calling LSP functions which trigger calltree UI
-- will open the unified panel. see *calltree-usage* for details.
unified_panel = false
-- whether to automatically open the unified ui on start.
-- unified_panel need not be true to open the panel
-- on start.
auto_open_panel = false,
-- automatic highlighting and window position updates when
-- navigating the symboltree UI. Set to false to disable.
auto_highlight = true
Expand Down
91 changes: 47 additions & 44 deletions lua/calltree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local M = {}
M.config = {
layout = "left",
layout_size = 30,
auto_open = false,
jump_mode = "invoking",
icons = "none",
no_hls = false,
Expand All @@ -13,6 +12,7 @@ M.config = {
hls = {},
resolve_symbols = true,
unified_panel = false,
auto_open_panel = false,
auto_highlight = true
}

Expand Down Expand Up @@ -74,6 +74,33 @@ function _setup_default_highlights()
end
end

local function sanatize_config()
-- sanatize the config
if (M.config.layout ~= "left")
and (M.config.layout ~= "right")
and (M.config.layout ~= "top")
and (M.config.layout ~= "bottom")
then
M.config.layout = "left"
end
if M.config.layout_size < 10 then
M.config.layout_size = 10
end
if M.config.jump_mode ~= "invoking" and M.config.jump_mode ~= "neighbor" then
M.config.jump_mode = "neighbor"
end
if M.config.icons ~= "codicons" and M.config.icons ~= "nerd" then
M.config.icons = "none"
else
if M.config.icons == "codicons" then
M.active_icon_set = M.codicons
end
if M.config.icons == "nerd" then
M.active_icon_set = M.nerd
end
end
end

function M.setup(user_config)
-- hijack the normal lsp handlers
vim.lsp.handlers['callHierarchy/incomingCalls'] = vim.lsp.with(
Expand Down Expand Up @@ -108,30 +135,7 @@ function M.setup(user_config)
end
end

-- sanatize the config
if (M.config.layout ~= "left")
and (M.config.layout ~= "right")
and (M.config.layout ~= "top")
and (M.config.layout ~= "bottom")
then
M.config.layout = "left"
end
if M.config.layout_size < 10 then
M.config.layout_size = 10
end
if M.config.jump_mode ~= "invoking" and M.config.jump_mode ~= "neighbor" then
M.config.jump_mode = "neighbor"
end
if M.config.icons ~= "codicons" and M.config.icons ~= "nerd" then
M.config.icons = "none"
else
if M.config.icons == "codicons" then
M.active_icon_set = M.codicons
end
if M.config.icons == "nerd" then
M.active_icon_set = M.nerd
end
end
sanatize_config()

-- setup default highlights
if not M.config.no_hls then
Expand All @@ -140,32 +144,31 @@ function M.setup(user_config)

-- automatically open the ui elements on buf enters.
if M.config.auto_open then
vim.cmd([[au BufEnter * lua require('calltree.ui').open_calltree()]])
vim.cmd([[au BufEnter * lua require('calltree.ui').open_symboltree()]])
vim.cmd([[au VimEnter * lua require('calltree.ui').toggle_panel()]])
end

-- will keep the outline view up to date when moving around buffers.
vim.cmd([[au TextChanged,BufEnter,BufWritePost * lua require('calltree.ui').refresh_symbol_tree()]])
vim.cmd([[au TextChanged,BufEnter,BufWritePost,WinEnter * lua require('calltree.ui').refresh_symbol_tree()]])

-- will enable symboltree ui tracking with source code lines.
vim.cmd([[au CursorHold * lua require('calltree.ui').source_tracking()]])

-- setup commands
vim.cmd("command! CTOpen lua require('calltree.ui').open_to('calltree')")
vim.cmd("command! STOpen lua require('calltree.ui').open_to('symboltree')")
vim.cmd("command! CTToggle lua require('calltree.ui').toggle_panel()")
vim.cmd("command! CTClose lua require('calltree.ui').close_calltree()")
vim.cmd("command! STClose lua require('calltree.ui').close_symboltree()")
vim.cmd("command! CTExpand lua require('calltree.ui').expand()")
vim.cmd("command! CTCollapse lua require('calltree.ui').collapse()")
vim.cmd("command! CTSwitch lua require('calltree.ui').switch_direction()")
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.ui').dump_tree()")
vim.cmd("command! CTDumpNode lua require('calltree.ui').dump_node()")
-- setup commands
vim.cmd("command! CTOpen lua require('calltree.ui').open_to('calltree')")
vim.cmd("command! STOpen lua require('calltree.ui').open_to('symboltree')")
vim.cmd("command! CTPanel lua require('calltree.ui').toggle_panel()")
vim.cmd("command! CTClose lua require('calltree.ui').close_calltree()")
vim.cmd("command! STClose lua require('calltree.ui').close_symboltree()")
vim.cmd("command! CTExpand lua require('calltree.ui').expand()")
vim.cmd("command! CTCollapse lua require('calltree.ui').collapse()")
vim.cmd("command! CTSwitch lua require('calltree.ui').switch_direction()")
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.ui').dump_tree()")
vim.cmd("command! CTDumpNode lua require('calltree.ui').dump_node()")
end

-- the configured icon set after setup() is ran.
Expand Down
4 changes: 2 additions & 2 deletions lua/calltree/lsp/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ M.ch_lsp_handler = function(direction)
if config.resolve_symbols then
lsp_util.gather_symbols_async(root, children, ui_state, function()
tree.add_node(ui_state.calltree_handle, root, children)
ui.open_to("calltree")
ui._open_calltree()
end)
return
end
tree.add_node(ui_state.calltree_handle, root, children)
ui.open_to("calltree")
ui._open_calltree()
end
end

Expand Down
4 changes: 3 additions & 1 deletion lua/calltree/ui/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ function M._setup_buffer(name, buffer_handle, tab)
vim.api.nvim_buf_set_option(buffer_handle, 'textwidth', 0)
vim.api.nvim_buf_set_option(buffer_handle, 'wrapmargin', 0)

-- au to clear highlights on window close
-- au to clear jump 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.buffer').close_all_popups()")

-- au to (re)set source code highlights when a symboltree node is hovered.
if config.auto_highlight then
vim.cmd("au BufWinLeave,WinLeave <buffer=" .. buffer_handle .. "> lua require('calltree.ui').auto_highlight(false)")
vim.cmd("au CursorHold <buffer=" .. buffer_handle .. "> lua require('calltree.ui').auto_highlight(true)")
Expand Down
31 changes: 29 additions & 2 deletions lua/calltree/ui/help_buffer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
local config = require('calltree').config
local M = {}

local function map_resize_keys(buffer_handle, opts)
local l = config.layout
if l == "top" or l == "bottom" then
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Right>", ":vert resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Left>", ":vert resize -5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Up>", ":resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Down>", ":resize -5<cr>", opts)
elseif l == "bottom" then
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Right>", ":vert resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Left>", ":vert resize -5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Down>", ":resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Up>", ":resize -5<cr>", opts)
elseif l == "left" then
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Up>", ":resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Down>", ":resize -5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Left>", ":vert resize -5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Right>", ":vert resize +5<cr>", opts)
elseif l == "right" then
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Up>", ":resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Down>", ":resize -5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Left>", ":vert resize +5<cr>", opts)
vim.api.nvim_buf_set_keymap(buffer_handle, "n", "<Right>", ":vert resize -5<cr>", opts)
end
end

-- _setup_help_buffer performs an idempotent creation
-- of the calltree help buffer
--
Expand All @@ -21,7 +47,7 @@ function M._setup_help_buffer(help_buf_handle)
help_buf_handle = buf
local lines = {
"CALLTREE HELP:",
"press 'c' to close",
"press '?' to close",
"",
"KEYMAP:",
"zo - expand a symbol",
Expand All @@ -46,7 +72,8 @@ function M._setup_help_buffer(help_buf_handle)

-- set buffer local keymaps
local opts = {silent=true}
vim.api.nvim_buf_set_keymap(help_buf_handle, "n", "c", ":lua require('calltree.ui').help(false)<CR>", opts)
vim.api.nvim_buf_set_keymap(help_buf_handle, "n", "?", ":lua require('calltree.ui').help(false)<CR>", opts)
map_resize_keys(help_buf_handle, opts)

return help_buf_handle
end
Expand Down
2 changes: 2 additions & 0 deletions lua/calltree/ui/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ end
-- which want to refresh the state of the UI but not
-- close the panels.
function M._toggle_panel(ui_state, keep_open)
local cur_win = vim.api.nvim_get_current_win()
local open = true
if not keep_open then
for _, win_name in pairs(type_to_ui_state_win) do
Expand All @@ -118,6 +119,7 @@ function M._toggle_panel(ui_state, keep_open)
-- the pannel open
M._open_window("calltree", ui_state)
M._open_window("symboltree", ui_state)
vim.api.nvim_set_current_win(cur_win)
end

-- setup_window evaluates the current layout and the desired layout
Expand Down

0 comments on commit 8b34809

Please sign in to comment.