From 268ecddaa77b4e7aa880580dfc4c594f33f4ba9b Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Sat, 25 Jan 2025 20:14:15 +0200 Subject: [PATCH] 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). (cherry picked from commit 63df2164903e0cd6819187483a64b892aa7e0219) --- 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 4f19e506f9e..3bb6b6f7496 100644 --- 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 diff --git a/lisp/treesit.el b/lisp/treesit.el index b56a0d7a7c6..4d0bf4b5cda 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -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) diff --git a/src/syntax.c b/src/syntax.c index f9734bffa14..6751989bd3b 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -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); -- 2.39.5