1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 22:41:06 -08:00

Make highlighting more regular across TS modes (bug#61205)

* doc/emacs/display.texi (Parser-based Font Lock):
Update description of treesit-font-lock-level, moving 'property' to
level 4.

* lisp/treesit.el (treesit-font-lock-level): Likewise, in docstring.

* lisp/progmodes/c-ts-mode.el (c-ts-base-mode): Do that here.

* lisp/progmodes/cmake-ts-mode.el (cmake-ts-mode): Add a comment.

* lisp/progmodes/go-ts-mode.el (go-ts-mode): Add 'definition' to
level 1.  Move 'function', 'property' and 'variable' to level 4.
(go-ts-mode--font-lock-settings): Move a bunch of existing rules
to 'definition'.  Add highlighting of function parameters.

* lisp/progmodes/rust-ts-mode.el (rust-ts-mode)
(rust-ts-mode--font-lock-settings): Same.  And also change "scoped
identifiers" highlights to only match capitalized names.
This commit is contained in:
Dmitry Gutov 2023-02-05 15:46:28 +02:00
parent 1dd751c3ac
commit 0c125fcc67
6 changed files with 48 additions and 29 deletions

View file

@ -1159,11 +1159,11 @@ function definitions.
This level adds fontification of keywords, strings, and data types. This level adds fontification of keywords, strings, and data types.
@item Level 3 @item Level 3
This is the default level; it adds fontification of assignments, This is the default level; it adds fontification of assignments,
numbers, properties, etc. numbers, etc.
@item Level 4 @item Level 4
This level adds everything else that can be fontified: operators, This level adds everything else that can be fontified: operators,
delimiters, brackets, other punctuation, function names in function delimiters, brackets, other punctuation, function names in function
calls, variables, etc. calls, property look ups, variables, etc.
@end table @end table
@vindex treesit-font-lock-feature-list @vindex treesit-font-lock-feature-list

View file

@ -779,8 +779,8 @@ the semicolon. This function skips the semicolon."
(setq-local treesit-font-lock-feature-list (setq-local treesit-font-lock-feature-list
'(( comment definition) '(( comment definition)
( keyword preprocessor string type) ( keyword preprocessor string type)
( assignment constant escape-sequence label literal property ) ( assignment constant escape-sequence label literal)
( bracket delimiter error function operator variable)))) ( bracket delimiter error function operator property variable))))
;;;###autoload ;;;###autoload
(define-derived-mode c-ts-mode c-ts-base-mode "C" (define-derived-mode c-ts-mode c-ts-base-mode "C"

View file

@ -220,6 +220,9 @@ the subtrees."
(setq-local treesit-font-lock-feature-list (setq-local treesit-font-lock-feature-list
'((comment) '((comment)
(keyword string) (keyword string)
;; 'function' and 'variable' here play slightly
;; different roles than in other ts modes, so we
;; kept them at level 3.
(builtin constant escape-sequence function number variable) (builtin constant escape-sequence function number variable)
(bracket error misc-punctuation))) (bracket error misc-punctuation)))

View file

@ -123,17 +123,26 @@
:feature 'delimiter :feature 'delimiter
'((["," "." ";" ":"]) @font-lock-delimiter-face) '((["," "." ";" ":"]) @font-lock-delimiter-face)
:language 'go
:feature 'definition
'((function_declaration
name: (identifier) @font-lock-function-name-face)
(method_declaration
name: (field_identifier) @font-lock-function-name-face)
(method_spec
name: (field_identifier) @font-lock-function-name-face)
(field_declaration
name: (field_identifier) @font-lock-property-face)
(parameter_declaration
name: (identifier) @font-lock-variable-name-face))
:language 'go :language 'go
:feature 'function :feature 'function
'((call_expression '((call_expression
function: (identifier) @font-lock-function-name-face) function: (identifier) @font-lock-function-name-face)
(call_expression (call_expression
function: (selector_expression function: (selector_expression
field: (field_identifier) @font-lock-function-name-face)) field: (field_identifier) @font-lock-function-name-face)))
(function_declaration
name: (identifier) @font-lock-function-name-face)
(method_declaration
name: (field_identifier) @font-lock-function-name-face))
:language 'go :language 'go
:feature 'keyword :feature 'keyword
@ -221,11 +230,10 @@
;; Font-lock. ;; Font-lock.
(setq-local treesit-font-lock-settings go-ts-mode--font-lock-settings) (setq-local treesit-font-lock-settings go-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list (setq-local treesit-font-lock-feature-list
'(( comment) '(( comment definition)
( keyword string type) ( keyword string type)
( constant escape-sequence function label number ( constant escape-sequence label number)
property variable) ( bracket delimiter error function operator property variable)))
( bracket delimiter error operator)))
(treesit-major-mode-setup))) (treesit-major-mode-setup)))

View file

