]> git.eshelyaron.com Git - emacs.git/commitdiff
Add 'treesit-forward-comment' with 'forward-comment-function'
authorJuri Linkov <juri@linkov.net>
Sat, 25 Jan 2025 18:14:15 +0000 (20:14 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 26 Jan 2025 18:53:26 +0000 (19:53 +0100)
* 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).

(cherry picked from commit 63df2164903e0cd6819187483a64b892aa7e0219)

etc/NEWS
lisp/treesit.el
src/syntax.c

index 4f19e506f9e94b206052cfe0a6cf3bdb6b65bc0f..3bb6b6f7496de3e2e722257cf20a5ccb1d9e68d4 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -935,6 +935,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
index b56a0d7a7c65520c9515a81eff32813d7c869d53..4d0bf4b5cda6ed7a61c13f735e551f793519aa6f 100644 (file)
@@ -2937,6 +2937,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
@@ -3642,11 +3665,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)
index f9734bffa14801701c05142252a06f8a3d2d2b0e..6751989bd3b3ddd41bfc9675102a359885dce7c7 100644 (file)
@@ -2375,6 +2375,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;
@@ -3727,6 +3730,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);