1

I'm trying to write a snippet to make case-style statements in JavaScript quicker. Right now I have this, and it works:

    "long if-else": {
        "scope": "javascript,typescript,html",
        "prefix": "ie",
        "body": [
            "if ( $2 ) {\n\n} ${1/(.)/else if ( ) {\n\n} /g}else {\n\n}\n"
        ]
    }

it allows you to insert 0 or more else if statements in between the opening if and closing else by entering n characters after first tab and hitting tab again.

I would like the user (me) to be able to specify a value to go inside the brackets (like myVar =). I've tried setting up a variable to be evaluated after the transform, but it hasn't been read as a variable.

4

2 回答 2

2

您不能在转换的替换部分中放置制表位或其他变量。

如果您希望每个 if/else 块包含不同的变量,则必须使用不同的方法,例如:

if (myVar == ) {

} else if (myVar == a) {

} else if (myVar == b) {

} else if (myVar == c) {

} else if (myVar == d) {

} else {

}

在生成每个 else if 块之前,您必须先列出这些变量。试试这个片段:

"long if-else": {
  "scope": "javascript,typescript,html",
  "prefix": "ie",
  "body": [

    "if (myVar = $2) {\n",

    "${1/(\\w+)(,\\s*|\\b)/} else if (myVar = $1) {${2:?\n\n:\n}/g}",

    "} else {",
    "",
    "}",
    "$3"
  ]
},

这是一个演示,看看如何输入变量:

在此处输入图像描述


您输入每个 myVar 作为逗号分隔的列表,然后Tab. 尽管没有更多复杂情况,但它不能处理零情况。

于 2020-04-05T22:44:19.810 回答
0

我不太确定这就是你正在寻找的(可能是)这样的

"long if-else": {
    "scope": "javascript,typescript,html",
    "prefix": "ie",
    "body": [
        "if ( ${myVar} ) {\n\n} ${1/(.)/else if ( ) {\n\n} /g}else {\n\n}\n"
    ]
}

使用这种方式,用户(您)将拥有类似的东西

if ( myVar ) {

 } else {

 }
于 2020-04-05T19:09:39.870 回答