在Atom中使用变量创建代码段 [英] Create Snippet with variables in Atom

查看:89
本文介绍了在Atom中使用变量创建代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将变量合并到Atom的代码片段中?例如,当您要预填充即将出现的斑点时,这对于for循环很有用.

我想像的以Java作为源的snippets.cson条目.不幸的是,它无法正常工作.

'.source.java':
    'For-Loop':
        'prefix': 'fori'
        'body': 'for (int ${1:VAR} = $2; ${VAR} < $3; ${VAR}++) {\n\t$3\n}'

解决方案

更新2016-09

Atom现在支持此功能-请参阅其他答案以了解如何使用它.我无法删除此答案,因为它是可接受的答案...

旧答案

snippets包的文档中有一个示例,展示了如何预定义变量的默认值:

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

在上面的示例中, crash 用作log语句的默认值,允许您通过覆盖 crash 的默认值来提供自己的值.

我想您要问的是,是否有一种方法可以自动使用您键入的值,然后将其应用于同一占位符的其他实例.因此,在您的示例中,当您使用变量$1并键入foo时,它会将foo用于所有其他出现的${VAR},对吧?

据我所知,目前尚无法实现.我建议您在摘要程序包的仓库中打开功能请求,或者甚至最好在添加此功能后创建一个请求请求.

很抱歉,这是否无济于事,但您询问是否可以使用当前版本的Atom来实现此目的.答案目前看来是.

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

解决方案

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.

这篇关于在Atom中使用变量创建代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