在特定文件/文件夹上运行VS Code任务,可通过下拉列表选择



我想弄清楚如何在一个特定的文件夹/文件上运行VS Code任务,该任务可以在下拉列表中选择。

的例子:

  1. 打开命令面板(Ctrl+Shift+P)
  2. 过滤'tasks' ->选择"任务:运行任务">
  3. 选择要运行的任务,例如:Cpplint
  4. NEW PART:现在不是马上执行它,应该打开一个下拉列表来选择你想要运行任务的文件夹/文件,或者你可以选择'all',它像以前一样工作,任务在所有文件夹上运行,从根(这里:src)文件夹开始。

结构:

ws
|   
+-- .env
|   |  
|   +-- ...
|    
+-- src
|  
+-- dir1
|   |
|   +-- file 1.1
|   +-- ... 
|   +-- file 1.n
+-- dir2
|   |
|   +-- file 2.1
|   +-- ... 
|   +-- file 2.n 
|   +-- dir2.2
|       |
|       +-- file 2.2.1
|       +-- ...
|       +-- file 2.2.n 
+-- ...
|
+-- dirn
|
+-- file n.1 
+-- ... 

有办法让这个工作吗?如果没有,是否有其他方法可以让类似的东西发挥作用?谢谢你的帮助。

进一步问题:
我试图在一个任务"Lint all"下同时运行多个测试程序。此任务依赖于另外两个任务。为了避免并行执行"Lint all"有" dependorder ";sequence",但不知何故下拉列表只显示第一个子任务。同样,只执行第一个子任务,然后停止执行。现在我有两个问题:

  1. 是否有办法获得每个子任务的下拉列表?
  2. 是否有一种方法可以获得下拉列表,只是为第一个子任务,然后下面的子任务记住输入并自动执行?
{
"version": "2.0.0",
"tasks": [
{
"label": "Lint all",
"detail": "Run all linters",
"dependsOrder": "sequence",
"dependsOn":["Cpplint", "Pylint"],
"problemMatcher": []
},
{
"label": "Cpplint",
"type": "shell",
"command": "cpplint --recursive ${input:selectDir}",
"problemMatcher": []
},
{
"label": "Pylint",
"type": "shell",
"command": "pylint ${input:selectDir}",
"problemMatcher": []
}
],
"inputs": [
{
"id": "selectDir",
"type": "command",
"command": "extension.commandvariable.pickStringRemember",
"args": {
"description": "Which directory to Lint?",
"options": [
["All", "src/"],
["Rerun task", "${remember:toRemember}"],
["Pick directory", "${pickFile:directory}"]
],
"default": null,
"pickFile": {
"directory": {
"description": "Which directory?",
"include": "src/**",
"showDirs": true,
"keyRemember":"toRemember"
}
}
}
}
]
}

您可以使用${input}变量

{
"version": "2.0.0",
"tasks": [
{
"label": "cpp lint",
"type": "shell",
"command": "cpplint ${input:selectDir}"
}
],
"inputs": [
{
"id": "selectDir",
"type": "pickString",
"description": "What directory to lint?",
"options": [
{"label": "All", "value": "" },
"dir1",
"dir2",
"dir3"
]
}
]
}

编辑

使用扩展命令变量v1.35.0,您可以获得目录的动态列表。

{
"version": "2.0.0",
"tasks": [
{
"label": "cpp lint",
"type": "shell",
"command": "cpplint ${input:selectDir}"
}
],
"inputs": [
{
"id": "selectDir",
"type": "command",
"command": "extension.commandvariable.pickStringRemember",
"args": {
"description": "Which directory to Lint for C++?",
"options": [
["Use previous directory", "${remember:srcSubDir}"],
["All", "all"],
["Pick directory", "${pickFile:srcSubDir}"]
],
"default": null,
"pickFile": {
"srcSubDir": {
"description": "Which directory?",
"include": "src/**/*.{cpp,h}",
"showDirs": true,
"keyRemember": "srcSubDir"
}
}
}
}
]
}

如果您选择src子目录,您将获得该目录的完整路径。我已经创建了一个问题,以获得相对于工作空间的结果。您可以使用extension.commandvariable.transform命令删除工作区文件夹部分。


编辑2

命令变量v1.35.1:

  • 修正了pickStringRemember中原始值的存储。现在存储变量替换后的值。
  • 修复了pickStringpickFile用户界面的转义以中止任务。
  • 在示例中为pickFile添加一个属性,将选中的文件存储在名称
  • 在示例中为pickString添加一个选项,以使用/记住之前的pickFile
  • pickString的示例中添加default属性以转义UI