Code-shape
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.
Installation
To install the code-shape CLI it's possible to use Rust's Cargo package manager:
Usage
To start using the tool it's needed to do some preparation.
Prerequsites
- Install Tree-sitter CLI.
- Run
tree-sitter init-configthat creates a config file like~/.config/tree-sitter/config.jsonin Tree-sitter's config dir. - Create a directory where installed parsers would be located and add it in
"parser-directories"list in Tree-sitter's config file. - Clone Tree-sitter parsers for required languages to the parsers directory.
Define extraction query
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:
; C language function declarations
(declaration
[
(function_declarator
declarator: (identifier) @fn.declaration.name
)
(_
(function_declarator
declarator: (identifier) @fn.declaration.name
)
)
]
)
; C language function pointer declarations
(declaration
[
(init_declarator
(function_declarator
(_ (_ declarator: (identifier) @fn.pointer.declaration.name))
)
)
(init_declarator
(_
(function_declarator
(_ (_ declarator: (identifier) @fn.pointer.declaration.name))
)
)
)
]
)
; C language function definitions
(function_definition
[
(function_declarator
declarator: (identifier) @fn.name
)
(_
(function_declarator
declarator: (identifier) @fn.name
)
)
]
body: (_) @fn.scope
)
It's needed to define captures with special names:
<type>.nameis a capture where thetypemay be, e.g.,fn,classor anything else to match a code entity name.<type>.scopeis a special capture that allows 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.
Examples of the tool output:
# code-shape --scope source.c tree-sitter/lib/src/alloc.c
# code-shape examples/foo.c
# code-shape examples/foo.py
Embedded shape queries
For now the tool has builtin shape queries for the following language parsers: