]> git.eshelyaron.com Git - emacs.git/commitdiff
Add an error fontification heuristic to c-ts-mode
authorYuan Fu <casouri@gmail.com>
Thu, 24 Nov 2022 01:35:15 +0000 (17:35 -0800)
committerYuan Fu <casouri@gmail.com>
Thu, 24 Nov 2022 03:48:28 +0000 (19:48 -0800)
* lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings): Use new
function.
(c-ts-fontify-error): New function.

lisp/progmodes/c-ts-mode.el

index 3555eb23d123c87460f96a2699fc1a7b6243d36f..fc35d9aeddae079e3e2a47c18b0a09906f957ca6 100644 (file)
@@ -314,7 +314,7 @@ MODE is either `c' or `cpp'."
 
    :language mode
    :feature 'error
-   '((ERROR) @font-lock-warning-face)
+   '((ERROR) @c-ts-fontify-error)
 
    :feature 'escape-sequence
    :language mode
@@ -421,6 +421,26 @@ This function corrects the fontification on the colon in
            (min end (treesit-node-end arg))
            'default override))))))
 
+(defun c-ts-fontify-error (node override start end &rest _)
+  "Fontify the error nodes.
+For NODE, OVERRIDE, START, and END, see
+`treesit-font-lock-rules'."
+  (let ((parent (treesit-node-parent node))
+        (child (treesit-node-child node 0)))
+    (treesit-fontify-with-override
+     (max start (treesit-node-start node))
+     (min end (treesit-node-end node))
+     (cond
+      ;; This matches the case MACRO(struct a, b, c)
+      ;; where struct is seen as error.
+      ((and (equal (treesit-node-type child) "identifier")
+            (equal (treesit-node-type parent) "argument_list")
+            (member (treesit-node-text child)
+                    '("struct" "long" "short" "enum" "union")))
+       'font-lock-keyword-face)
+      (t 'font-lock-warning-face))
+     override)))
+
 (defun c-ts-mode--imenu-1 (node)
   "Helper for `c-ts-mode--imenu'.
 Find string representation for NODE and set marker, then recurse