(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'."
(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