制作一个可以使用可变数量的参数的vscode代码段 [英] Make a vscode snippet that can use a variable number of arguments

查看:147
本文介绍了制作一个可以使用可变数量的参数的vscode代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VSCode的新手。考虑到代码片段,我环顾四周,寻找一种在该片段内编写脚本的方法。我的意思是要做的不仅仅是填充或转换变量。例如...



这是一个简单的代码段。我要为类初始化程序键入 rci 。当我输入方法参数时,我希望赋值和文档以及其他一些事情发生。



rci< tab> 然后 def初始化(a,b))会得到类似这样的结果...

  attr_reader:a 
attr_reader:b

#@param a [...] ...
# @param b [...] ...
def initialize(a,b)
@a = a
@b = b
结束

有可能吗?如何实现?可以有任意数量的参数。每个参数都将触发类初始化器的另一行。

解决方案

 类初始化器 :{{
prefix: rci,
body:[

$ {1 /([^,] +)([,\\ s] * |)/ attr_reader:$ 1\n / g},
$ {1 /([^,] +)([,\\s] * |)/#@param $ 1 [...] $ {2:+ \n} / g},
def initialize($ 1),
$ {1 /([^,] +)((, \\s *)|)/ \t @ $ 1 = $ 1 $ {2:+ \n} / g},
end
],

description: Initialize Class
}

获取密码的关键任何数量的方法参数的工作都是将它们放入相同的正则表达式捕获组



然后,设置了全局标志,每个捕获组将触发替换文本。因此,例如,如果您有3个方法参数, / attr_reader:$ 1\n / g 将被触发3次。



您将在上面的转换中看到此 $ {2:+ \n} 。这意味着如果有捕获组2,请添加换行符。正则表达式的设计是,如果参数之间存在另一个,则仅存在捕获组2。因此,最后一个参数之后的最后一个不会触发另一个换行符-因此输出与换行符的输出完全匹配(但您可以轻松添加或删除换行符)。 / p>

您的输入必须采用正确的格式:



v1,v2,v3



这是一个演示:





因此必要的形式只是 v1 v2 v3 。参数之间不需要有空格,但是您会得到 def initialize(v1,v2,v3)也不带空格。



在最后一个参数触发后点击 Tab



事实证明,摘要非常强大!



有关使用多个参数的类似问题,请参见 VSCode片段:将多个对象添加到类构造器中


I am new to VSCode. Thinking about code snippets, I looked around for a way to kind of script inside the snippet. I mean to do more than just fill or transform a variable. For example...

This is a simple snippet. I am going to type rci for the class initializer. When I enter the method arguments I would like the assignment and documentation + some other things to happen.

rci<tab> and then def initialize(a, b)) to result in something like this...

attr_reader :a
attr_reader :b

# @param a [...] ...
# @param b [...] ...
def initialize(a, b)
  @a = a
  @b = b
end

Is it possible? How can it be achieved? There could be any number of arguments. And each argument would trigger another line of the class initializer.

解决方案

"Class Initializer": {
  "prefix": "rci",
  "body": [

    "${1/([^,]+)([,\\s]*|)/attr_reader :$1\n/g}",    
    "${1/([^,]+)([,\\s]*|)/# @param $1 [...]${2:+\n}/g}",    
    "def initialize($1)",        
    "${1/([^,]+)((,\\s*)|)/\t@$1 = $1${2:+\n}/g}",
    "end"
  ],

  "description": "Initialize Class"
}

The key to get it to work for any number of method arguments is to get them into the same regex capture group.

Then, with the global flag set, each capture group will trigger the replacement text. So for instance, /attr_reader :$1\n/g will get triggered 3 times if you have 3 method arguments.

You will see this ${2:+\n} in the transforms above. That means if there is a capture group 2, add a newline. The regex is designed so that there is only a capture group 2 if there is another , between arguments. So a final ) after the last argument will not trigger another newline - so the output exactly matches your desired output as to newlines (but you could easily add or remove newlines).

Your input must be in the correct form:

v1, v2, v3

Here is a demo:

So again the necessary form is just v1 v2 v3. There doesn't need to be a space between the arguments but then you would get def initialize(v1,v2,v3) without spaces either.

Hit Tab after the final argument to trigger completion.

It turns out snippets are pretty powerful!!

For a similar question about using multiple arguments, see VSCode snippet: add multiple objects to a class constructor

这篇关于制作一个可以使用可变数量的参数的vscode代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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