From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "Jostein Kjønigsen" <jostein@secure.kjonigsen.net>
Cc: jostein@kjonigsen.net,
"Ergus via Emacs development discussions." <emacs-devel@gnu.org>,
Yuan Fu <casouri@gmail.com>,
Theodor Thornhill <theo@thornhill.no>
Subject: Re: Detecting tree-sitter based major-modes for end-user customization and third party functions/packages
Date: Tue, 20 Dec 2022 15:21:40 -0500 [thread overview]
Message-ID: <jwv35994ytu.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <cb2fbf5e-5ab4-c7c7-c45f-31709cdb9622@secure.kjonigsen.net> ("Jostein Kjønigsen"'s message of "Tue, 20 Dec 2022 21:14:03 +0100")
> Something like the code below:
>
> (defun my-auto-parent ()
> (interactive)
> (when (treesit-parser-list)
> (goto-char (treesit-node-start (treesit-node-parent (treesit-node-at
> (point)))))))
Even if Tree-sitter is available and there's a parser active in the
current buffer, `treesit-node-at` may return nil, so the better code is
probably something like
(defun my-auto-parent ()
(interactive)
(let ((node (treesit-node-at (point))))
(if node
(goto-char (treesit-node-start (treesit-node-parent node)))
<do something else>)))
and then lobby for `treesit-node-at` to return nil rather
than signal an error when there's no parser, tho an alternative is
(defun my-auto-parent ()
(interactive)
(condition-case nil
(let ((node (treesit-node-at (point))))
(if node
(goto-char (treesit-node-start (treesit-node-parent node)))
<do something else>))
(treesit-error
<do yet something else, or maybe not>)))
Stefan
prev parent reply other threads:[~2022-12-20 20:21 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-20 11:35 Detecting tree-sitter based major-modes for end-user customization and third party functions/packages Jostein Kjønigsen
2022-12-20 14:21 ` Eli Zaretskii
2022-12-20 15:38 ` Stefan Monnier
2022-12-20 19:44 ` Jostein Kjønigsen
2022-12-20 20:03 ` Stefan Monnier
2022-12-20 20:14 ` Jostein Kjønigsen
2022-12-20 20:21 ` Stefan Monnier [this message]
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=jwv35994ytu.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=casouri@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=jostein@kjonigsen.net \
--cc=jostein@secure.kjonigsen.net \
--cc=theo@thornhill.no \
/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).