-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TreesitER #279
TreesitER #279
Conversation
That is indeed fantastic! This looks great. Two points:
Cheers for doing this! |
I'd like to give it a shake, but I'll stop if it complicates the test setup too much (which it may).
Yeah, I was looking for one, but couldn't find anything. Things are still in flux so perhaps it's just not there yet. We could advise |
Sadly, there isn't. Maybe it is worth to propose it to emacs-devel? Dunno if it is to late for 29, though. |
I've opened a bug to see if we can't get some sort of hook for treesit. |
I think we can improve the current implementation by prefer marking the region between the first named child's beginning and the last named child's end first, compared to the entire parent node. (I may come up with that later, if possible.) |
Well, no global ts-mode-hook is going to come out of my feature request. However, we could have our cake and eat it, too, by hooking into prog-mode (or some other general hook, as text-mode could conceivably be parsed by tree-sitter, too) and checking for tree-sitter there. Something like that: (defmacro add-ts-mode-hook (f)
"Add mode hook that only executes in ts-enabled modes"
(when (and (fboundp 'treesit-available-p)
(treesit-available-p))
`(add-hook 'prog-mode-hook
(lambda ()
(when (treesit-language-at (point))
(,f)))))) Note that checking In general, I'd like to find a way to avoid having to make a check on every invocation, and only install the expander when necessary. It makes things clearer to the user: imagine the user inspecting It's also the only way to remove other expansions when tree-sitter is enabled.
I'll try that out, if it works, it might be the simplest option.
Finding strings and comments could be difficult, because AFAIU, not all grammars give the same kind of node names to these. E.g. in Also, some languages might have symbols, or atoms etc, on which these expansions should also work.
I agree in principle, though we're overriding in any case, by adding ours.
Yes, that's true, thanks for calling it to my attention. I'll try to think of a fix.
IIUC, we'd have to check whether we're on a delimiter, like |
This has a drawback: the user could have the feature enabled (i.e. Emacs compiled with treesitter support) but never actually load a treesitter mode during their editing session. We would still execute (require 'treesit-er-expansions)! I don't know of a good way to avoid that.
Instead of enumerating treesitter modes, we just ask every buffer whether it's a treesitter buffer in its hook.
@daanturo Thanks for your input. I've decided to append treesitter expansions to In my trials, this mostly solved the problems with brackets. The exception are brackets that the existing expansions don't find, but treesitter's expansion does find. I can't think of a mode-independent fix for that. At the very least you'd have to specify which brackets your ts-sub-mode expects. @magnars I've removed the enumeration of different modes. Sadly, my feature request for a hook for ts-modes has been met with little enthusiasm. Currently, the code has two weaknesses:
I've not yet solved the ecukes problem. That one's next on the list. |
Seems like a good middle ground. 👍 |
@magnars Yeah, as expected, running the tests is a mess. I didn't push the commit here, but made it available here on a separate branch. tl;dr: it's not really too difficult, but there's a hard requirement to point Emacs to a binary file (I chose The test works, it's just that Emacs won't actually load tree-sitter grammars without binary blobs, which aren't (yet) included in the executable or default Emacs distribution and perhaps never will be. |
Thank you for taking my previous comment to account , after a while I have noticed a few more things.
For example fn some_func(arg0: String, arg1: b|ool) In this case, after marking "bool", its parent node to mark after that should be "arg1: bool" instead of the whole parameters list including the brackets.
er-treesit.webm |
I am thrilling to see this being developed! Just manually added the https://github.com/magnars/expand-region.el/pull/279/files#diff-698e32988bb3d04dacdb2cb8f9b74ffe5371bec409c5fe594ce14d68371dd7f2 changes to my local package. Working beautifully on emacs 30. Specially good with those JSX (html inside javascript) tags. |
I have also applied the patch locally. |
Note that there is also a new package called https://github.com/casouri/expreg (I guess it will soon be added to ELPA, because its author asked for adding this package to ELPA) |
There's also https://github.com/mickeynp/combobulate too |
I have merged a more general approach to this feature in #282. Hopefully that should cover the use cases presented here as well. Feel free to open this PR again if that is not the case. Cheers! |
Emacs 29 will bring
treesit
—a new way to incrementally parse files usingtreesitter
grammars. It's fantastic 🎉This is supposed to bring treesit-based expansions to expand-region. Since we can use the AST directly, there is no need for more than one expansion rule, which makes the implementation almost trivial.
The only problem (and the reason this PR is still a draft) is that I can't figure out how to add the grammar files to the Emacs runtime used by ecukes. Any pointers are appreciated, but I'll try to tackle it over the weekend.
TODO: