[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 63df2164903: Add 'treesit-forward-comment' with 'forward-comment-
From: |
Juri Linkov |
Subject: |
master 63df2164903: Add 'treesit-forward-comment' with 'forward-comment-function' |
Date: |
Sat, 25 Jan 2025 13:14:40 -0500 (EST) |
branch: master
commit 63df2164903e0cd6819187483a64b892aa7e0219
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>
Add 'treesit-forward-comment' with 'forward-comment-function'
* lisp/treesit.el (treesit-forward-comment): New function.
(treesit-major-mode-setup): Set 'forward-comment-function' to
'treesit-forward-comment' if the 'comment' thing is defined.
* src/syntax.c (forward-comment-function): New variable.
(Fforward_comment): Call the function from 'forward-comment-function'
when it's non-nil (bug#75609).
---
etc/NEWS | 4 ++++
lisp/treesit.el | 28 +++++++++++++++++++++++++++-
src/syntax.c | 8 ++++++++
3 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/etc/NEWS b/etc/NEWS
index 385e943c997..af5c320a19b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1099,6 +1099,10 @@ the tree-sitter library. The new function
'treesit-show-paren-data' is
used to communicate the tree-sitter parsing results to
'show-paren-mode'.
+*** New treesit thing 'comment'.
+The new variable 'forward-comment-function' is set to the new function
+'treesit-forward-comment' if a major mode defines the thing 'comment'.
+
+++
*** New function 'treesit-language-display-name'.
This new function returns the display name of a language given the
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 5e00379fa06..e08aa52ca50 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2945,6 +2945,29 @@ by `text' and `sentence' in `treesit-thing-settings'."
(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-chars-forward " \t\n")
+ (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-chars-backward " \t\n")
+ (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
@@ -3649,11 +3672,14 @@ before calling this function."
(setq-local forward-list-function #'treesit-forward-list)
(setq-local down-list-function #'treesit-down-list)
(setq-local up-list-function #'treesit-up-list)
- (setq-local show-paren-data-function 'treesit-show-paren-data))
+ (setq-local show-paren-data-function #'treesit-show-paren-data))
(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 @@ between them, return t; otherwise return nil. */)
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 @@ In both cases, LIMIT bounds the search. */);
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);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master 63df2164903: Add 'treesit-forward-comment' with 'forward-comment-function',
Juri Linkov <=