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

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

问题描述

是否可以将变量合并到 Atom 的片段中?例如,当您想要预填充即将出现的位置时,这对 for 循环非常有用.

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.

snippets.cson 条目以 java 作为我想象的源.不幸的是它不起作用.

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}++) {
	$3
}'

推荐答案

更新 2016-09

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

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...

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

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'

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

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.

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

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.

很抱歉,如果这对您没有帮助,但您询问是否有办法使用当前版本的 Atom 执行此操作.目前的答案似乎是.

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天全站免登陆