visual-studio-code - multiple if/else statements with different variables: vscode snippet
问题描述
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.
解决方案
您不能在转换的替换部分中放置制表位或其他变量。
如果您希望每个 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. 尽管没有更多复杂情况,但它不能处理零情况。
推荐阅读
- python - 使用 Pyspark 将巨大的 csv 文件加载到 oracle 数据库表中
- confluent-platform - 如何在融合 6.2.0 中运行 kafka s3 sink 连接器
- docker-compose - 使用 `links` 指令迁移 docker-compose 文件
- reactjs - NX React - 如何为库创建默认模拟
- python - switch/toogle 导致 BadRequestKeyError: 400 Bad Request
- qt - 如何获得带有枚举的 QDialog 的子项(访问按钮)?
- c# - 具有用户定义表类型的 SQL Server 存储过程
- hive - 查找值是否属于 impala 中的一组值
- swift - 如何更改 UIButton 的背景颜色?
- linux - Azure 总线服务队列消息处理