[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
emacs-29 f571e8f1bb6: Improve c-ts-mode font-lock for function names (bu
From: |
Yuan Fu |
Subject: |
emacs-29 f571e8f1bb6: Improve c-ts-mode font-lock for function names (bug#63390) |
Date: |
Fri, 19 May 2023 19:12:28 -0400 (EDT) |
branch: emacs-29
commit f571e8f1bb678c52839180e450c2415b31a70516
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Improve c-ts-mode font-lock for function names (bug#63390)
When a function definition has preproc directives in its body, it
can't correctly parse into a function_definition. This fix tries to
recognize this case and highlight the function_declarator correctly.
* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--font-lock-settings): New rule.
(c-ts-mode--top-level-declarator): New function.
---
lisp/progmodes/c-ts-mode.el | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index b042782efa7..7a57d5adeb1 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -627,6 +627,13 @@ MODE is either `c' or `cpp'."
(function_definition
declarator: (_) @c-ts-mode--fontify-declarator)
+ ;; When a function definition has preproc directives in its body,
+ ;; it can't correctly parse into a function_definition. We still
+ ;; want to highlight the function_declarator correctly, hence
+ ;; this rule. See bug#63390 for more detail.
+ ((function_declarator) @c-ts-mode--fontify-declarator
+ (:pred c-ts-mode--top-level-declarator
+ @c-ts-mode--fontify-declarator))
(parameter_declaration
declarator: (_) @c-ts-mode--fontify-declarator)
@@ -750,6 +757,19 @@ For NODE, OVERRIDE, START, END, and ARGS, see
(treesit-node-start identifier) (treesit-node-end identifier)
face override start end))))
+(defun c-ts-mode--top-level-declarator (node)
+ "Return non-nil if NODE is a top-level function_declarator."
+ ;; These criterion are observed in
+ ;; xterm.c:x_draw_glyphless_glyph_string_foreground on emacs-29
+ ;; branch, described in bug#63390. They might not cover all cases
+ ;; where a function_declarator is at top-level, outside of a
+ ;; function_definition. We might need to amend them as we discover
+ ;; more cases.
+ (let* ((parent (treesit-node-parent node))
+ (grandparent (treesit-node-parent parent)))
+ (and (treesit-node-match-p parent "ERROR")
+ (null grandparent))))
+
(defun c-ts-mode--fontify-variable (node override start end &rest _)
"Fontify an identifier node if it is a variable.
Don't fontify if it is a function identifier. For NODE,
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- emacs-29 f571e8f1bb6: Improve c-ts-mode font-lock for function names (bug#63390),
Yuan Fu <=