From: Juri Linkov <juri@linkov.net>
To: 68993@debbugs.gnu.org
Cc: Yuan Fu <casouri@gmail.com>
Subject: bug#68993: treesitter support for forward-sexp-default-function
Date: Thu, 08 Feb 2024 19:38:17 +0200 [thread overview]
Message-ID: <86y1bv6jsm.fsf@mail.linkov.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 327 bytes --]
'treesit-forward-sentence' supports the node type 'text',
and for matching nodes it uses the fallback to
'forward-sentence-default-function'.
This patch does exactly the same for 'treesit-forward-sexp':
for nodes that match a new node type 'comment',
it uses the fallback to the new function
'forward-sexp-default-function'.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: forward-sexp-default-function.patch --]
[-- Type: text/x-diff, Size: 1880 bytes --]
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 82b2f97b4a5..284c4915f3a 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2137,7 +2137,10 @@ treesit-forward-sexp
(interactive "^p")
(let ((arg (or arg 1))
(pred (or treesit-sexp-type-regexp 'sexp)))
- (or (if (> arg 0)
+ (or (when (treesit-node-match-p (treesit-node-at (point)) 'comment t)
+ (funcall #'forward-sexp-default-function arg)
+ t)
+ (if (> arg 0)
(treesit-end-of-thing pred (abs arg) 'restricted)
(treesit-beginning-of-thing pred (abs arg) 'restricted))
;; If we couldn't move, we should signal an error and report
diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el
index 4b722b4e9a7..d3c3bf55de3 100644
--- a/lisp/emacs-lisp/lisp.el
+++ b/lisp/emacs-lisp/lisp.el
@@ -45,7 +45,12 @@ parens-require-spaces
:type 'boolean
:group 'lisp)
-(defvar forward-sexp-function nil
+(defun forward-sexp-default-function (&optional arg)
+ "Default function for `forward-sexp-function'."
+ (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
+ (if (< arg 0) (backward-prefix-chars)))
+
+(defvar forward-sexp-function #'forward-sexp-default-function
;; FIXME:
;; - for some uses, we may want a "sexp-only" version, which only
;; jumps over a well-formed sexp, rather than some dwimish thing
@@ -74,10 +79,7 @@ forward-sexp
"No next sexp"
"No previous sexp"))))
(or arg (setq arg 1))
- (if forward-sexp-function
- (funcall forward-sexp-function arg)
- (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
- (if (< arg 0) (backward-prefix-chars)))))
+ (funcall forward-sexp-function arg)))
(defun backward-sexp (&optional arg interactive)
"Move backward across one balanced expression (sexp).
[-- Attachment #3: Type: text/plain, Size: 483 bytes --]
Maybe the node type 'comment' is not the best name,
but it was intended to allow using the default function
to be able to move with 'M-C-f' in the comments and strings
there tree-sitter has no information.
It makes sense to support the default movement with 'M-C-f'
in the comments and strings of all ts modes. The second patch
shows how this could be achieved by adding the default
'comment' match to 'treesit-thing-settings' of all modes.
Or maybe this should be customizable?
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: treesit-major-mode-setup.patch --]
[-- Type: text/x-diff, Size: 1071 bytes --]
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 82b2f97b4a5..284c4915f3a 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -3054,6 +3057,18 @@ treesit-major-mode-setup
(setq-local outline-search-function #'treesit-outline-search
outline-level #'treesit-outline-level))
+ (dolist (parser (treesit-parser-list))
+ (let ((language (treesit-parser-language parser))
+ (comment (regexp-opt '("comment" "string" "string_content"))))
+ (unless (treesit-thing-defined-p 'comment language)
+ (if-let ((l (alist-get language treesit-thing-settings)))
+ (progn
+ (setf (alist-get 'comment l) (list comment))
+ (setf (alist-get language treesit-thing-settings) l))
+ (setq-local treesit-thing-settings
+ (append `((,language (comment ,comment)))
+ treesit-thing-settings))))))
+
;; Remove existing local parsers.
(dolist (ov (overlays-in (point-min) (point-max)))
(when-let ((parser (overlay-get ov 'treesit-parser)))
[-- Attachment #5: Type: text/plain, Size: 389 bytes --]
The third patch demonstrates how it's possible to close bug#67036
that was impossible to fix without more general changes in treesit.el.
The problem is that e.g. Ruby parser to such text:
hash[:key]
produces such syntax tree:
(element_reference object: (identifier) [ (simple_symbol) ])
so when point is on [ then 'M-C-f' can't move to ].
This is fixed now by the third patch:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: ruby-ts-mode.patch --]
[-- Type: text/x-diff, Size: 1409 bytes --]
diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el
index 598eaa461ff..4d0ae2e9303 100644
--- a/lisp/progmodes/ruby-ts-mode.el
+++ b/lisp/progmodes/ruby-ts-mode.el
@@ -1170,7 +1170,20 @@ ruby-ts-mode
"global_variable"
)
eol)
- #'ruby-ts--sexp-p)))))
+ #'ruby-ts--sexp-p))
+ (comment ,(lambda (node)
+ (or (member (treesit-node-type node)
+ '("comment" "string_content"))
+ (and (member (treesit-node-text node)
+ '("[" "]"))
+ (equal (treesit-node-type
+ (treesit-node-parent node))
+ "element_reference"))
+ (and (member (treesit-node-text node)
+ '("#{" "}"))
+ (equal (treesit-node-type
+ (treesit-node-parent node))
+ "interpolation"))))))))
;; AFAIK, Ruby can not nest methods
(setq-local treesit-defun-prefer-top-level nil)
next reply other threads:[~2024-02-08 17:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-08 17:38 Juri Linkov [this message]
2024-02-10 17:46 ` bug#68993: treesitter support for forward-sexp-default-function Juri Linkov
2024-02-12 1:28 ` Yuan Fu
2024-04-10 18:03 ` Juri Linkov
2024-02-12 1:42 ` Yuan Fu
2024-02-12 18:41 ` Juri Linkov
2024-04-16 10:15 ` Mattias Engdegård
2024-04-17 6:54 ` Juri Linkov
[not found] <5BB812B1-6A9F-4F9D-89C5-A4A1A191409C@gmail.com>
2024-04-14 16:23 ` Juri Linkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86y1bv6jsm.fsf@mail.linkov.net \
--to=juri@linkov.net \
--cc=68993@debbugs.gnu.org \
--cc=casouri@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).