Coder Social home page Coder Social logo

Comments (8)

monnier avatar monnier commented on June 11, 2024

Could you clarify what "better syntax" you're thinking of?

from yasnippet.

MirkoHernandez avatar MirkoHernandez commented on June 11, 2024

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.

# -*- mode: snippet -*-
# name:parameters 
# key: p 
# repeat: t (or a number)
# --
"$1, "

from yasnippet.

monnier avatar monnier commented on June 11, 2024

from yasnippet.

MirkoHernandez avatar MirkoHernandez commented on June 11, 2024

The left-over comma could be handled with the regular yas-after-exit-snippet-hook.

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.

# -*- mode: snippet -*-
# name:json-fields 
# key: jf
# repeat: t (or a number)
# --
"$1 ":  $2, 

from yasnippet.

monnier avatar monnier commented on June 11, 2024

from yasnippet.

MirkoHernandez avatar MirkoHernandez commented on June 11, 2024

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.

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.

(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))

from yasnippet.

monnier avatar monnier commented on June 11, 2024

from yasnippet.

MirkoHernandez avatar MirkoHernandez commented on June 11, 2024

[ You can use defvar-local instead. ]

Thanks I was not aware of this.

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.

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.

How do you start your repeated template?

In my approach I just use tab (or whatever yas-next-field is), using tab in the last field repeats the snippet.
The problem is how to stop repeating the snippet, I use the prefix argument C-u which is kind of cumbersome, I tried yas-abort-snippet but it does not work, I believe it uses the exit hook.

Maybe we can provide a new command yas-expand-repeatedly?

Great!, I think this probably the best solution, and it would allow snippets already created to be repeatable.
I still think the previous expand-last command is useful for a different use case.

from yasnippet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.