Create Snippet with variables in Atom

2020-07-06 08:15发布

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}'

2条回答
Lonely孤独者°
2楼-- · 2020-07-06 08:32

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.

查看更多
SAY GOODBYE
3楼-- · 2020-07-06 08:57

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}'
查看更多
登录 后发表回答