Inspired by Sourcetrail and trails.nvim, this is an interactive ascii call graph plugin developed to assist in reading source code.
This is my first plugin. The plugin is still in a very early stage, and bugs are expected. Feedback is welcome.
It has only been tested and passed with the clangd server (since my main programming language is C++). In theory, it can be used with all LSP servers that support callHierarchy/incomingCalls.
- The pyright server has been tested and is usable, but its performance is poor, and it is highly not recommended.
2025-02-18.12.49.35.mp4
Demonstrated with the open-source project muduo:
Image Description
-
The naming format of each node is
func_name/file:line_numer
. -
The incoming edge of each node is the callee, and the outgoing edge is the caller.
-
When the cursor moves to a node, all its incoming and outgoing edges will be automatically highlighted.
-
When the cursor moves to an edge, all the edges overlapping with the current cursor (there may be multiple edges) will be automatically highlighted.
-
Each node and edge can be jumped to the corresponding position using the shortcut key
gd
. -
For each node, you can use the shortcut key
K
to display the full file path, for example:
- When there are overlapping multiple edges and you want to jump to one of them, an optional window will be provided:
As follows, foo1 is called by both foo2 and foo3 at the same time, and the cursor overlaps with both edges:
At this time, a selection will be provided when pressing gd
:
Besides, this plugin will generate mermaid graph and export it to a file, you can use CallGraphOpenMermaidGraph
cmd to open it.
Installing with lazy.nvim:
{
"ravenxrz/call-graph.nvim",
dependencies = {
"nvim-treesitter/nvim-treesitter",
},
opts = {
log_level = "info",
reuse_buf = true, -- Whether to reuse the same buffer for call graphs generated multiple times
auto_toggle_hl = true, -- Whether to automatically highlight
hl_delay_ms = 200, -- Interval time for automatic highlighting
in_call_max_depth = 4, -- Maximum search depth for incoming calls
ref_call_max_depth = 4, -- Maximum search depth for reference calls
export_mermaid_graph = false, -- Whether to export the Mermaid graph
}
}
- CallGraphI: Generate a call graph using incoming calls (fast, but not comprehensive)
- CallGraphR: Generate a call graph using references + treesitter (slow, and many files will be opened, but the call graph is more comprehensive. Currently, it only supports C++)
- CallGraphO: Generate a outcomimg call graph using treesitter (only supports C++)
- CallGraphOpenMermaidGraph: Open the Mermaid graph
- CallGraphLog: Open the log of the call graph
- CallGraphLine: the default value is linked to
Search
.
1. When jumping from the call graph to the buffer, the location is incorrect
This may be a bug in Neovim or the LSP server. The return value of the vim.lsp.buf.incoming_calls
function is inaccurate. You can call :lua vim.lsp.buf.incoming_calls()
to confirm the return value of the location information
2. The call graph is incomplete
According to issue, the LSP incomingCalls only supports analyzing opened files. Therefore, for files that are not opened, the analysis may be missing.
Solutions: Manually jump around the graph (to open the file), and then regenerate the call graph from the root node; or use CallGraphR.