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:
For the full list of supported major modes, check the variable
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.
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.
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:
(function_item (identifier) @func) (impl_item (type_identifier) @impl)
(class_definition (identifier) @class) (function_definition (identifier) @func)
(function_declaration (identifier) @func) (variable_declarator (identifier) @var)
For more details on tree queries, see Queries.