GNU bug report logs -
#75609
Hideshow support for treesitter
Previous Next
Reported by: Juri Linkov <juri <at> linkov.net>
Date: Thu, 16 Jan 2025 17:50:01 UTC
Severity: wishlist
Tags: patch
Fixed in version 31.0.50
Done: Juri Linkov <juri <at> linkov.net>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
[Message part 1 (text/plain, inline)]
>> 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:
[treesit-forward-comment.patch (text/x-diff, inline)]
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);
This bug report was last modified 115 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.