The minor mode tree-sitter-mode
provides a buffer-local syntax tree, which is kept up-to-date with changes to the buffer’s text.
It can be toggled in a buffer by the command tree-sitter-mode
, or enabled through major mode hooks:
(add-hook 'rust-mode-hook #'tree-sitter-mode)
To enable it for all supported major modes:
(global-tree-sitter-mode)
For the full list of supported major modes, check the variable tree-sitter-major-mode-language-alist
.
Run M-x tree-sitter-hl-mode
to replace the regex-based highlighting provided by font-lock-mode
with tree-based syntax highlighting.
For more details, see Syntax Highlighting.
Run M-x tree-sitter-debug-mode
to show the current buffer’s syntax tree in a separate buffer.
Printing the syntax tree can be slow for very large buffers, as it hasn’t been optimized yet.
Run M-x tree-sitter-query-builder
to open the query playground, where you can write tree queries and see matches highlighted in the source buffer.
Here are some example queries to try:
Rust:
(function_item (identifier) @func)
(impl_item (type_identifier) @impl)
Python:
(class_definition (identifier) @class)
(function_definition (identifier) @func)
JavaScript:
(function_declaration (identifier) @func)
(variable_declarator (identifier) @var)
For more details on tree queries, see Queries.