bug#80837: treesit-forward-comment off-by-one issue: overshoots by one character

Juri Linkov <[email protected]> Thu, 30 Jul 2026 20:55:25 +0300
Newsgroups gmane.emacs.bugs
Organization LINKOV.NET
Message-ID <[email protected]>
>> I'm very sorry to reopen it, but we still have the same problem
>> in the emacs-31 release branch.  For example, try in c-ts-mode to
>> uncomment a comment, and it leaves the trailing */ comment endings.
>
> I'm not sure this is important enough to block the release,

Ok, then I will not add it to the release blocking bug#81521.

> but we can at least discuss whether we think there is a possible small,
> non-invasive for the emacs-31 branch.  Yuan, sorry to go back and forth
> so many times here, but do you think there is a way to do it less
> invasively?

I have tried to backport the commit fb5cf238a9d from master
to the release branch, but without adding the new variable,
and by retaining only code after the comment:
"Without `comment-start-line-regexp', it's kind of best-effort."

And can confirm that it fixes the problem.

Here is the patch that should be pushed to the release branch
where the commit author is Yuan:
treesit--likely-line-comment-p.patch (text/x-diff, 1.8 KB)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 7fa7df08194..d4c64bd9cba 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -3764,7 +3764,18 @@ treesit-forward-sentence
                      (max (point-min) (previous-single-char-property-change
                                        (point) 'treesit-parser)))))))
 
-(defun treesit-forward-comment (&optional count)
+(defun treesit--likely-line-comment-p (node)
+  "Return non-nil if NODE is likely a line comment."
+  (save-excursion
+    (goto-char (treesit-node-start node))
+    ;; Without `comment-start-line-regexp', it's kind of best-effort.
+    (and comment-start
+         ;; If `comment-end' is non-empty, `comment-start' must be
+         ;; paired with it.
+         (string-empty-p (string-trim (or comment-end "")))
+         (looking-at-p (regexp-quote (string-trim-right comment-start))))))
+
+(defun treesit-forward-comment (count)
   "Tree-sitter `forward-comment-function' implementation.
 
 COUNT is the same as in `forward-comment'."
@@ -3774,7 +3785,15 @@ treesit-forward-comment
       (setq thing (treesit-thing-at (point) 'comment))
       (if (and thing (eq (point) (treesit-node-start thing)))
           (progn
-            (goto-char (min (1+ (treesit-node-end thing)) (point-max)))
+            (goto-char (treesit-node-end thing))
+            ;; For line comments, go to the next line.  This is
+            ;; important because a) for navigation convenience, and b)
+            ;; many functions expect `forward-comment' to behave this
+            ;; way (bug#80837).
+            (when (treesit--likely-line-comment-p thing)
+              (skip-chars-forward " \t")
+              (when (looking-at-p "\n")
+                (forward-char)))
             (setq count (1- count)))
         (setq count 0 res nil)))
     (while (< count 0)