aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/progmodes/c-ts-mode.el39
1 files changed, 17 insertions, 22 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 165bdd668c3..7350ef3f4b8 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -1018,12 +1018,11 @@ if `c-ts-mode-emacs-sources-support' is non-nil."
1018;; FOR_EACH_TAIL, FOR_EACH_TAIL_SAFE, FOR_EACH_FRAME etc., followed by 1018;; FOR_EACH_TAIL, FOR_EACH_TAIL_SAFE, FOR_EACH_FRAME etc., followed by
1019;; an unbracketed body will mess up the parser, which parses the thing 1019;; an unbracketed body will mess up the parser, which parses the thing
1020;; as a function declaration. We "fix" it by adding a shadow parser 1020;; as a function declaration. We "fix" it by adding a shadow parser
1021;; for a language 'emacs-c' (which is just 'c' but under a different 1021;; with the tag `for-each'. We use this parser to find each
1022;; name). We use 'emacs-c' to find each FOR_EACH_* macro with a 1022;; FOR_EACH_* macro with a unbracketed body, and set the ranges of the
1023;; unbracketed body, and set the ranges of the C parser so that it 1023;; default C parser so that it skips those FOR_EACH_*'s. Note that we
1024;; skips those FOR_EACH_*'s. Note that we only ignore FOR_EACH_*'s 1024;; only ignore FOR_EACH_*'s with a unbracketed body. Those with a
1025;; with a unbracketed body. Those with a bracketed body parse more 1025;; bracketed body parse more or less fine.
1026;; or less fine.
1027;; 1026;;
1028;; In the meantime, we have a special fontification rule for 1027;; In the meantime, we have a special fontification rule for
1029;; FOR_EACH_* macros with a bracketed body that removes any applied 1028;; FOR_EACH_* macros with a bracketed body that removes any applied
@@ -1044,12 +1043,12 @@ For BOL see `treesit-simple-indent-rules'."
1044(defvar c-ts-mode--emacs-c-range-query 1043(defvar c-ts-mode--emacs-c-range-query
1045 (when (treesit-available-p) 1044 (when (treesit-available-p)
1046 (treesit-query-compile 1045 (treesit-query-compile
1047 'emacs-c `(((declaration 1046 'c `(((declaration
1048 type: (macro_type_specifier 1047 type: (macro_type_specifier
1049 name: (identifier) @_name) 1048 name: (identifier) @_name)
1050 @for-each-tail) 1049 @for-each-tail)
1051 (:match ,c-ts-mode--for-each-tail-regexp 1050 (:match ,c-ts-mode--for-each-tail-regexp
1052 @_name))))) 1051 @_name)))))
1053 "Query that finds a FOR_EACH_* macro with an unbracketed body.") 1052 "Query that finds a FOR_EACH_* macro with an unbracketed body.")
1054 1053
1055(defvar-local c-ts-mode--for-each-tail-ranges nil 1054(defvar-local c-ts-mode--for-each-tail-ranges nil
@@ -1079,9 +1078,11 @@ parser parse the whole buffer."
1079 "Set ranges for the C parser to skip some FOR_EACH_* macros. 1078 "Set ranges for the C parser to skip some FOR_EACH_* macros.
1080BEG and END are described in `treesit-range-rules'." 1079BEG and END are described in `treesit-range-rules'."
1081 (let* ((c-parser (treesit-parser-create 'c)) 1080 (let* ((c-parser (treesit-parser-create 'c))
1081 (for-each-parser (treesit-parser-create 'c nil nil 'for-each))
1082 (old-ranges c-ts-mode--for-each-tail-ranges) 1082 (old-ranges c-ts-mode--for-each-tail-ranges)
1083 (new-ranges (treesit-query-range 1083 (new-ranges (treesit-query-range
1084 'emacs-c c-ts-mode--emacs-c-range-query beg end)) 1084 (treesit-parser-root-node for-each-parser)
1085 c-ts-mode--emacs-c-range-query beg end))
1085 (set-ranges (treesit--clip-ranges 1086 (set-ranges (treesit--clip-ranges
1086 (treesit--merge-ranges 1087 (treesit--merge-ranges
1087 old-ranges new-ranges beg end) 1088 old-ranges new-ranges beg end)
@@ -1233,16 +1234,10 @@ in your configuration."
1233 :after-hook (c-ts-mode-set-modeline) 1234 :after-hook (c-ts-mode-set-modeline)
1234 1235
1235 (when (treesit-ready-p 'c) 1236 (when (treesit-ready-p 'c)
1236 ;; Add a fake "emacs-c" language which is just C. Used for 1237 ;; Create an "for-each" parser, see `c-ts-mode--emacs-set-ranges'
1237 ;; skipping FOR_EACH_* macros, see `c-ts-mode--emacs-set-ranges'. 1238 ;; for more.
1238 (setf (alist-get 'emacs-c treesit-load-name-override-list)
1239 '("libtree-sitter-c" "tree_sitter_c"))
1240 ;; If Emacs source support is enabled, make sure emacs-c parser is
1241 ;; after c parser in the parser list. This way various tree-sitter
1242 ;; functions will automatically use the c parser rather than the
1243 ;; emacs-c parser.
1244 (when c-ts-mode-emacs-sources-support 1239 (when c-ts-mode-emacs-sources-support
1245 (treesit-parser-create 'emacs-c)) 1240 (treesit-parser-create 'c nil nil 'for-each))
1246 1241
1247 (treesit-parser-create 'c) 1242 (treesit-parser-create 'c)
1248 ;; Comments. 1243 ;; Comments.