Fix c-ts-common--fill-paragraph for non-rust modes (bug#77727)

* lisp/progmodes/c-ts-common.el:
(c-ts-common--comment-regexp): Add // and /* to regexp for Rust.
(c-ts-common--line-comment-p): Extract out into new function,
add a check that the parent node is a comment node.
(c-ts-common--fill-paragraph): Extract some code out.
This commit is contained in:
Yuan Fu 2025-04-15 17:28:32 -07:00
parent ae1d01328f
commit 9d43715baa
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -110,9 +110,25 @@ non-whitespace characters of the current line."
(defvar c-ts-common--comment-regexp
;; These covers C/C++, Java, JavaScript, TypeScript, Rust, C#.
(rx (or "comment" "line_comment" "block_comment"))
(rx (or "comment" "line_comment" "block_comment" "//" "/*"))
"Regexp pattern that matches a comment in C-like languages.")
(defun c-ts-common--line-comment-p (node)
"Return non-nil if NODE is a comment node."
(or (save-excursion
(goto-char (treesit-node-start node))
(looking-at "//"))
;; In rust, NODE will be the body of a comment, and the
;; parent will be the whole comment.
(let* ((parent (treesit-node-parent node))
(parent-start (treesit-node-start parent)))
(when (and (treesit-node-match-p
parent c-ts-common--comment-regexp)
parent parent-start)
(save-excursion
(goto-char parent-start)
(looking-at "//"))))))
(defun c-ts-common--fill-paragraph (&optional arg)
"Filling function for `c-ts-common'.
ARG is passed to `fill-paragraph'."
@ -122,16 +138,7 @@ ARG is passed to `fill-paragraph'."
(let ((node (treesit-node-at (point))))
(when (string-match-p c-ts-common--comment-regexp
(treesit-node-type node))
(if (or (save-excursion
(goto-char (treesit-node-start node))
(looking-at "//"))
;; In rust, NODE will be the body of a comment, and the
;; parent will be the whole comment.
(if-let* ((start (treesit-node-start
(treesit-node-parent node))))
(save-excursion
(goto-char start)
(looking-at "//"))))
(if (c-ts-common--line-comment-p node)
(fill-comment-paragraph arg)
(c-ts-common--fill-block-comment arg)))
;; Return t so `fill-paragraph' doesn't attempt to fill by