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.
This commit is contained in:
Yuan Fu 2022-11-27 14:15:57 -08:00
parent 849223fba1
commit d5dc1dbf7c
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
6 changed files with 45 additions and 31 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 "[]*".

View file

@ -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

View file

@ -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