emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master d5dc1dbf7c: Remove treesit-comment-start/end and use comment-star


From: Yuan Fu
Subject: master d5dc1dbf7c: Remove treesit-comment-start/end and use comment-start/end-skip
Date: Sun, 27 Nov 2022 17:21:06 -0500 (EST)

branch: master
commit d5dc1dbf7cb263d8ff541a0def028c2d7d24f82b
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    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.
---
 lisp/progmodes/c-ts-mode.el          | 18 +++++++++++++-----
 lisp/progmodes/csharp-mode.el        | 10 ++++++++--
 lisp/progmodes/java-ts-mode.el       | 10 +++++++---
 lisp/progmodes/js.el                 |  9 ++++++---
 lisp/progmodes/typescript-ts-mode.el | 11 +++++++----
 lisp/treesit.el                      | 18 ++++--------------
 6 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 086257483e..a79dabcd31 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -29,7 +29,7 @@
 ;;; Code:
 
 (require 'treesit)
-(require 'rx)
+(eval-when-compile (require 'rx))
 
 (declare-function treesit-parser-create "treesit.c")
 (declare-function treesit-induce-sparse-tree "treesit.c")
@@ -545,10 +545,13 @@ the subtrees."
 
   ;; Comments.
   (setq-local comment-start "/* ")
-  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end " */")
-  (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*"))))
-  (setq-local treesit-comment-end (rx (+ (or "*")) "/"))
+  (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                     (* (syntax whitespace))))
+  (setq-local comment-end-skip
+              (rx (* (syntax whitespace))
+                  (group (or (syntax comment-end)
+                             (seq (+ "*") "/")))))
 
   (setq-local treesit-simple-indent-rules
               (c-ts-mode--set-indent-style 'c))
@@ -568,8 +571,13 @@ the subtrees."
 
   ;; Comments.
   (setq-local comment-start "// ")
-  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end "")
+  (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                     (* (syntax whitespace))))
+  (setq-local comment-end-skip
+              (rx (* (syntax whitespace))
+                  (group (or (syntax comment-end)
+                             (seq (+ "*") "/")))))
 
   (treesit-parser-create 'cpp)
 
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index 3f691956f8..6712fcc57e 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -36,7 +36,8 @@
 (require 'treesit)
 
 (eval-when-compile
-  (require 'cc-fonts))
+  (require 'cc-fonts)
+  (require 'rx))
 
 (declare-function treesit-parser-create "treesit.c")
 (declare-function treesit-induce-sparse-tree "treesit.c")
@@ -888,8 +889,13 @@ Key bindings:
 
   ;; Comments.
   (setq-local comment-start "// ")
-  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end "")
+  (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                     (* (syntax whitespace))))
+  (setq-local comment-end-skip
+              (rx (* (syntax whitespace))
+                  (group (or (syntax comment-end)
+                             (seq (+ "*") "/")))))
 
   ;; Indent.
   (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 dd3d6d31e0..cf2482bb6e 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -29,6 +29,7 @@
 ;;; Code:
 
 (require 'treesit)
+(eval-when-compile (require 'rx))
 
 (declare-function treesit-parser-create "treesit.c")
 (declare-function treesit-induce-sparse-tree "treesit.c")
@@ -299,10 +300,13 @@ the subtrees."
 
   ;; Comments.
   (setq-local comment-start "// ")
-  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
   (setq-local comment-end "")
-  (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*"))))
-  (setq-local treesit-comment-end (rx (+ (or "*")) "/"))
+  (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                     (* (syntax whitespace))))
+  (setq-local comment-end-skip
+              (rx (* (syntax whitespace))
+                  (group (or (syntax comment-end)
+                             (seq (+ "*") "/")))))
 
   ;; Indent.
   (setq-local treesit-simple-indent-rules java-ts-mode--indent-rules)
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index f2016deb5d..ad1fe62d42 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3848,11 +3848,14 @@ Currently there are `js-mode' and `js-ts-mode'."
     (setq-local which-func-imenu-joiner-function #'js--which-func-joiner)
     ;; Comment.
     (setq-local comment-start "// ")
-    (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
     (setq-local comment-end "")
+    (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                       (* (syntax whitespace))))
+    (setq-local comment-end-skip
+                (rx (* (syntax whitespace))
+                    (group (or (syntax comment-end)
+                               (seq (+ "*") "/")))))
     (setq-local comment-multi-line t)
-    (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*"))))
-    (setq-local treesit-comment-end (rx (+ (or "*")) "/"))
     ;; Electric-indent.
     (setq-local electric-indent-chars
                (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 8a9d540bd3..bf483a31d3 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -25,8 +25,8 @@
 ;;; Code:
 
 (require 'treesit)
-(require 'rx)
 (require 'js)
+(eval-when-compile (require 'rx))
 
 (declare-function treesit-parser-create "treesit.c")
 
@@ -294,10 +294,13 @@
 
     ;; Comments.
     (setq-local comment-start "// ")
-    (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
     (setq-local comment-end "")
-    (setq-local treesit-comment-start (rx "/" (or (+ "/") (+ "*"))))
-    (setq-local treesit-comment-end (rx (+ (or "*")) "/"))
+    (setq-local comment-start-skip (rx (group "/" (or (+ "/") (+ "*")))
+                                       (* (syntax whitespace))))
+    (setq-local comment-end-skip
+                (rx (* (syntax whitespace))
+                    (group (or (syntax comment-end)
+                               (seq (+ "*") "/")))))
 
     ;; Electric
     (setq-local electric-indent-chars
diff --git a/lisp/treesit.el b/lisp/treesit.el
index bae44f6b0a..8f092f475d 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -973,16 +973,6 @@ parser notifying of the change."
 
 ;;; Indent
 
-;; `comment-start' and `comment-end' assume there is only one type of
-;; comment, and that the comment spans only one line.  So they are not
-;; sufficient for our purpose.
-
-(defvar-local treesit-comment-start nil
-  "Regular expression matching an opening comment token.")
-
-(defvar-local treesit-comment-end nil
-  "Regular expression matching a closing comment token.")
-
 (define-error 'treesit-indent-error
               "Generic tree-sitter indentation error"
               'treesit-error)
@@ -1071,7 +1061,7 @@ See `treesit-simple-indent-presets'.")
         (cons 'comment-end (lambda (_node _parent bol &rest _)
                              (save-excursion
                                (goto-char bol)
-                               (looking-at-p treesit-comment-end))))
+                               (looking-at-p comment-end-skip))))
         ;; TODO: Document.
         (cons 'catch-all (lambda (&rest _) t))
 
@@ -1097,14 +1087,14 @@ See `treesit-simple-indent-presets'.")
               (lambda (_n parent &rest _)
                 (save-excursion
                   (goto-char (treesit-node-start parent))
-                  (re-search-forward treesit-comment-start)
+                  (re-search-forward comment-start-skip)
+                  (skip-syntax-backward "-")
                   (point))))
         (cons 'comment-start-skip
               (lambda (_n parent &rest _)
                 (save-excursion
                   (goto-char (treesit-node-start parent))
-                  (re-search-forward treesit-comment-start)
-                  (skip-syntax-forward "-")
+                  (re-search-forward comment-start-skip)
                   (point))))
         ;; TODO: Document.
         (cons 'grand-parent



reply via email to

[Prev in Thread] Current Thread [Next in Thread]