Skip to content

Commit

Permalink
refactor: big ol' refactor
Browse files Browse the repository at this point in the history
things were getting messy, this commit restructures the code into a more
modular structure.

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Nov 18, 2021
1 parent 2e28baa commit 4a1eb95
Show file tree
Hide file tree
Showing 9 changed files with 536 additions and 389 deletions.
2 changes: 1 addition & 1 deletion lua/calltree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +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! CTClearHL lua require('calltree.ui.jumps').set_jump_hl(false)")
vim.cmd("command! CTDumpTree lua require('calltree.tree').dump_tree()")
end

Expand Down
4 changes: 1 addition & 3 deletions lua/calltree/lsp/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ M.ch_lsp_handler = function(direction)
table.insert(children, child)
end

-- add the new root, its children, and rewrite the
-- tree (will open the calltree ui if necessary).
tree.add_node(root, children)
ui.write_tree({}, tree.root_node)
ui.write_tree()
end
end

Expand Down
56 changes: 25 additions & 31 deletions lua/calltree/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ M.Node.mt = {
--
-- kind : string - the kind of symbol this node represents.
--
-- references : array of LSP ranges
-- references : array of references of the given symbol
function M.Node.new(name, depth, call_hierarchy_obj, kind, references)
local node = {
name=name,
Expand Down Expand Up @@ -76,6 +76,29 @@ M.depth_table = nil
-- the root of the current call tree.
M.root_node = nil

-- recursive_dpt_compute traverses the tree
-- and flattens it into out depth_table
--
-- node : Node - calltree's root node.
local function _recursive_dpt_compute(node)
local depth = node.depth
if M.depth_table[depth] == nil then
M.depth_table[depth] = {}
end
table.insert(M.depth_table[depth], node)
-- recurse
for _, child in ipairs(node.children) do
_recursive_dpt_compute(child)
end
end

-- _refresh_dpt dumps the current depth_table
-- and writes a new one.
local function _refresh_dpt()
M.depth_table = {}
_recursive_dpt_compute(M.root_node)
end

-- add_node adds the provided root and
-- its children to the tree.
--
Expand Down Expand Up @@ -117,41 +140,12 @@ function M.add_node(parent, children)
-- ditch the old child array, we are refreshing the children
pNode.children = {}

-- add children to parent node and update
-- depth_table.
if M.depth_table[parent.depth+1] == nil then
M.depth_table[parent.depth+1] = {}
end

local child_depth = parent.depth + 1
for _, child in ipairs(children) do
child.depth = child_depth
table.insert(pNode.children, child)
table.insert(M.depth_table[child.depth], child)
end
end

-- recursive_dpt_compute traverses the tree
-- and flattens it into out depth_table
--
-- node : Node - calltree's root node.
local function _recursive_dpt_compute(node)
local depth = node.depth
if M.depth_table[depth] == nil then
M.depth_table[depth] = {}
end
table.insert(M.depth_table[depth], node)
-- recurse
for _, child in ipairs(node.children) do
_recursive_dpt_compute(child)
end
end

-- _refresh_dpt dumps the current depth_table
-- and writes a new one.
local function _refresh_dpt()
M.depth_table = {}
_recursive_dpt_compute(M.root_node)
_refresh_dpt()
end

-- remove_node will remove all nodes associated
Expand Down
Loading

0 comments on commit 4a1eb95

Please sign in to comment.