From ae91d5cd53111216788473e81b378c556ba496a2 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 21 Nov 2022 22:37:36 -0800 Subject: [PATCH] Tweak c-ts-mode fontification New features: function and variable, which consistently fontify all occurrences of functions and variables, respectively. Remove expression feature, as its purpose is fulfilled by function, variable, and property combined. Fix declaration feature, remove unnecessary rule from label feature. * lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings): See above description. (c-ts-mode--fontify-variable): New function. (c-ts-mode--base-mode): Add & remove features. --- lisp/progmodes/c-ts-mode.el | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 6eaf200182d..3555eb23d12 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -269,7 +269,7 @@ MODE is either `c' or `cpp'." :feature 'definition ;; Highlights identifiers in declarations. `((declaration - declarator: (_) @font-lock-variable-name-face) + declarator: (_) @c-ts-mode--fontify-declarator) (field_declaration declarator: (_) @c-ts-mode--fontify-declarator) @@ -295,21 +295,21 @@ MODE is either `c' or `cpp'." (identifier) @font-lock-variable-name-face)) (assignment_expression left: (subscript_expression - (identifier) @font-lock-variable-name-face))) + (identifier) @font-lock-variable-name-face)) + (init_declarator declarator: (_) @c-ts-mode--fontify-declarator)) :language mode - :feature 'expression + :feature 'function '((call_expression - function: (identifier) @font-lock-function-name-face) - (field_expression - argument: (identifier) @font-lock-variable-name-face) - (pointer_expression - (identifier) @font-lock-variable-name-face)) + function: (identifier) @font-lock-function-name-face)) + + :language mode + :feature 'variable + '((identifier) @c-ts-mode--fontify-variable) :language mode :feature 'label - '((expression_statement (identifier) @font-lock-variable-name-face) - (labeled_statement + '((labeled_statement label: (statement_identifier) @font-lock-constant-face)) :language mode @@ -367,6 +367,19 @@ For NODE, OVERRIDE, START, END, and ARGS, see (_ 'font-lock-variable-name-face)) override)))) +(defun c-ts-mode--fontify-variable (node override start end &rest _) + "Fontify an identifier node. +Fontify it if NODE is not a function identifier. For NODE, +OVERRIDE, START, END, and ARGS, see `treesit-font-lock-rules'." + (when (not (equal (treesit-node-type + (treesit-node-parent node)) + "call_expression")) + (treesit-fontify-with-override + (max (treesit-node-start node) start) + (min (treesit-node-end node) end) + 'font-lock-variable-name-face + override))) + (defun c-ts-mode--fontify-defun (node override start end &rest _) "Correctly fontify the DEFUN macro. For NODE, OVERRIDE, START, and END, see @@ -497,8 +510,8 @@ the subtrees." (setq-local treesit-font-lock-feature-list '(( comment constant keyword literal preprocessor string) ( assignment definition label property type) - ( bracket delimiter error escape-sequence expression - operator)))) + ( delimiter error escape-sequence function + operator variable bracket)))) ;;;###autoload (define-derived-mode c-ts-mode c-ts-mode--base-mode "C" -- 2.39.2