Support filling line comments in c-ts-mode

Now we handle line comments separately because for line comments, each
line is an individual node, unlike block comments, where the whole
block comment is a single node.  fill-comment-paragraph turns out to
do the job perfectly fine.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--fill-paragraph): Extract out most parts into
c-ts-mode--fill-block-comment.
(c-ts-mode--fill-block-comment): New extracted function.
This commit is contained in:
Yuan Fu 2023-01-13 23:31:18 -08:00
parent f02998939c
commit d428d51066
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -682,6 +682,22 @@ ARG is passed to `fill-paragraph'."
(interactive "*P")
(save-restriction
(widen)
(let ((node (treesit-node-at (point))))
(when (string-match-p c-ts-mode--comment-regexp
(treesit-node-type node))
(if (save-excursion
(goto-char (treesit-node-start node))
(looking-at "//"))
(fill-comment-paragraph arg)
(c-ts-mode--fill-block-comment arg)))
;; Return t so `fill-paragraph' doesn't attempt to fill by
;; itself.
t)))
(defun c-ts-mode--fill-block-comment (&optional arg)
"Fillling function for block comments.
ARG is passed to `fill-paragraph'. Assume point is in a block
comment."
(let* ((node (treesit-node-at (point)))
(start (treesit-node-start node))
(end (treesit-node-end node))
@ -692,8 +708,6 @@ ARG is passed to `fill-paragraph'."
(end-marker nil)
(end-len 0))
(move-marker start-marker start)
(when (string-match-p c-ts-mode--comment-regexp
(treesit-node-type node))
;; We mask "/*" and the space before "*/" like
;; `c-fill-paragraph' does.
(atomic-change-group
@ -743,11 +757,7 @@ ARG is passed to `fill-paragraph'."
(when end-marker
(goto-char end-marker)
(delete-region (point) (+ end-len (point)))
(insert (make-string end-len ?\s))))
(goto-char orig-point))
;; Return t so `fill-paragraph' doesn't attempt to fill by
;; itself.
t)))
(insert (make-string end-len ?\s))))))
(defun c-ts-mode-comment-setup ()
"Set up local variables for C-like comment.