2

在较新版本的 VSCode 中,我可以创建绑定到我在tasks.json. 例如,下面的 3 个任务

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Clean",
      "type": "shell",
      "command": "clean.cmd",
      "problemMatcher": []
    },
    {
      "label": "Build",
      "type": "shell",
      "command": "build.cmd",
      "problemMatcher": [],
      "group": { "kind": "build", "isDefault": true }
    },
    {
      "label": "Flash",
      "type": "shell",
      "command": "flash.cmd",
      "problemMatcher": []
    }
  ]
}

我可以创建键绑定

[
  {
    "key": "alt+f9",
    "command": "workbench.action.tasks.runTask",
    "args": "Clean"
  },
  {
    "key": "ctrl+f9",
    "command": "workbench.action.tasks.build"
  },
  {
    "key": "f9",
    "command": "workbench.action.tasks.runTask",
    "args": "Flash"
  }
]  

一切都按预期工作。我正在尝试使用 VSCode API 从扩展(例如使用Script Commands)中做同样的事情,但它仅适用于buildandtest任务。

打电话

vscode.commands.executeCommand("workbench.action.tasks.build")

有效,但

vscode.commands.executeCommand("workbench.action.tasks.runTask", ["Clean"])

打开任务选择列表。

除了使用 JavaScript 代码之外build,我如何才能直接启动其他任务?test

4

1 回答 1

0

您的任务配置文件看起来很旧。如果您使用的是新版本的 Visual Studio Code,那么您应该升级。

我认为 api 期望任务有一个“标签”字段。

https://code.visualstudio.com/docs/editor/tasks#_convert-from-010-to-200

所以在以下方面:

{
   "taskName": "Clean",
   "type": "shell",
   "command": "clean.cmd",
   "problemMatcher": []
},

它应该是:

{
  "label": "Clean",
  "type": "shell",
  "command": "clean.cmd",
  "problemMatcher": []
},

更新:尝试仅使用字符串调用它,而不是将其包装在这样的数组中:

vscode.commands.executeCommand("workbench.action.tasks.runTask", "Clean")
于 2017-11-15T20:03:52.093 回答