-
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
Suggestion for a repeat snippet feature. #1192
Comments
Could you clarify what "better syntax" you're thinking of? |
Just a repeat directive. Maybe a repeat hook (or just a function) could be implemented to run before
|
Just a repeat directive. Maybe a repeat hook (or just a function) could be
implemented to run before `yas-after-exit-snippet-hook`, repeating the
snippet by name a number of times or indefinitely until the prefix argument
is used.
``` text
# -*- mode: snippet -*-
# name:parameters
# key: p
# repeat: t (or a number)
# --
"$1, "
```
But what about the left-over comma?
Also, I wonder about about the usefulness of your example: isn't it
easier to just type your text directly rather than use a snippet to
insert the comma?
Stefan
|
The left-over comma could be handled with the regular The example is just a basic demonstration of the functionality, a more practical one would be a bunch of json fields. Other examples: css rules, html tags like list items, case statements. It is easier to just type without having to write and expand each snippet. One could have a regular snippet and the repeatable version and use the more convenient one for each use case.
|
The left-over comma could be handled with the regular `yas-after-exit-snippet-hook`.
That's not very satisfactory.
One could have a regular snippet and the repeatable version and use the
more convenient one for each use case.
This suggests that instead of having the "repeat" be a property of the
template, maybe we should have a command to repeat a(ny) template.
|
Yes I agree, but using
This seems like a better solution than the initial proposal. I looked for a "last snippet" variable in the yanippet code and could not find it. I think a possible implementation would be just adding it. (defvar yas-last-snippet nil)
(make-variable-buffer-local 'yas-last-snippet)
(defun yas-expand-snippet (snippet &optional start end expand-env)
;;; omited code
(let ((content (if (yas--template-p snippet)
(yas--template-content snippet)
snippet)))
(setq yas-last-snippet snippet)
)
(defun my/expand-last-snippet ()
(interactive)
(yas-expand-snippet yas-last-snippet)) |
> That's not very satisfactory.
Yes I agree, but using `yas-after-exit-snippet-hook` seems like the right
thing to do (cleanup work after exiting the snippet), maybe there is another
way I'm not familiar with.
Some alternatives: maybe a directive for `yas-after-exit-snippet-hook` , or
maybe a different hook that runs on a per snippet basis.
Maybe some annotation directly in the template marking the
comma as "insert only if this is not the last repetition".
> This suggests that instead of having the "repeat" be a property of the
> template, maybe we should have a command to repeat a(ny) template.
This seems like a better solution than the initial proposal.
I looked for a "last snippet" variable in the yanippet code and could not find it. I think a possible implementation would be just adding it.
``` emacs-lisp
(defvar yas-last-snippet nil)
(make-variable-buffer-local 'yas-last-snippet)
[ You can use `defvar-local` instead. ]
(defun yas-expand-snippet (snippet &optional start end expand-env)
;;; omited code
(let ((content (if (yas--template-p snippet)
(yas--template-content snippet)
snippet)))
(setq yas-last-snippet snippet)
)
(defun my/expand-last-snippet ()
(interactive)
(yas-expand-snippet yas-last-snippet))
```
That'd be simple, indeed.
But for the user that requires hitting a key for each repetition,
whereas your current approach requires hitting a key only for the
last repetition.
How do you start your repeated template?
Maybe we can provide a new command `yas-expand-repeatedly`?
Stefan
|
Thanks I was not aware of this.
I don't think that this would be a problem for most snippets. It is probably a bit annoying for simple snippets with one or two fields but would otherwise work fine.
In my approach I just use tab (or whatever yas-next-field is), using tab in the last field repeats the snippet.
Great!, I think this probably the best solution, and it would allow snippets already created to be repeatable. |
I'm currently using the following method to repeat snippets: a
yas-after-exit-snippet-hook
that usesyas-lookup-snippet
to find the current snippet and expand it. To exit the snippet I use the prefix argument C-u . Maybe something like this could be implemented in yasnippet with better syntax (a #repeat field).The text was updated successfully, but these errors were encountered: