15.13.2 Parser-based Font Lock

If your Emacs was built with the tree-sitter library, it can use the results of parsing the buffer text by that library for the purposes of fontification. This is usually faster and more accurate than the “traditional” methods described in the previous subsection, since the tree-sitter library provides full-blown parsers for programming languages and other kinds of formatted text which it supports. Major modes which utilize the tree-sitter library are named foo-ts-mode, with the ‘-ts-’ part indicating the use of the library. This subsection documents the Font Lock support based on the tree-sitter library.

You can control the amount of fontification applied by Font Lock mode of major modes based on tree-sitter by customizing the variable treesit-font-lock-level. Its value is a number between 1 and 4:

Level 1

This level usually fontifies only comments and function names in function definitions.

Level 2

This level adds fontification of keywords, strings, and data types.

Level 3

This is the default level; it adds fontification of assignments, numbers, etc.

Level 4

This level adds everything else that can be fontified: operators, delimiters, brackets, other punctuation, function names in function calls, property look ups, variables, etc.

What exactly constitutes each of the syntactical categories mentioned above depends on the major mode and the parser grammar used by tree-sitter for the major-mode’s language. However, in general the categories follow the conventions of the programming language or the file format supported by the major mode. The buffer-local value of the variable treesit-font-lock-feature-list holds the fontification features supported by a tree-sitter based major mode, where each sub-list shows the features provided by the corresponding fontification level.

Once you change the value of treesit-font-lock-level via M-x customize-variable (see Customizing Specific Items), it takes effect immediately in all the existing buffers and for files you visit in the future in the same session.