Query Source Code with Tree-sitter
TreeQuery is a new command-line tool for querying source code with Tree-sitter
Tree-sitter is a really cool project. It’s primarily designed for code syntax-highlighting use cases (in editors and IDEs), but it also exposes a Query API for selecting portions of a parsed syntax tree, using an S-Expression based query syntax.
TreeQuery is a new CLI that makes it easier to run Tree-sitter queries against local source code files. It installs as tq
and looks something like this:
> tq -q some_file.go "(function_declaration name: (identifier) @method_name)"
init
handleErr
someFunc
main
(prints out all the function names in a go file).
I think this is pretty cool. We’re still exploring use-cases and getting a feel for what’s possible to query, go ahead and give it a try! It’s still rough around the edges and more language support will be added soon. See the open issues for a sense of the roadmap.
It will soon be integrated into askgit
.