reddit / emacs

Context-aware yasnippets (via tree-sitter)

xenodium · 2022-12-12 20:56
xenodium · 2022-12-12 20:57
tl;dr Using tree-sitter, we can write context-aware yasnippets that extract details from syntax tree: https://xenodium.com/emacs-generate-a-swift-initializer
eleven_cupfuls · 2022-12-13 00:14
That is extremely impressive. Nice work! The Elisp API will be different with the built-in support but I imagine more stuff like this will be possible. FYI the 'tree-sitter-langs' Swift grammar is ancient and woefully incomplete. You will want to use one of these two if you go delving further: https://github.com/alex-pinkus/tree-sitter-swift https://gitlab.com/woolsweater/tree-sitter-swifter (Disclosure, the second one is mine.)
TheIsletOfLangerhans · 2022-12-13 02:04
This is pretty neat. I think you've finally managed to convince me to really check out tree-sitter.
churchzebra · 2022-12-13 02:23
soooo cool
esrse · 2022-12-13 04:29
Cool
pikachu_palestrante · 2022-12-13 06:30
The real question: which languae is this?
konrad1977 · 2022-12-13 06:51
Have you compared yours to Alex version? I've been using yours earlier, but then I forgot how to build it so I switched to Alex version. It would be great to have one of above integrated, either in tree-sitter-langs or in 29.x branch so we get swift-support out of the box. I noticed that tree-sitter really makes org-src-blocks slow. Takes like 1-2 seconds to insert a new line (M1 Pro Max, with 64 GB Ram - so its a pretty fast machine I got). Also opening a regular swift-buffer will have a delay of 1 second.
konrad1977 · 2022-12-13 06:52
Really nice, thanks for posting.
xenodium · 2022-12-13 07:46
Ah, thanks for letting me know the grammar is old! Being new to Tree-sitter, installing Swift support via tree-sitter-langs was really handy. Also big thanks for building the Swift grammar. Without work like yours, tricks like this would not be possible.
xenodium · 2022-12-13 07:50
Swift
xenodium · 2022-12-13 07:51
I’m pretty new to it too :)
xenodium · 2022-12-13 08:02
Thanks :) I see you’re using the Swift grammar too. I’m very new to tree-sitter. Are you primarily using it for syntax highlight? Other tricks?
konrad1977 · 2022-12-13 08:31
Only highlighting. But your addition looks very promising. Maybe this could be used to add stubs for protocol conformance? I dont think LSP supports that.
konrad1977 · 2022-12-13 08:48
I see an improvement now that I want (if you're up for the challenge). If you struct is marked as public, make sure that the initializer you are generated is also marked as public. This could be helpful for swift-packages.
xenodium · 2022-12-13 09:01
Nice suggestion. I may not get a chance to play with it for a little while, but patches totally welcome :)
xenodium · 2022-12-13 09:07
Protocol conformance would be neat. The protocol is often declared in a separate file so it may require some additional glue with lsp. Not sure. Maybe just visiting the protocol file to extract the snippet is enough.
xenodium · 2022-12-13 09:08
Thanks!
xenodium · 2022-12-13 09:09
Thank you :)
eleven_cupfuls · 2022-12-13 18:03
I have not tried Alex's grammar in some time. I should, I know, but I have just been focused on developing mine and I don't really want to have a "competition". It's good to know that his grammar is working well. Developing mine has been an interesting project, but to be honest parsing Swift via Tree-sitter is a pain in the ass. :) Syntactically it's a pretty complex, or at least ambiguous, language.
konrad1977 · 2022-12-13 19:50
How did I build yours, I want to try it again? (M1 MBP) I got this error. `.emacs.d/elpa/tree-sitter-langs-20221126.446/bin/swift.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))`
eleven_cupfuls · 2022-12-14 18:07
I use [`script/compile swift` from 'tree-sitter-langs'](https://github.com/emacs-tree-sitter/tree-sitter-langs/blob/master/script/compile) to build the grammar on both M1 and x86 machines. But I was looking at that code yesterday and I actually can't figure out _why_ it works to produce an arm64 dylib for me. There's no place where the target arch is being set. It's very odd. You could try using that script and passing an arch triple, like `compile swift $(clang -print-target-triple)` 🤔 That might work. (Side note, I also seem to have introduced a weird highlighting regression in a recent commit that I'm trying to fix without just reverting. That's going to take a few days to sort out.) Anyways, I finally spent some time last night setting up a Bitrise job to build the dylib. The build itself is fine; I just need to get notarization working and figure out how to upload it back to GitLab. I'll let you know when that is done. Thanks for your interest!