Skip to content

Commit c3d2d8c

Browse files
committed
v0.0.1: initial implementation
this commit is the v0.0.1 initial implementation of calltree. Signed-off-by: ldelossa <louis.delos@gmail.com>
0 parents  commit c3d2d8c

File tree

7 files changed

+1058
-0
lines changed

7 files changed

+1058
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
notes.md

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
```
2+
/_____/\ /_______/\ /_/\ /_/\ /________/\/_____/\ /_____/\ /_____/\
3+
\:::__\/ \::: _ \ \\:\ \ \:\ \ \__.::.__\/\:::_ \ \ \::::_\/_\::::_\/_
4+
\:\ \ __\::(_) \ \\:\ \ \:\ \ \::\ \ \:(_) ) )_\:\/___/\\:\/___/\
5+
\:\ \/_/\\:: __ \ \\:\ \____\:\ \____\::\ \ \: __ `\ \\::___\/_\::___\/_
6+
\:\_\ \ \\:.\ \ \ \\:\/___/\\:\/___/\\::\ \ \ \ `\ \ \\:\____/\\:\____/\
7+
\_____\/ \__\/\__\/ \_____\/ \_____\/ \__\/ \_\/ \_\/ \_____\/ \_____\/
8+
9+
====================================================================================
10+
Neovim's missing call-hierarchy UI
11+
```
12+
13+
# Calltree
14+
15+
Calltree implements the missing "call-hierarchy" tree UI seen in other popular IDE's
16+
such as Pycharm and VSCode.
17+
18+
Calltree allows you to start at a root symbol and discover the callers or callees of it.
19+
20+
Subsequently, you can drill down the tree futher to discover the "callers-of-caller" or
21+
the "callees-of-callee".
22+
23+
This relationship forms a tree and this is exactly how Calltree works, keeping an in
24+
memory representation of the call tree and writing the tree out in outline form when
25+
requested.
26+
27+
# Usage
28+
29+
Usage is similar to any other lua plugin.
30+
31+
# Get it
32+
33+
Plug:
34+
```
35+
Plug 'ldelossa/calltree.nvim'
36+
```
37+
38+
# Set it
39+
40+
Call the setup function from anywhere you configure your plugins from.
41+
42+
Configuration dictionary is explained in ./doc/calltree.txt (:h calltree-config)
43+
```
44+
require('calltree').setup({})
45+
```
46+
47+
# Use it
48+
49+
The setup function hooks directly into the "textDocument/incomingCalls" and "textDocument/outgoingCalls"
50+
LSP handlers.
51+
52+
To start a call tree use the LSP client just like you're used to:
53+
54+
```
55+
:lua vim.lsp.buf.incoming_calls
56+
:lua vim.lsp.buf.outgoing_calls
57+
```
58+
59+
You most likely have key mappings set for this if you're using the lsp-config.
60+
61+
Once the calltree is open you can expand and collapse symbols to discover a total call
62+
hierarchy in an intuitative way.
63+
64+
Use ":CTExpand" and ":CTCollapse" to achieve this.
65+
66+
See (:h calltree-commands) for a list of commands.
67+
68+
# Features
69+
70+
This plugin aims to be super simple and do one thing very well.
71+
72+
There are a few features which add a bit more to the basic calltree usage.
73+
74+
## Switching Directions
75+
76+
The ":CTSwitch" command will focus and inverse the call tree for the symbol under the curosor.
77+
78+
## Focusing
79+
80+
The ":CTFocus" command will reparent the symbol under the cursor, making it root.
81+
82+
From there you can continue down the call tree.
83+
84+
## Hover
85+
86+
The ":CTHover" will show hover info for the given symbol.
87+
88+
## Jump
89+
90+
Calltree supports jumping to the symbol.
91+
92+
The ":CTJump" command will do this.
93+
94+
How jumping occurs is controlled by the config, see (h: calltree-config)
95+
96+
## Icons
97+
98+
Nerd font icons along with codicons are currently supported.
99+
100+
You'll need a patched font for them to work correctly. see (h: calltree-config)
101+
102+
## Demo
103+
104+
[![Calltree Demonstration]()](https://user-images.githubusercontent.com/5642902/142290929-15082e72-3848-4af9-8bbf-8c7b2424b299.mp4)

doc/calltree.txt

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
*calltree.txt* Calltree
2+
*calltree.nvim*
3+
4+
Author: Louis DeLosSantos <louis.delos@gmail.com>
5+
Homepage: <https://github.com/ldelossa/calltree.nvim>
6+
License: MIT license
7+
8+
______ ________ __ __ _________ ______ ______ ______
9+
/_____/\ /_______/\ /_/\ /_/\ /________/\/_____/\ /_____/\ /_____/\
10+
\:::__\/ \::: _ \ \\:\ \ \:\ \ \__.::.__\/\:::_ \ \ \::::_\/_\::::_\/_
11+
\:\ \ __\::(_) \ \\:\ \ \:\ \ \::\ \ \:(_) ) )_\:\/___/\\:\/___/\
12+
\:\ \/_/\\:: __ \ \\:\ \____\:\ \____\::\ \ \: __ `\ \\::___\/_\::___\/_
13+
\:\_\ \ \\:.\ \ \ \\:\/___/\\:\/___/\\::\ \ \ \ `\ \ \\:\____/\\:\____/\
14+
\_____\/ \__\/\__\/ \_____\/ \_____\/ \__\/ \_\/ \_\/ \_____\/ \_____\/
15+
16+
====================================================================================
17+
CONTENTS *calltree-contents*
18+
19+
1. Intro........................................|calltree-intro|
20+
2. Usage........................................|caltree-usage|
21+
3. Commands.....................................|calltree-commands|
22+
3. Config.......................................|calltree-config|
23+
24+
====================================================================================
25+
INTRODUCTION *calltree-intro*
26+
27+
Calltree is a simple plugin which adds the missing "call-hierarchy" tool
28+
found in IDEs such as Pycharm and VSCode.
29+
30+
A "call-hierarchy" tool creates a tree view of incoming (callers) or outging
31+
(callees) for a given symbol.
32+
33+
From there you can drive down further, discovering "callers-of-callers" or
34+
"callees-of-callees", and producing a tree view.
35+
36+
====================================================================================
37+
Usage *calltree-usage*
38+
39+
Calltree's entry point is the "calltree.lsp.handlers.ch_lsp_handler" function.
40+
41+
This function returns a handler suitable for usage in neovim's LSP handler
42+
dicationary.
43+
44+
While the setup function will hijack the handlers dict for you, you can also do this
45+
manually like so:
46+
47+
vim.lsp.handlers['callHierarchy/incomingCalls'] = vim.lsp.with(
48+
require('calltree.lsp.handlers').ch_lsp_handler("from"), {}
49+
)
50+
vim.lsp.handlers['callHierarchy/outgoingCalls'] = vim.lsp.with(
51+
require('calltree.lsp.handlers').ch_lsp_handler("to"), {}
52+
)
53+
54+
This is what the "callsigns.setup()" function does.
55+
56+
Once the calltree handlers are in place issuing the normal "vim.lsp.buf.incoming_calls"
57+
and "vim.lsp.buf.outgoing_calls" will open the calltree window.
58+
59+
From there check out *calltree-commands* to manipulate the calltree UI.
60+
61+
====================================================================================
62+
COMMANDS *calltree-commands*
63+
64+
Calltree exports several commands for manipulating the calltree UI.
65+
66+
*:CTOpen*
67+
:CTOpen
68+
Open the calltree window with the most recent call tree present.
69+
70+
*:CTClose*
71+
:CTClose
72+
Closes the calltree window
73+
74+
75+
*:CTExpand*
76+
:CTExpand
77+
Expands a calltree symbol resolving any callers/callees of the symbol under
78+
the cursor.
79+
80+
*:CTCollapse*
81+
:CTCollapse
82+
Collapses a calltree symbol and its subtree.
83+
84+
*:CTFocus*
85+
:CTFocus
86+
Create a new calltree with the symbol under the cursor as root.
87+
88+
*:CTJump*
89+
:CTJump
90+
Jump to the symbol's definition.
91+
How this jump is done is controlled via config.
92+
93+
*:CTHover*
94+
:CTHover
95+
Show hover info for the symbol
96+
97+
*:CTDumpTree*
98+
:CTDumpTree
99+
Echos the current calltree in lua dictonary syntax.
100+
Useful for debugging.
101+
102+
====================================================================================
103+
CONFIG *calltree-config*
104+
105+
To configure calltree you can pass the a config table to "require('calltree').setup(config)".
106+
You only need to specify the fields you wish to override.
107+
108+
The config table is described below:
109+
110+
M.config = {
111+
-- where the calltree ui will spawn
112+
-- "left" (default) - Spawn calltree as the left most vertical window.
113+
-- "right" - Spawn calltree as the left most vertical window.
114+
layout = "left",
115+
-- the initial size of the calltree ui
116+
-- int - An integer used in the initial "resize" command for
117+
-- the ui.
118+
layout_size = 30,
119+
-- the method used when jumping to a symbol in the calltree
120+
-- "invoking" - Jumping to a symbol will use the window which the calltree
121+
-- was initially invoked from.
122+
-- If the invoking window has been removed a new botright split
123+
-- will be created and take on the invoking window identity from there.
124+
-- "neighbor" - Jumping to a symbol will use the neighbor on the opposite side of
125+
-- the call tree.
126+
-- If no neighbor exists a split will be created for the jump.
127+
jump_mode = "invoking",
128+
-- the icon to represent the symbol type
129+
-- "none" - No icons for symbol types will just use text
130+
-- "nerd" - Use Nerd font icon set for symbol types (requires patched font)
131+
-- "codicon" - Use VSCode codicon icon set for symbol types (requires patched font)
132+
icons = "none"
133+
}
134+
135+
136+

lua/calltree.lua

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
local M = {}
2+
3+
-- alot of these are yoinked from:
4+
-- https://github.com/onsails/lspkind-nvim/blob/master/lua/lspkind/init.lua
5+
M.nerd = {
6+
Text = "",
7+
Method = "",
8+
Function = "",
9+
Constructor = "",
10+
Field = "",
11+
Variable = "",
12+
Class = "",
13+
Interface = "",
14+
Module = "",
15+
Property = "",
16+
Unit = "",
17+
Value = "",
18+
Enum = "",
19+
Keyword = "",
20+
Snippet = "",
21+
Color = "",
22+
File = "",
23+
Reference = "",
24+
Folder = "",
25+
EnumMember = "",
26+
Constant = "",
27+
Struct = "",
28+
Event = "",
29+
Operator = "",
30+
TypeParameter = ""
31+
}
32+
33+
M.codicons = {
34+
Text = "",
35+
Method = "",
36+
Function = "",
37+
Constructor = "",
38+
Field = "",
39+
Variable = "",
40+
Class = "",
41+
Interface = "",
42+
Module = "",
43+
Property = "",
44+
Unit = "",
45+
Value = "",
46+
Enum = "",
47+
Keyword = "",
48+
Snippet = "",
49+
Color = "",
50+
File = "",
51+
Reference = "",
52+
Folder = "",
53+
EnumMember = "",
54+
Constant = "",
55+
Struct = "",
56+
Event = "",
57+
Operator = "",
58+
TypeParameter = "",
59+
}
60+
61+
-- config is explained via ":help calltree-config"
62+
M.config = {
63+
layout = "left",
64+
layout_size = 30,
65+
jump_mode = "invoking",
66+
icons = "none"
67+
}
68+
69+
-- the configured icon set after setup() is ran.
70+
M.active_icon_set = {}
71+
72+
function M.setup(user_config)
73+
-- hijack the normal lsp handlers
74+
vim.lsp.handlers['callHierarchy/incomingCalls'] = vim.lsp.with(
75+
require('calltree.lsp.handlers').ch_lsp_handler("from"), {}
76+
)
77+
vim.lsp.handlers['callHierarchy/outgoingCalls'] = vim.lsp.with(
78+
require('calltree.lsp.handlers').ch_lsp_handler("to"), {}
79+
)
80+
81+
-- merge config
82+
if user_config ~= nil then
83+
for k, v in pairs(user_config) do
84+
M.config[k] = v
85+
end
86+
end
87+
88+
-- sanatize the config
89+
if (M.config.layout ~= "left") and (M.config.layout ~= "left") then
90+
M.config.layout = "left"
91+
end
92+
if M.config.layout_size < 10 then
93+
M.config.layout_size = 10
94+
end
95+
if M.config.jump_mode ~= "invoking" and M.config.jump_mode ~= "neighbor" then
96+
M.config.jump_mode = "neighbor"
97+
end
98+
if M.config.icons ~= "codicons" and M.config.icons ~= "nerd" then
99+
M.config.icons = "none"
100+
else
101+
if M.config.icons == "codicons" then
102+
M.active_icon_set = M.codicons
103+
end
104+
if M.config.icons == "nerd" then
105+
M.active_icon_set = M.nerd
106+
end
107+
end
108+
109+
-- setup commands
110+
vim.cmd("command! CTOpen lua require('calltree.ui').open()")
111+
vim.cmd("command! CTClose lua require('calltree.ui').close()")
112+
vim.cmd("command! CTExpand lua require('calltree.ui').expand()")
113+
vim.cmd("command! CTCollapse lua require('calltree.ui').collapse()")
114+
vim.cmd("command! CTSwitch lua require('calltree.ui').switch_direction()")
115+
vim.cmd("command! CTFocus lua require('calltree.ui').focus()")
116+
vim.cmd("command! CTJump lua require('calltree.ui').jump()")
117+
vim.cmd("command! CTHover lua require('calltree.ui').hover()")
118+
vim.cmd("command! CTDumpTree lua require('calltree.tree').dump_tree()")
119+
end
120+
121+
return M

0 commit comments

Comments
 (0)