json - 宏扩展不同步运行命令
问题描述
我想做一个快捷方式,可以在 VScode 中的行的 72 位置插入当前日期。我先让光标转到 Pos 72 并使用扩展来获取当前日期。但是,自定义扩展没有等待光标移动,日期出现在当前位置。宏运行时似乎发生了异步。这是我的代码
"addDate": [
"cursorLineEnd",
{"command": "type", "args": {"text": " "}},
"cursorLineStart",
{"command": "cursorMove", "args": {"to": "right", "by": "character", "value": 72}},
{"command": "type", "args": {"text": "AD"}},
"editor.action.trimTrailingWhitespace",
{"command": "insertDateString.insertDate"},
]
{"command": "insertDateString.insertDate"},
没有等待 cursorMove 完成并直接工作。有没有像“promise...then”这样的方法或者让PG按顺序运行的优先级设置?谢谢
解决方案
我建议使用宏命令multi-command。它正确处理同步命令。因此,使用多命令,将其放入您的 settings.josn:
"multiCommand.commands": [
{
"command": "multiCommand.addDate",
"sequence": [
"cursorLineEnd",
{
"command": "type",
"args": { "text": " "
}
},
"cursorLineStart",
{"command": "cursorMove", "args": {"to": "right", "by": "character", "value": 72}},
{"command": "type", "args": {"text": "AD"}},
"editor.action.trimTrailingWhitespace",
{"command": "insertDateString.insertDate"}
]
}
]
然后你的键绑定看起来像这样:
{
"key": "alt+d", // whatever you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.addDate" },
// "when": "editorTextFocus"
},
它按预期工作。
推荐阅读
- php - 使用简码在任何地方显示 WooCommerce 优惠券输入字段
- sap - SAP PowerDesigner - 使用 SQL 更改模型
- linux - 在 linux 中使用 display 命令显示所有 .png 图像
- python - ValueError:字典更新序列元素 #0 的长度为 28;2 是必需的
- arm - GPU ARM Mali 和 OpenCL 驱动程序
- javascript - 将Javascript计算输出到html
- javascript - React:如何对排序数据进行分页
- javascript - JS setTimeout 没有延迟
- angular - Angular 6:如何获取响应状态码
- python - jupyterlab 不显示 png 图像