首页 > 解决方案 > 宏扩展不同步运行命令

问题描述

我想做一个快捷方式,可以在 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按顺序运行的优先级设置?谢谢

标签: jsonvscode-settings

解决方案


我建议使用宏命令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"
  },

它按预期工作。


推荐阅读