]> git.eshelyaron.com Git - emacs.git/commitdiff
Use tagged parser in c-ts-mode
authorYuan Fu <casouri@gmail.com>
Tue, 5 Sep 2023 03:05:50 +0000 (20:05 -0700)
committerYuan Fu <casouri@gmail.com>
Wed, 6 Sep 2023 04:03:37 +0000 (21:03 -0700)
Now that we have tags for parsers, use that instead of the
load-name-override hack.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--emacs-c-range-query): Change parser to C.
(c-ts-mode--emacs-set-ranges): Use the tagged parser.
(c-ts-mode): Create the tagged parser.

lisp/progmodes/c-ts-mode.el

index 165bdd668c311056ba1d9bbf0cd84a0f65a3b51d..7350ef3f4b877777e7972ce98881e1fcaeeb988e 100644 (file)
@@ -1018,12 +1018,11 @@ if `c-ts-mode-emacs-sources-support' is non-nil."
 ;; FOR_EACH_TAIL, FOR_EACH_TAIL_SAFE, FOR_EACH_FRAME etc., followed by
 ;; an unbracketed body will mess up the parser, which parses the thing
 ;; as a function declaration.  We "fix" it by adding a shadow parser
-;; for a language 'emacs-c' (which is just 'c' but under a different
-;; name).  We use 'emacs-c' to find each FOR_EACH_* macro with a
-;; unbracketed body, and set the ranges of the C parser so that it
-;; skips those FOR_EACH_*'s.  Note that we only ignore FOR_EACH_*'s
-;; with a unbracketed body.  Those with a bracketed body parse more
-;; or less fine.
+;; with the tag `for-each'.  We use this parser to find each
+;; FOR_EACH_* macro with a unbracketed body, and set the ranges of the
+;; default C parser so that it skips those FOR_EACH_*'s.  Note that we
+;; only ignore FOR_EACH_*'s with a unbracketed body.  Those with a
+;; bracketed body parse more or less fine.
 ;;
 ;; In the meantime, we have a special fontification rule for
 ;; FOR_EACH_* macros with a bracketed body that removes any applied
@@ -1044,12 +1043,12 @@ For BOL see `treesit-simple-indent-rules'."
 (defvar c-ts-mode--emacs-c-range-query
   (when (treesit-available-p)
     (treesit-query-compile
-     'emacs-c `(((declaration
-                  type: (macro_type_specifier
-                         name: (identifier) @_name)
-                  @for-each-tail)
-                 (:match ,c-ts-mode--for-each-tail-regexp
-                         @_name)))))
+     'c `(((declaration
+            type: (macro_type_specifier
+                   name: (identifier) @_name)
+            @for-each-tail)
+           (:match ,c-ts-mode--for-each-tail-regexp
+                   @_name)))))
   "Query that finds a FOR_EACH_* macro with an unbracketed body.")
 
 (defvar-local c-ts-mode--for-each-tail-ranges nil
@@ -1079,9 +1078,11 @@ parser parse the whole buffer."
   "Set ranges for the C parser to skip some FOR_EACH_* macros.
 BEG and END are described in `treesit-range-rules'."
   (let* ((c-parser (treesit-parser-create 'c))
+         (for-each-parser (treesit-parser-create 'c nil nil 'for-each))
          (old-ranges c-ts-mode--for-each-tail-ranges)
          (new-ranges (treesit-query-range
-                      'emacs-c c-ts-mode--emacs-c-range-query beg end))
+                      (treesit-parser-root-node for-each-parser)
+                      c-ts-mode--emacs-c-range-query beg end))
          (set-ranges (treesit--clip-ranges
                       (treesit--merge-ranges
                        old-ranges new-ranges beg end)
@@ -1233,16 +1234,10 @@ in your configuration."
   :after-hook (c-ts-mode-set-modeline)
 
   (when (treesit-ready-p 'c)
-    ;; Add a fake "emacs-c" language which is just C.  Used for
-    ;; skipping FOR_EACH_* macros, see `c-ts-mode--emacs-set-ranges'.
-    (setf (alist-get 'emacs-c treesit-load-name-override-list)
-          '("libtree-sitter-c" "tree_sitter_c"))
-    ;; If Emacs source support is enabled, make sure emacs-c parser is
-    ;; after c parser in the parser list. This way various tree-sitter
-    ;; functions will automatically use the c parser rather than the
-    ;; emacs-c parser.
+    ;; Create an "for-each" parser, see `c-ts-mode--emacs-set-ranges'
+    ;; for more.
     (when c-ts-mode-emacs-sources-support
-      (treesit-parser-create 'emacs-c))
+      (treesit-parser-create 'c nil nil 'for-each))
 
     (treesit-parser-create 'c)
     ;; Comments.