@ -155,6 +155,16 @@
:feature 'delimiter :feature 'delimiter
'((["," "." ";" ":" "::"]) @font-lock-delimiter-face) '((["," "." ";" ":" "::"]) @font-lock-delimiter-face)
:language 'rust
:feature 'definition
'((function_item name: (identifier) @font-lock-function-name-face)
(macro_definition "macro_rules!" @font-lock-constant-face)
(macro_definition (identifier) @font-lock-preprocessor-face)
(field_declaration name: (field_identifier) @font-lock-property-face)
(parameter pattern: (identifier) @font-lock-variable-name-face)
(parameter
pattern: (reference_pattern (identifier) @font-lock-variable-name-face)))
:language 'rust :language 'rust
:feature 'function :feature 'function
'((call_expression '((call_expression
@ -164,15 +174,12 @@
field: (field_identifier) @font-lock-function-name-face) field: (field_identifier) @font-lock-function-name-face)
(scoped_identifier (scoped_identifier
name: (identifier) @font-lock-function-name-face)]) name: (identifier) @font-lock-function-name-face)])
(function_item (identifier) @font-lock-function-name-face)
(generic_function (generic_function
function: [(identifier) @font-lock-function-name-face function: [(identifier) @font-lock-function-name-face
(field_expression (field_expression
field: (field_identifier) @font-lock-function-name-face) field: (field_identifier) @font-lock-function-name-face)
(scoped_identifier (scoped_identifier
name: (identifier) @font-lock-function-name-face)]) name: (identifier) @font-lock-function-name-face)])
(macro_definition "macro_rules!" @font-lock-constant-face)
(macro_definition (identifier) @font-lock-preprocessor-face)
(macro_invocation macro: (identifier) @font-lock-preprocessor-face)) (macro_invocation macro: (identifier) @font-lock-preprocessor-face))
:language 'rust :language 'rust
@ -208,20 +215,20 @@
(mod_item name: (identifier) @font-lock-constant-face) (mod_item name: (identifier) @font-lock-constant-face)
(primitive_type) @font-lock-type-face (primitive_type) @font-lock-type-face
(type_identifier) @font-lock-type-face (type_identifier) @font-lock-type-face
(scoped_identifier name: (identifier) @font-lock-type-face) ((scoped_identifier name: (identifier) @font-lock-type-face)
(scoped_identifier path: (identifier) @font-lock-constant-face) (:match "^[A-Z]" @font-lock-type-face))
((scoped_identifier path: (identifier) @font-lock-type-face)
(:match "^[A-Z]" @font-lock-type-face))
((scoped_identifier
(scoped_identifier (scoped_identifier
(scoped_identifier path: (identifier) @font-lock-type-face))
path: (identifier) @font-lock-constant-face)) (:match "^[A-Z]" @font-lock-type-face))
((scoped_identifier ((scoped_identifier
path: [(identifier) @font-lock-type-face path: [(identifier) @font-lock-type-face
(scoped_identifier (scoped_identifier
name: (identifier) @font-lock-type-face)]) name: (identifier) @font-lock-type-face)])
(:match "^[A-Z]" @font-lock-type-face)) (:match "^[A-Z]" @font-lock-type-face))
(scoped_type_identifier path: (identifier) @font-lock-constant-face) (scoped_type_identifier path: (identifier) @font-lock-constant-face)
(scoped_use_list
path: [(identifier) @font-lock-constant-face
(scoped_identifier (identifier) @font-lock-constant-face)])
(type_identifier) @font-lock-type-face (type_identifier) @font-lock-type-face
(use_as_clause alias: (identifier) @font-lock-type-face) (use_as_clause alias: (identifier) @font-lock-type-face)
(use_list (identifier) @font-lock-type-face)) (use_list (identifier) @font-lock-type-face))
@ -317,11 +324,11 @@ delimiters < and >'s."
;; Font-lock. ;; Font-lock.
(setq-local treesit-font-lock-settings rust-ts-mode--font-lock-settings) (setq-local treesit-font-lock-settings rust-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list (setq-local treesit-font-lock-feature-list
'(( comment) '(( comment definition)
( keyword string) ( keyword string)
( attribute builtin constant escape-sequence ( attribute builtin constant escape-sequence
function number property type variable) number type)
( bracket delimiter error operator))) ( bracket delimiter error function operator property variable)))
;; Imenu. ;; Imenu.
(setq-local treesit-simple-imenu-settings (setq-local treesit-simple-imenu-settings

View file

@ -612,9 +612,10 @@ fontifications.
Level 1 usually contains only comments and definitions. Level 1 usually contains only comments and definitions.
Level 2 usually adds keywords, strings, data types, etc. Level 2 usually adds keywords, strings, data types, etc.
Level 3 usually represents full-blown fontifications, including Level 3 usually represents full-blown fontifications, including
assignments, constants, numbers and literals, properties, etc. assignments, constants, numbers and literals, etc.
Level 4 adds everything else that can be fontified: delimiters, Level 4 adds everything else that can be fontified: delimiters,
operators, brackets, punctuation, all functions and variables, etc. operators, brackets, punctuation, all functions, properties,
variables, etc.
In addition to the decoration level, individual features can be In addition to the decoration level, individual features can be
turned on/off by calling `treesit-font-lock-recompute-features'. turned on/off by calling `treesit-font-lock-recompute-features'.