在保存时运行扩展命令

时间:2020-09-15 19:45:22

标签: visual-studio-code

我使用排序HTML和Jade属性扩展名,该扩展名提供命令 attrsSorter.execute 。我想设置我的工作区settings.json,以便“ editor.codeActionsOnSave”部分在保存之前每次都运行此命令,然后进行格式化/整理,以便我们获得排序的好处。

到处都看过,无法弄清楚每次执行保存时如何简单地执行此操作。感谢帮助!

2 个答案:

答案 0 :(得分:0)

You can use this extension

在保存时运行Visual Studio代码

此扩展程序允许配置命令,以在将文件保存在vscode中时运行。

答案 1 :(得分:0)

我只是失去了我的生活方式太多时间搞清楚如何运行命令test-explorer.run-all通过定义this plugin。如果您将 editor.codeActionsOnSave 替换为以下所有位置的 test-explorer.run-all,我认为应该可行。

我一起破解的是首先安装 Trigger Task on Save extension 它不是按照其他答案运行 bash 命令,而是在保存时运行 VS 代码任务。

根据the VS code docs 然后,您可以定义一个自定义任务来包装命令。对我来说,这看起来像

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "mocha-explorer",
      "command": "${command:test-explorer.run-all}",
    }
  ]
}

这里的 mocha-explorer 是我自己的任意标签。 最后,配置 settings.json 文件以通过其标签在保存时运行您的任务,对我来说看起来像:

    "triggerTaskOnSave.tasks": {
        "mocha-explorer": ["*.js"]
    }

我是所有这些 VS 代码配置的新手,但我希望对某人有所帮助。

可能对更复杂的任务有帮助的相关有用答案:Is there a way to run a command with VS Code tasks?