具有不同变量的多个if / else语句:vscode代码段

问题描述 投票:1回答:2

我正在尝试编写一个片段,以更快地在JavaScript中创建大小写样式的语句。现在我有了这个,它可以工作:

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

通过在第一个选项卡后输入n个字符并再次点击选项卡,您可以在开头的if和结束else之间插入0或多个else if语句。

我希望用户(me)能够指定要放在方括号内的值(例如myVar =)。我尝试设置一个要在转换后求值的变量,但尚未将其作为变量读取。

visual-studio-code vscode-settings code-snippets vscode-snippets
2个回答
1
投票

您不能将Tabstop或另一个变量放在转换的替换部分内。

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

if (myVar == ) {

} else if (myVar == a) {

} else if (myVar == b) {

} else if (myVar == c) {

} else if (myVar == d) {

} 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"
  ]
},

这里是一个演示,以了解如何输入变量:

enter image description here


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


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 {

 }
© www.soinside.com 2019 - 2024. All rights reserved.