Code-shape is a tool that uses [Tree-sitter] to extract a shape of code definitions from a source code file. The tool uses the same language parsers that are installed for Tree-sitter CLI.
To install the code-shape
CLI it's possible to use Rust's Cargo package manager:
sh
cargo install code-shape
To start using to tool it's needed to do some preparation.
tree-sitter init-config
that creates a config file like ~/.config/tree-sitter/config.json
in Tree-sitter's [config dir]."parser-directories"
list in Tree-sitter's config file.To make it possible to extract a shape of definitions from some source code file for some language, it's needed to define a [query]. To define a new query create a file in a Code-shape's languages [config dir] ~/.config/code-shape/languages/
with an .scm
suffix like ~/.config/code-shape/languages/c.scm
and put there a set of Tree-sitter [query] patterns like:
scheme
; C language function definitions & declarations
[
(declaration
[
(function_declarator
declarator: (identifier) @fn.name
)
(init_declarator
(_
(function_declarator
(_ (_ declarator: (identifier) @fn.name))
)
)
)
]
)
(function_definition
[
(function_declarator
declarator: (identifier) @fn.name
)
(_
(function_declarator
declarator: (identifier) @fn.name
)
)
]
body: (_
"{" @fn.begin
"}" @fn.end
)
)
]
It's needed to define captures with special names:
<type>.name
is a capture where the type
may be, e.g., fn
, class
or anything else to match a code entity name.<type>.begin
and <type>.end
are special captures that allow for the tool to capture a context of entities and usually are tokens that defines a body of the the entity, e.g., a function body.An example of the tool output:
```sh
fn tsmallocdefault fn tscallocdefault fn tsreallocdefault fn tscurrentmalloc fn tscurrentcalloc fn tscurrentrealloc fn tssetallocator ```
For now the tool has builtin shape queries for the following language parsers: