]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix c-ts-common--fill-paragraph for non-rust modes (bug#77727)
authorYuan Fu <casouri@gmail.com>
Wed, 16 Apr 2025 00:28:32 +0000 (17:28 -0700)
committerEshel Yaron <me@eshelyaron.com>
Thu, 17 Apr 2025 07:19:30 +0000 (09:19 +0200)
* 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

index 9fd2a678903c81b4c4a2f3f19a05e910a7dd40eb..d6516f185b24aeab68fea4a2328e049514529425 100644 (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