From: Yuan Fu <casouri@gmail.com>
To: code@aleks.bg
Cc: 62204@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>
Subject: bug#62204: 30.0.50; Feature Request: treesit-major-mode-hook
Date: Sat, 18 Mar 2023 00:49:02 -0700 [thread overview]
Message-ID: <E63ED87B-2C27-46D8-A2C9-29C9A6FF56FA@gmail.com> (raw)
In-Reply-To: <86cz5adyi9.fsf@aleks.bg>
Aleksandar Dimitrov <code@aleks.bg> writes:
>>> Currently, I've found two ways to accomplish loading my functionality for all ts-modes:
>>>
>>> - enumerate them all and use their respective hooks
>>> - advise something like `treesit-major-mode-setup` to execute my code
>>
>> Isn't it enough to check that the buffer has a treesit parser?
>
> I'm not sure I understand you, so I'll try to provide some code.
>
> I'd like to be able to do something like this:
>
> (defun my-setup ()
> "Code that depends on the presence of TS")
> (add-hook 'treesit-major-mode-hook 'my-setup)
>
> If I understand you correctly, I could probably do something like this:
>
> (defmacro add-ts-mode-hook (f)
> "Add mode hook that only executes in ts modes"
> `(add-hook 'prog-mode-hook
> (lambda ()
> (when (treesit-language-at (point))
> (,f)))))
>
> I'd say there's bound to be more people who would like to configure a
> certain behaviour whenever treesit is available, regardless of major
> mode. A macro like the above could be a possible solution, but it
> doesn't feel terribly ergonomic.
>
>> A hooks sounds too blunt and ad-hoc for your purposes, AFAIU.
>
> The reason I want to execute my function in a hook is that it sets
> buffer local variables, and configures buffer-local behaviour, perhaps
> even keybindings. I was under the impression that hooks are the correct
> place to do this.
IIUC, you are trying to do something like
(er/enable-mode-expansions 'clojure-mode 'er/add-clojure-mode-expansions)
right?
But a tree-sitter-based expander doesn’t really depend on any particular
major mode. In essence, they depend on the existence of a tree-sitter
parser in the current buffer. So I suggest that you define a universal
expander (similar to er/expand-word, etc) that checks the existence of a
tree-sitter parser and uses the parser to expand the region, and simply
do nothing if there isn’t a parser.
But to work with tree-sitter, expand-region might need to disable some
of its expanders when a tree-sitter parser is available, in case there’s
some conflict between the existing language-specific expander and the
tree-sitter expander.
Yuan
next prev parent reply other threads:[~2023-03-18 7:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-15 11:50 bug#62204: 30.0.50; Feature Request: treesit-major-mode-hook Aleksandar Dimitrov
2023-03-15 14:24 ` Eli Zaretskii
2023-03-16 0:35 ` Aleksandar Dimitrov
2023-03-16 6:38 ` Eli Zaretskii
2023-03-19 22:35 ` Aleksandar Dimitrov
2023-03-18 7:49 ` Yuan Fu [this message]
2023-03-19 22:26 ` Aleksandar Dimitrov
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=E63ED87B-2C27-46D8-A2C9-29C9A6FF56FA@gmail.com \
--to=casouri@gmail.com \
--cc=62204@debbugs.gnu.org \
--cc=code@aleks.bg \
--cc=eliz@gnu.org \
/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).