有没有一种方法可以对VS Code任务运行扩展命令?

lep*_*ott 6 visual-studio-code vscode-tasks

在VS Code中,我有一个扩展,可以通过按F1并按名称搜索来运行其命令。但是我想从任务(tasks.json)自动运行它。我从键盘快捷键知道它的全名。

sou*_*ned 4

tasks.json您可以使用以下语法运行有效命令${command:}

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "command": "${command:editor.action.addCommentLine}",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

上面的例子注释掉了当前行

复合命令

如果您想顺序/并行运行命令,可以使用该dependsOn属性来运行多个命令:

表示另一个任务的字符串或该任务所依赖的其他任务的数组。

例如,假设有一种情况,您想要复制当前所在的行,将其注释掉,然后出于演示目的,立即将终端聚焦:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "create temporary duplicate line",
            "command": "${command:workbench.action.terminal.focus}",
            "dependsOn": [
                "duplicate line up",
                "comment line out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "comment line out",
            "command": "${command:editor.action.addCommentLine}"
        },
        {
            "label": "duplicate line up",
            "command": "${command:editor.action.copyLinesUpAction}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

假设您复制的行是:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "command": "${command:editor.action.addCommentLine}",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

该任务将运行并执行以下操作:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "create temporary duplicate line",
            "command": "${command:workbench.action.terminal.focus}",
            "dependsOn": [
                "duplicate line up",
                "comment line out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "comment line out",
            "command": "${command:editor.action.addCommentLine}"
        },
        {
            "label": "duplicate line up",
            "command": "${command:editor.action.copyLinesUpAction}"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后重点关注综合终端


您可以查看有关命令变量和其他占位符语法的文档,例如接受用户输入以创建动态任务,或使用该runOn属性在文件夹启动时自动运行任务