Skip to content

Commit

Permalink
ui: add highlighting
Browse files Browse the repository at this point in the history
this commit implements highlighting for symbol and symbol references on
jump.

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Nov 18, 2021
1 parent b974d82 commit db980e9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lua/calltree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ M.config = {
layout = "left",
layout_size = 30,
jump_mode = "invoking",
icons = "none"
icons = "none",
symbol_hl = "Search",
symbol_refs_hl = "Search"
}

-- the configured icon set after setup() is ran.
Expand Down Expand Up @@ -115,6 +117,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! CTClearHL lua require('calltree.ui').clear_jump_hl()")
vim.cmd("command! CTDumpTree lua require('calltree.tree').dump_tree()")
end

Expand Down
3 changes: 2 additions & 1 deletion lua/calltree/lsp/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ M.ch_lsp_handler = function(direction)
call_hierarchy_call[direction].name,
0, -- tree.add_node will set the depth correctly.
call_hierarchy_call[direction],
call_hierarchy_call[direction].kind
call_hierarchy_call[direction].kind,
call_hierarchy_call.fromRanges
)
table.insert(children, child)
end
Expand Down
7 changes: 5 additions & 2 deletions lua/calltree/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ M.Node.mt = {
-- call hierarchy object per the LSP spec.
--
-- kind : string - the kind of symbol this node represents.
function M.Node.new(name, depth, call_hierarchy_obj, kind)
--
-- references : array of LSP ranges
function M.Node.new(name, depth, call_hierarchy_obj, kind, references)
local node = {
name=name,
depth=depth,
children={},
expanded=false,
call_hierarchy_obj=call_hierarchy_obj,
kind=kind
kind=kind,
references=references
}
setmetatable(node, M.Node.mt)
return node
Expand Down
52 changes: 50 additions & 2 deletions lua/calltree/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ M.direction = nil
M.invoking_win_handle = nil
-- the buffer switched to when the user asks for help
M.help_buffer_handle = nil
-- the current sighlight source, reset on jumps
M.jump_higlight_ns = vim.api.nvim_create_namespace("calltree-jump")
M.last_highlighted_buffer = nil

-- idempotent creation and configuration of call tree's ui buffer
local function _setup_buffer()
Expand Down Expand Up @@ -57,6 +60,9 @@ local function _setup_buffer()
vim.api.nvim_buf_set_option(M.buffer_handle, 'modifiable', false)
vim.api.nvim_buf_set_option(M.buffer_handle, 'swapfile', false)

-- au to clear highlights on window close
vim.cmd("au BufWinLeave <buffer=" .. M.buffer_handle .. "> lua require('calltree.ui').clear_jump_hl()")

-- set buffer local keymaps
local opts = {silent=true}
vim.api.nvim_buf_set_keymap(M.buffer_handle, "n", "zo", ":CTExpand<CR>", opts)
Expand Down Expand Up @@ -366,7 +372,8 @@ M.ch_switch_handler = function(direction)
call_hierarchy_call[direction].name,
0, -- tree.add_node will set the depth correctly.
call_hierarchy_call[direction],
call_hierarchy_call[direction].kind
call_hierarchy_call[direction].kind,
call_hierarchy_call.fromRanges
)
table.insert(children, child)
end
Expand All @@ -379,10 +386,38 @@ M.ch_switch_handler = function(direction)
end
end

local function set_jump_hl(node)
M.last_highlighted_buffer = vim.api.nvim_get_current_buf()
-- set highlght for function itself
local range = node.call_hierarchy_obj.range
vim.api.nvim_buf_add_highlight(
M.last_highlighted_buffer,
M.jump_higlight_ns,
ct.config.symbol_hl,
range["start"].line,
range["start"].character,
range["end"].character
)
-- apply it to all the references
if node.references ~= nil then
for _, ref in ipairs(node.references) do
vim.api.nvim_buf_add_highlight(
M.last_highlighted_buffer,
M.jump_higlight_ns,
ct.config.symbol_refs_hl,
ref["start"].line,
ref["start"].character,
ref["end"].character
)
end
end
end

-- jump will jump to the source code location of the
-- symbol under the cursor.
M.jump = function()
M.clear_jump_hl()

local line = vim.api.nvim_get_current_line()
local node = M.decode_line_to_node(line)
if node == nil then
Expand Down Expand Up @@ -425,6 +460,18 @@ M.jump = function()

::jump::
vim.lsp.util.jump_to_location(location)
set_jump_hl(node)
end

M.clear_jump_hl = function()
if M.last_highlighted_buffer ~= nil then
vim.api.nvim_buf_clear_namespace(
M.last_highlighted_buffer,
M.jump_higlight_ns,
0,
-1
)
end
end

-- hover will show LSP hover information for the symbol
Expand Down Expand Up @@ -469,7 +516,8 @@ M.ch_expand_handler = function(node, linenr, direction)
call_hierarchy_call[direction].name,
0, -- tree.add_node will compute depth for us
call_hierarchy_call[direction],
call_hierarchy_call[direction].kind
call_hierarchy_call[direction].kind,
call_hierarchy_call.fromRanges
)
table.insert(children, child)
end
Expand Down

0 comments on commit db980e9

Please sign in to comment.