Sublime Forum

Is it possible to cycle through values in snippets?

#1

Let me elaborate… I would like to be able to have a some snippets for my front-end framework that merely output a variable. This in and of itself is easy, but in some cases I would like to be able to cycle multiple values because they are variables of a similar type - think Sass breakpoints, type sizes or colors…

For example, if the snippet for using one of the main colors has the below options:

  • $color-primary
  • $color-secondary
  • $color-tertiary

See this snippet to see how I currently handle this use case.

Are there any smart devs that know how to do this?

Many thanks

0 Likes

#2

have you considered using completions instead of snippets?

i.e. in a .sublime-completions file:

{
    "scope": "source.scss",

    "completions":
    [
        { "trigger": "color\tprimary", "contents": "\\$color-primary" },
        { "trigger": "color\tsecondary", "contents": "\\$color-secondary" },
        { "trigger": "color\thighlight", "contents": "\\$color-highlight" },
        { "trigger": "color\ttext", "contents": "\\$color-text" },
        { "trigger": "color\tbg", "contents": "\\$color-bg" },
    ]
}

you could then make use of the default replace_completion_with_next_completion keybinding on Tab so that when you type clr (unfortunately it won’t work if you type the full color by default, but I guess you could change the trigger if that is what you want) and press Tab, you could then press Tab again repeatedly to cycle through the completions. Of course, it has the disadvantage that your auto-completions list becomes bigger.

2 Likes