Skip to content

Commit

Permalink
tree: unique keys
Browse files Browse the repository at this point in the history
to get around #10 each node in the tree now has a unique key.

marshaling the tree is updated to cache the node at all given lines every
time write_tree is called.

marshaling a line is greatly simplified and just looks at the cached now
to understand which node is being selected.

closes #10

Signed-off-by: ldelossa <louis.delos@gmail.com>
  • Loading branch information
ldelossa committed Nov 19, 2021
1 parent 8827b06 commit 686f916
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
19 changes: 17 additions & 2 deletions lua/calltree/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@ function M.Node.new(name, depth, call_hierarchy_obj, kind, references)
kind=kind,
references=references
}
node.key = M.Node.keyify(node)
setmetatable(node, M.Node.mt)
return node
end

-- keyify creates a key for a node
--
-- node : tree.Node - the node a key is being created
-- for
--
-- returns:
-- string - the key
function M.Node.keyify(node)
local key = node.name .. ":"
.. node.call_hierarchy_obj.uri .. ":"
.. node.call_hierarchy_obj.range.start.line
return key
end

-- eq perfoms a recursive comparison
-- between the two roots and their children.
function M.Node.eq(a, b)
Expand Down Expand Up @@ -128,7 +143,7 @@ function M.add_node(parent, children)
-- lookup parent node in depth tree (faster then tree diving.)
local pNode = nil
for _, node in pairs(M.depth_table[parent.depth]) do
if node.name == parent.name then
if node.key == parent.key then
pNode = node
break
end
Expand Down Expand Up @@ -193,7 +208,7 @@ function M.remove_subtree(node, root)
local dt = M.depth_table[node.depth]
local nw_dt = {}
for _, dt_node in ipairs(dt) do
if dt_node.name ~= node.name then
if dt_node.key ~= node.key then
table.insert(nw_dt, dt_node)
end
end
Expand Down
26 changes: 12 additions & 14 deletions lua/calltree/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ end
-- position
M.collapse = function()
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand Down Expand Up @@ -154,8 +153,7 @@ end
-- expand will expand a symbol at the current cursor position
M.expand = function()
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand All @@ -177,8 +175,8 @@ end
-- the cursor, creating a calltree with the symbol
-- as root.
M.focus = function()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand Down Expand Up @@ -229,8 +227,8 @@ end
-- switch_direction will focus the symbol under the
-- cursor and then invert the call hierarchy direction.
M.switch_direction = function()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand All @@ -253,8 +251,8 @@ end
-- jump will jump to the source code location of the
-- symbol under the cursor.
M.jump = function()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand All @@ -276,8 +274,8 @@ end
-- under the cursor.
M.hover = function()
ui_buf.close_all_popups()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand All @@ -297,8 +295,8 @@ end
-- showing more information.
M.details = function()
ui_buf.close_all_popups()
local line = vim.api.nvim_get_current_line()
local node = marshal.marshal_line(line)
local linenr = vim.api.nvim_win_get_cursor(M.win_handle)
local node = marshal.marshal_line(linenr)
if node == nil then
return
end
Expand Down
37 changes: 14 additions & 23 deletions lua/calltree/ui/marshal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ M.glyphs = {
separator = ""
}

-- buf_line_map keeps a mapping between marshaled
-- buffer lines and the node objects
M.buf_line_map = {}

-- marshal_node takes a node and marshals
-- it into a UI buffer line.
--
Expand Down Expand Up @@ -60,32 +64,14 @@ end
-- marshal_line takes a UI buffer line and
-- marshals it into a tree.Node.
--
-- line : string - the UI buffer line representing
-- the node to marshal.
-- linenr : {row,col} - the UI buffer line typically returned by
-- vim.api.nvim_win_get_cursor(calltree_win_handle)
--
-- returns:
-- tree.Node - the marshaled tree.Node table.
function M.marshal_line(line)
-- number of characters up to the expand symbol encode
-- the node's tree depth.
local depth = vim.fn.match(line, "[▼▶]")
if depth == -1 then
vim.api.nvim_err_writeln("failed to find matching character: " .. depth)
return
end

-- just your normal string parsing to carve out the symbol portion
-- of the line.
local symbol_and_type = vim.fn.strcharpart(line, depth+2)
local symbol_end_idx = vim.fn.stridx(symbol_and_type, "[")
local symbol = vim.fn.strpart(symbol_and_type, 0, symbol_end_idx)

for _, node in ipairs(tree.depth_table[depth]) do
if node.name == symbol then
return node
end
end
return nil
function M.marshal_line(linenr)
local node = M.buf_line_map[linenr[1]]
return node
end

-- marshal_tree recursively marshals all nodes from the provided root
Expand All @@ -100,7 +86,12 @@ end
-- node : tree.Node - the root node of the tree where marshaling will
-- begin.
function M.marshal_tree(buf_handle, lines, node)
if node.depth == 0 then
-- create a new line mapping
buf_line_map = {}
end
table.insert(lines, M.marshal_node(node))
M.buf_line_map[#lines] = node

-- if we are an expanded node or we are the root (always expand)
-- recurse
Expand Down

0 comments on commit 686f916

Please sign in to comment.