Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

regex - Make a vscode snippet that can use a variable number of arguments

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
"Class Initializer": {
  "prefix": "rci",
  "body": [

    "${1/([^,]+)([,\s]*|)/attr_reader :$1
/g}",    
    "${1/([^,]+)([,\s]*|)/# @param $1 [...]${2:+
}/g}",    
    "def initialize($1)",        
    "${1/([^,]+)((,\s*)|)/@$1 = $1${2:+
}/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 /g will get triggered 3 times if you have 3 method arguments.

You will see this ${2:+ } 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:

demo snippet of multiple arguments

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...