From 46b8b1bf7512ed0d46dd5b7a50ad2245a314c28a Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Tue, 15 Apr 2025 17:28:32 -0700 Subject: [PATCH] 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. (cherry picked from commit 9d43715baa5da4a644e180c7fb4550be0eb69be4) --- lisp/progmodes/c-ts-common.el | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el index 9fd2a678903..d6516f185b2 100644 --- a/lisp/progmodes/c-ts-common.el +++ b/lisp/progmodes/c-ts-common.el @@ -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 -- 2.39.5