Create Snippet with variables in Atom
Asked Answered
F

2

9

Is it possible to incorporate variables into snippets in Atom? This comes in handy with for loops for example, when you want to pre-fill spots that are about to come.

The snippets.cson entry with java as a source I would imagine. Unfortunately it is not working.

'.source.java':
    'For-Loop':
        'prefix': 'fori'
        'body': 'for (int ${1:VAR} = $2; ${VAR} < $3; ${VAR}++) {\n\t$3\n}'
Friederike answered 15/10, 2014 at 21:58 Comment(0)
M
10

Update 2016-09

This is now supported in Atom - please see the other answer for how to use it. I can't delete this answer since it is the accepted one...

Old Answer

The documentation for the snippets package has an example that shows how to predefine the default value for a variable:

'.source.js':
  'console.log':
    'prefix': 'log'
    'body': 'console.log(${1:"crash"});$2'

In the above example, crash is used as the default value for the log statement, allowing you to provide your own value by overwriting the crash default.

I guess what you're asking is whether there's a way to automatically use a value you typed and then apply it to the other instances of the same placeholder. So in your example, when you're at variable $1 and type foo, that it uses foo for all other occurrences of ${VAR}, right?

That's currently not possible from what I understand. I suggest you open a feature request at the snippets package's repo, or even better take a stab at adding this functionality and then creating a pull request.

Sorry if this doesn't help you, but you asked whether there was a way of doing this with the current version of Atom. The answer seems to be No at the present time.

Morava answered 20/10, 2014 at 13:36 Comment(4)
Well. At least I know. Thanks!Friederike
This is now supported.Conservatoire
Thanks for the update - I was not aware that this was added.Morava
FYI, for me ${1:"crash"} defaults to "crash" and not crash (ie, don't quote the default values unless you want those quotes). Also note that multiline snippets are possible using ''' and """.Docia
Z
13

Current version of Atom can support this kind of snippet. You can just put $1 at every places you want.

'.source.coffee':
  'For-Loop':
    'prefix': 'fori'
    'body': 'for (int ${1:i} = $2; ${1:i} < $3; ${1:i}++) {\n\t$4\n}'
Zinazinah answered 6/5, 2015 at 17:15 Comment(1)
As this is now supported, this is the right answer.Conservatoire
M
10

Update 2016-09

This is now supported in Atom - please see the other answer for how to use it. I can't delete this answer since it is the accepted one...

Old Answer

The documentation for the snippets package has an example that shows how to predefine the default value for a variable:

'.source.js':
  'console.log':
    'prefix': 'log'
    'body': 'console.log(${1:"crash"});$2'

In the above example, crash is used as the default value for the log statement, allowing you to provide your own value by overwriting the crash default.

I guess what you're asking is whether there's a way to automatically use a value you typed and then apply it to the other instances of the same placeholder. So in your example, when you're at variable $1 and type foo, that it uses foo for all other occurrences of ${VAR}, right?

That's currently not possible from what I understand. I suggest you open a feature request at the snippets package's repo, or even better take a stab at adding this functionality and then creating a pull request.

Sorry if this doesn't help you, but you asked whether there was a way of doing this with the current version of Atom. The answer seems to be No at the present time.

Morava answered 20/10, 2014 at 13:36 Comment(4)
Well. At least I know. Thanks!Friederike
This is now supported.Conservatoire
Thanks for the update - I was not aware that this was added.Morava
FYI, for me ${1:"crash"} defaults to "crash" and not crash (ie, don't quote the default values unless you want those quotes). Also note that multiline snippets are possible using ''' and """.Docia

© 2022 - 2024 — McMap. All rights reserved.