aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2022-11-27 14:15:57 -0800
committerYuan Fu2022-11-27 14:20:56 -0800
commitd5dc1dbf7cb263d8ff541a0def028c2d7d24f82b (patch)
tree878994bb6bc5c1b6465fd0ba7df0352c5888d853
parent849223fba1ef899f90a6edff05bce24b90fbb043 (diff)
downloademacs-d5dc1dbf7cb263d8ff541a0def028c2d7d24f82b.tar.gz
emacs-d5dc1dbf7cb263d8ff541a0def028c2d7d24f82b.zip
Remove treesit-comment-start/end and use comment-start/end-skip
treesit-comment-start/end is unnecessary because of comment-start/end-skip, so they should be removed. Cleanup and set comment-start/end-skip for tree-sitter C-like major modes. I replaced the [ \t]* part in comment-start-skip with (syntax whitespace), which is what comment-end-skip uses. I also added grouping in comment-start-skip to match that of comment-end-skip. * lisp/progmodes/c-ts-mode.el (c-ts-mode) (c++-ts-mode) * lisp/progmodes/csharp-mode.el (csharp-ts-mode) * lisp/progmodes/java-ts-mode.el (java-ts-mode) * lisp/progmodes/js.el (js-ts-mode) * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode): Setup comment-start/end-skip. * lisp/treesit.el (treesit-comment-start) (treesit-comment-end): Remove variables. (treesit-simple-indent-presets): Use comment-start/end-skip instead.
-rw-r--r--lisp/progmodes/c-ts-mode.el18
-rw-r--r--lisp/progmodes/csharp-mode.el10
-rw-r--r--lisp/progmodes/java-ts-mode.el10
-rw-r--r--lisp/progmodes/js.el9
-rw-r--r--lisp/progmodes/typescript-ts-mode.el11
-rw-r--r--lisp/treesit.el18
6 files changed, 45 insertions, 31 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 086257483e8..a79dabcd312 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -29,7 +29,7 @@
29;;; Code: 29;;; Code:
30 30
31(require 'treesit) 31(require 'treesit)
32(require 'rx) 32(eval-when-compile (require 'rx))
33 33
34(declare-function treesit-parser-create "treesit.c") 34(declare-function treesit-parser-create "treesit.c")
35(declare-function treesit-induce-sparse-tree "treesit.c") 35(declare-function treesit-induce-sparse-tree "treesit.c")
@@ -545,10 +545,13 @@ the subtrees."
545 545
546 ;; Comments. 546 ;; Comments.
547 (setq-local comment-start "/* ") 547 (setq-local comment-start "/* ")
548 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
549 (setq-local comment-end " */") 548 (setq-local comment-end " */")
550 (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*")))) 549 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
551 (setq-local treesit-comment-end (rx (+ (or "*")) "/")) 550 (* (syntax whitespace))))
551 (setq-local comment-end-skip
552 (rx (* (syntax whitespace))
553 (group (or (syntax comment-end)
554 (seq (+ "*") "/")))))
552 555
553 (setq-local treesit-simple-indent-rules 556 (setq-local treesit-simple-indent-rules
554 (c-ts-mode--set-indent-style 'c)) 557 (c-ts-mode--set-indent-style 'c))
@@ -568,8 +571,13 @@ the subtrees."
568 571
569 ;; Comments. 572 ;; Comments.
570 (setq-local comment-start "// ") 573 (setq-local comment-start "// ")
571 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
572 (setq-local comment-end "") 574 (setq-local comment-end "")
575 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
576 (* (syntax whitespace))))
577 (setq-local comment-end-skip
578 (rx (* (syntax whitespace))
579 (group (or (syntax comment-end)
580 (seq (+ "*") "/")))))
573 581
574 (treesit-parser-create 'cpp) 582 (treesit-parser-create 'cpp)
575 583
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index 3f691956f8f..6712fcc57e2 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -36,7 +36,8 @@
36(require 'treesit) 36(require 'treesit)
37 37
38(eval-when-compile 38(eval-when-compile
39 (require 'cc-fonts)) 39 (require 'cc-fonts)
40 (require 'rx))
40 41
41(declare-function treesit-parser-create "treesit.c") 42(declare-function treesit-parser-create "treesit.c")
42(declare-function treesit-induce-sparse-tree "treesit.c") 43(declare-function treesit-induce-sparse-tree "treesit.c")
@@ -888,8 +889,13 @@ Key bindings:
888 889
889 ;; Comments. 890 ;; Comments.
890 (setq-local comment-start "// ") 891 (setq-local comment-start "// ")
891 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
892 (setq-local comment-end "") 892 (setq-local comment-end "")
893 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
894 (* (syntax whitespace))))
895 (setq-local comment-end-skip
896 (rx (* (syntax whitespace))
897 (group (or (syntax comment-end)
898 (seq (+ "*") "/")))))
893 899
894 ;; Indent. 900 ;; Indent.
895 (setq-local treesit-simple-indent-rules csharp-ts-mode--indent-rules) 901 (setq-local treesit-simple-indent-rules csharp-ts-mode--indent-rules)
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index dd3d6d31e02..cf2482bb6ee 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -29,6 +29,7 @@
29;;; Code: 29;;; Code:
30 30
31(require 'treesit) 31(require 'treesit)
32(eval-when-compile (require 'rx))
32 33
33(declare-function treesit-parser-create "treesit.c") 34(declare-function treesit-parser-create "treesit.c")
34(declare-function treesit-induce-sparse-tree "treesit.c") 35(declare-function treesit-induce-sparse-tree "treesit.c")
@@ -299,10 +300,13 @@ the subtrees."
299 300
300 ;; Comments. 301 ;; Comments.
301 (setq-local comment-start "// ") 302 (setq-local comment-start "// ")
302 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
303 (setq-local comment-end "") 303 (setq-local comment-end "")
304 (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*")))) 304 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
305 (setq-local treesit-comment-end (rx (+ (or "*")) "/")) 305 (* (syntax whitespace))))
306 (setq-local comment-end-skip
307 (rx (* (syntax whitespace))
308 (group (or (syntax comment-end)
309 (seq (+ "*") "/")))))
306 310
307 ;; Indent. 311 ;; Indent.
308 (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules) 312 (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules)
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index f2016deb5d8..ad1fe62d429 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3848,11 +3848,14 @@ Currently there are `js-mode' and `js-ts-mode'."
3848 (setq-local which-func-imenu-joiner-function #'js--which-func-joiner) 3848 (setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
3849 ;; Comment. 3849 ;; Comment.
3850 (setq-local comment-start "// ") 3850 (setq-local comment-start "// ")
3851 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
3852 (setq-local comment-end "") 3851 (setq-local comment-end "")
3852 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
3853 (* (syntax whitespace))))
3854 (setq-local comment-end-skip
3855 (rx (* (syntax whitespace))
3856 (group (or (syntax comment-end)
3857 (seq (+ "*") "/")))))
3853 (setq-local comment-multi-line t) 3858 (setq-local comment-multi-line t)
3854 (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*"))))
3855 (setq-local treesit-comment-end (rx (+ (or "*")) "/"))
3856 ;; Electric-indent. 3859 ;; Electric-indent.
3857 (setq-local electric-indent-chars 3860 (setq-local electric-indent-chars
3858 (append "{}():;," electric-indent-chars)) ;FIXME: js2-mode adds "[]*". 3861 (append "{}():;," electric-indent-chars)) ;FIXME: js2-mode adds "[]*".
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 8a9d540bd3c..bf483a31d34 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -25,8 +25,8 @@
25;;; Code: 25;;; Code:
26 26
27(require 'treesit) 27(require 'treesit)
28(require 'rx)
29(require 'js) 28(require 'js)
29(eval-when-compile (require 'rx))
30 30
31(declare-function treesit-parser-create "treesit.c") 31(declare-function treesit-parser-create "treesit.c")
32 32
@@ -294,10 +294,13 @@
294 294
295 ;; Comments. 295 ;; Comments.
296 (setq-local comment-start "// ") 296 (setq-local comment-start "// ")
297 (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
298 (setq-local comment-end "") 297 (setq-local comment-end "")
299 (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*")))) 298 (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
300 (setq-local treesit-comment-end (rx (+ (or "*")) "/")) 299 (* (syntax whitespace))))
300 (setq-local comment-end-skip
301 (rx (* (syntax whitespace))
302 (group (or (syntax comment-end)
303 (seq (+ "*") "/")))))
301 304
302 ;; Electric 305 ;; Electric
303 (setq-local electric-indent-chars 306 (setq-local electric-indent-chars
diff --git a/lisp/treesit.el b/lisp/treesit.el
index bae44f6b0ab..8f092f475de 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -973,16 +973,6 @@ parser notifying of the change."
973 973
974;;; Indent 974;;; Indent
975 975
976;; `comment-start' and `comment-end' assume there is only one type of
977;; comment, and that the comment spans only one line. So they are not
978;; sufficient for our purpose.
979
980(defvar-local treesit-comment-start nil
981 "Regular expression matching an opening comment token.")
982
983(defvar-local treesit-comment-end nil
984 "Regular expression matching a closing comment token.")
985
986(define-error 'treesit-indent-error 976(define-error 'treesit-indent-error
987 "Generic tree-sitter indentation error" 977 "Generic tree-sitter indentation error"
988 'treesit-error) 978 'treesit-error)
@@ -1071,7 +1061,7 @@ See `treesit-simple-indent-presets'.")
1071 (cons 'comment-end (lambda (_node _parent bol &rest _) 1061 (cons 'comment-end (lambda (_node _parent bol &rest _)
1072 (save-excursion 1062 (save-excursion
1073 (goto-char bol) 1063 (goto-char bol)
1074 (looking-at-p treesit-comment-end)))) 1064 (looking-at-p comment-end-skip))))
1075 ;; TODO: Document. 1065 ;; TODO: Document.
1076 (cons 'catch-all (lambda (&rest _) t)) 1066 (cons 'catch-all (lambda (&rest _) t))
1077 1067
@@ -1097,14 +1087,14 @@ See `treesit-simple-indent-presets'.")
1097 (lambda (_n parent &rest _) 1087 (lambda (_n parent &rest _)
1098 (save-excursion 1088 (save-excursion
1099 (goto-char (treesit-node-start parent)) 1089 (goto-char (treesit-node-start parent))
1100 (re-search-forward treesit-comment-start) 1090 (re-search-forward comment-start-skip)
1091 (skip-syntax-backward "-")
1101 (point)))) 1092 (point))))
1102 (cons 'comment-start-skip 1093 (cons 'comment-start-skip
1103 (lambda (_n parent &rest _) 1094 (lambda (_n parent &rest _)
1104 (save-excursion 1095 (save-excursion
1105 (goto-char (treesit-node-start parent)) 1096 (goto-char (treesit-node-start parent))
1106 (re-search-forward treesit-comment-start) 1097 (re-search-forward comment-start-skip)
1107 (skip-syntax-forward "-")
1108 (point)))) 1098 (point))))
1109 ;; TODO: Document. 1099 ;; TODO: Document.
1110 (cons 'grand-parent 1100 (cons 'grand-parent