-
Notifications
You must be signed in to change notification settings - Fork 311
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
Adding support for regex as keys when expanding snippets #988
Comments
It's not currently possible. Sounds like an interesting feature, though I think it breaks the current decision of looking up the snippet key in a hashtable, so implementation might be somewhat involved. I guess the check against regexps could just happen first, and then fallback to hashtable lookup otherwise. #114 (Multiple keys) is sort of related. |
My current approach is to do a regex match on from the beginning of the line to the cursor so that the search space is small. The regex is appended with a '$' so that the matched part is guaranteed to end at the cursor. The matched string is then deleted and placed in a global variable so that the snippet can use it. I could submit it as a pull-request but I feel like it would be nicer if the regex could be defined in the snippet file instead of in elisp code. |
You can use
Agreed. |
How would you feel about submitting a pull-request implementing the regex-in-snippet-file approach? |
I would love to! If you could give me a few pointers to bits of code that are relevant I think I could do it. |
Okay, first off I should mention that since yasnippet is in GNU ELPA, you need to assign copyright before I can install subsantial (roughly defined as more than 15 lines) patches from you. To start the copyright assignment process, fill in this form, with "Emacs" as the program name, and send it to the mentioned email address. As for code pointers, I'm thinking you'll want to allow the
And finally, update Those are my thoughts about it without trying to implement anything, so it's possible I've made some design bug in the above: if something doesn't make sense, don't hesitate to ask questions. |
So many cool features of UltiSnips. Hope they will be implemented in yasnippet. |
So I have realized that the process for expanding regular snippets and regexp snippets will probably be different. I am thinking that regexp snippets should delete the text that matched the text and put that text into a variable like I think that it might be better to modify |
I think this part is already covered in
This part is missing though. |
I noticed that |
It doesn't look like any callers are interested in any template apart from the first, maybe it's easier to always return a single template, with a single position. |
Sorry if this is off topic but how do I debug tests in yasnippet? If I run ert as is described in the test file I just get the message that there are unexpected errors. I tested running ert manually so that I can get a backtrace but the backtrace is so large that I can't even copy the backtrace without Emacs hanging or lagging immensly. |
Oh, maybe it's because
Which Emacs version are you using? I think there was a bug in 26.1 that caused it to ignore Alternatively, use If you have |
First off it seems like my Emacs version was the issue, I had Emacs 26. I tried a different approach which you can find here here. It is by no means finished but it outlines the approach. First we parse a new directive called "regexp-key". The reason that I went for the approach above was because I got type errors because I noticed that was that snippets with a direct key-binding don't have to have a key. Can we add to the mechanism that is used there in some way to solve the problem above? |
That sounds fine.
Hmm, I'm not sure about this. Looks like you somewhat arbitrarily
I think it's better if you put the regexp matching in
Could we perhaps have In retrospect, perhaps I should have suggested starting by |
The reason I chose a limit is because it is expensive to match backwards as regexps really only can search forwards. One solution is therefore to search forwards in a substring of the buffer, the limit could be the beginning of the line or the buffer. Another could be to do
I am not sure I understand this. Is it not the point to have keys that do not respect yas-key-syntaxes? I would want to have snippets that expand on surrounding parentheses(ex.
That issue could be fixed with regexp though.
I dug around a bit more and found that perhaps only two changes were actually necessary. You can view them here. Edit2: |
Well yes, there has to be some limit. The question is whether it should be configurable or not.
Might be okay. It's true that using word boundaries and such in the regexp might subsume some of the things that By the way, what about using
I don't know, you opened the feature request, so I guess you have a better of what the point is than I do :)
Can you elaborate on this a bit, I'm not quite following what you mean here. Where would point/cursor be in your example? What's the sequence of input? |
I don't know a lot of elisp at all but maybe that has better performance? Is there a way to limit the search space with that option? In any case it should be easy to swap between the two.
Well then I don't want them to follow
Ok. Let |
Yes, that's what the
Perhaps marginally better, it probably doesn't make that much difference. More importantly, it requires the match to end at point (that's the
What would the regexp key be in this case? From my point of view, |
No it won't because I append "$" to the regexp in
Well I just realized that we could probably skip limiting to the current line and just go with |
Oh, yeah, I missed that. In addition to being unclear, it means that snippets created through
I'm not declaring victory yet, it might still turn out that the |
I do not think this will work very well. I have tried tweaking Don't know what a good solution should look like, but I would vote not to do it this way. |
@serycjon Well in your example I feel like it is a bad key rather than weirdness from |
The "st" key was a regular key, that is the point. I think that it would often make sense to have different |
Oh yeah, I get what you mean now. That would become very weird indeed. Thank you for pointing that out. |
Well I am back with a new report. Multiple regexp-keys can match the same text. In yasnippet this I also made regexp-keys respect I changed using Lastly I have added some tests and documentation. |
Hmm, not sure I want to start adding yet another mechanism for configuring this. Do you think it would be very common to have overlapping regexp keys? Why do you have a new
I think it's better to name it PS any progress on the copyright assignment? I don't see any likely matching name in the list |
The article in the original post had snippets that depended on order so I figured I would implement it. I don't think the implementation is very complicated as it uses the mechanisms already in place.
That is something that should be removed. I moved the code from
Fair enough. Edit: That would be kinda pointless though as normal keys are constant and are declared in the same snippet as the variable would be used.
I thought I would do it when I was done. I should start doing that now though as most of the code seems to be complete. |
Oh, I think it's better to start early, as most of the process involves waiting, so it can easily be done in parallel. |
It's less about complication on the implementing side, and more about complication for the user trying to configure things. But maybe it's practically a necessity to be able to order regexp keys.
Yeah, but it would be nice if a snippet could keep working even if you happen to remove its regexp, and use a plain key instead. Plus it makes the documentation a bit simpler. |
I think having
Yeah it would probably be simpler for the user. Another benefit could be that it is easier to change key definitions. |
I've started using the fork created by @Zetagon and am really enjoying the functionality. Thanks for working on this! Would love to see merged in when you feel it's ready. |
Yeah the only thing that I'm waiting for is an approval for assignment of copyright but I have not gotten a response in months. Maybe it is time to send another mail again... |
...Any updates on this? |
Well I sent another mail. Hopefully they will respond this time. |
I have sent the request now so hopefully we can start merging now! |
@Zetagon how's it going? |
I'm struggling a bit to get a disclaimer from my university. They will
not try to claim my work, it's just a little bit of confusion that needs
to be cleared up. After that's done I think we can actually begin the
work to merge this in!
tecosaur <[email protected]> writes:
… @Zetagon how's it going?
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#988 (comment)
|
I had the same issue with my uni... be prepared to wait a couple months 😭. |
Any update on this? |
Yes! I might finally get the papers signed! There was an issue where the guys at FSF forgot to attach the disclaimer for my school so I had to wait some extra rounds in email-limbo... Edit: Papers have been signed! Now I just have to get a confirmation from FSF. |
Papers are signed! @npostavs Are we ready to merge now? |
Any updates here? It will definitely be an interesting feature |
For those that want to use this for latex: there is a very nice package called https://github.com/tecosaur/Auto-LaTeX-Snippets that works very nicely for inputing latex-snippets. There is no readme but the idea is that you type in "ascii math" (e.g. a/b) which gets converted to latex math on the fly (\fraction{a}{b}). |
A readme is coming, once I finish separating the backend from the actual snippets :) |
It looks very promising. I'll follow your repo. |
Backend seperation complete! The backend now lives at ymarco/auto-activating-snippets while the pre-typed LaTeX snippets are on tecosaur/LaTeX-auto-activating-snippets. Both complete with READMEs and configuration examples. |
Awesome work guys! Please submit those packages to MELPA! Thank you! |
Ping. |
Any update on this feature? |
I'm not working on this anymore, and don't intend to in the future. If anybody wants to work on it, feel free to do so. |
I needed this for react to simulate the way they do it in vscode with emmet. I ended up doing this:
Ideally I would like to do this as the key
I know this is how it works in ultisnips, given the power of elisp and yasnippet IDKY this is not a thing. |
I read https://castel.dev/post/lecture-notes-1/ and wanted to replicate at least the simpler parts of the fraction snippet. Is it possible to easily write such snippets in yasnippet where the key is a regex?
In case it is not currently possible I have hacked together some elisp code using the interface provided that can do this but it is not very integrated into yasnippet. I have basically made a new keybinding that checks if a regex has matched. If it matched a corresponding snippet is expanded using
yas-lookup-snippet
andyas-expand-snippet
. The configuration still happens in elisp code and not in a snippet buffer which is suboptimal.Is anyone else interested in using regex as keys? If so I could with some help integrate this more into yasnippet and merge it.
The text was updated successfully, but these errors were encountered: