Subject: Re: bug#75609: Hideshow support for treesitter

From: Juri Linkov <[email-masked]>
Date: Thu, 23 Jan 2025 09:34:34 +0200

>> Maybe use the comment thing first, and then match “comment” with the node type?
>
> We need to introduce the new thing named "comment".
> It's necessary to add treesit support for 'forward-comment'
> that is used in many packages including 'hideshow'.
>
> This means adding the variable 'forward-comment-function'
> with the default value 'forward-comment-default-function'.
> Then will override it with 'treesit-forward-comment' that
> uses the comment thing.

So here is the treesit implementation for the new
variable forward-comment-function:

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 8d86d142e3f..894608bde81 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2945,6 +2945,29 @@ treesit-forward-sentence
      (if (> arg 0) #'treesit-end-of-thing #'treesit-beginning-of-thing)
      'sentence (abs arg))))
 
+(defun treesit-forward-comment (&optional count)
+  "Tree-sitter `forward-comment-function' implementation.
+
+COUNT is the same as in `forward-comment'."
+  (let ((res t) thing)
+    (while (> count 0)
+      (skip-syntax-forward " >")
+      (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)))
+            (setq count (1- count)))
+        (setq count 0 res nil)))
+    (while (< count 0)
+      (skip-syntax-backward " >")
+      (setq thing (treesit-thing-at (max (1- (point)) (point-min)) 'comment))
+      (if (and thing (eq (point) (treesit-node-end thing)))
+          (progn
+            (goto-char (treesit-node-start thing))
+            (setq count (1+ count)))
+        (setq count 0 res nil)))
+    res))
+
 (defun treesit-default-defun-skipper ()
   "Skips spaces after navigating a defun.
 This function tries to move to the beginning of a line, either by
@@ -3654,6 +3736,9 @@ treesit-major-mode-setup
   (when (treesit-thing-defined-p 'sentence nil)
     (setq-local forward-sentence-function #'treesit-forward-sentence))
 
+  (when (treesit-thing-defined-p 'comment nil)
+    (setq-local forward-comment-function #'treesit-forward-comment))
+
   ;; Imenu.
   (when (or treesit-aggregated-simple-imenu-settings
             treesit-simple-imenu-settings)
diff --git a/src/syntax.c b/src/syntax.c
index 6ffa8a94c7f..e25618ee03d 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -2434,6 +2434,9 @@ DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
   int dummy2;
   unsigned short int quit_count = 0;
 
+  if (!NILP (Vforward_comment_function))
+    return calln (Vforward_comment_function, count);
+
   CHECK_FIXNUM (count);
   count1 = XFIXNUM (count);
   stop = count1 > 0 ? ZV : BEGV;
@@ -3796,6 +3799,11 @@ syms_of_syntax (void)
   DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
   Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
 
+  DEFVAR_LISP ("forward-comment-function", Vforward_comment_function,
+	       doc: /* If non-nil, `forward-comment' delegates to this function.
+Should take the same arguments and behave similarly to `forward-comment'.  */);
+  Vforward_comment_function = Qnil;
+
   defsubr (&Ssyntax_table_p);
   defsubr (&Ssyntax_table);
   defsubr (&Sstandard_syntax_table);