gpt4 book ai didi

vscode-tasks - 有没有办法在 VS Code 任务中动态填充 pickString?

转载 作者:行者123 更新时间:2023-12-03 15:14:16 24 4
gpt4 key购买 nike

我想提供一个字符串列表作为任务的pickString。字符串列表将是文件夹名称列表,我可以从 PowerShell 获取,但我不确定如何在任务中显示此列表。

如何设置我的任务和输入以便可以填充此列表?

{
"version": "2.0.0",
"tasks": [
{
"label": "Test Task",
"type": "shell",
"windows": {
"command": "echo",
"args": [
"-opt",
"${input:optionsList}"
]
}
}
],
"inputs": [
"id": "optionsList",
"type": "pickString",
"options": [<insert something here>]
]
}

我希望用户在任务运行时查看文件夹列表。

最佳答案

Task/Launch Input Variables显示了一种使用 command input variable 执行此操作的方法.该命令可以来自 vscode 的内置命令或扩展。在您的情况下,没有内置命令可返回给定工作区中的文件夹列表。但是在扩展中执行它是相当简单的。该文档没有给出如何执行此操作的完整示例,因此我将在此处展示一个示例。首先是它的演示到 ls任务中选定的文件夹:
Using an extension command to populate a pickString variable .
这是整个tasks.json文件:


{
"version": "2.0.0",
"tasks": [

{
"label": "List Folder Choice files",
"type": "shell",
"command": "ls", // your command here
"args": [
"${input:pickTestDemo}" // wait for the input by "id" below
],
"problemMatcher": []
}
],

"inputs": [
{
"id": "pickTestDemo",
"type": "command",
"command": "folder-operations.getFoldersInWorkspace" // returns a QuickPick element
},
]
}
您可以在输入变量中看到从 folder-operations 调用的命令。延期。由于该命令返回 QuickPick运行任务时将看到的元素。
这是扩展的内容:
    const vscode = require('vscode');
const fs = require('fs');
const path = require('path');

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {

let disposable = vscode.commands.registerCommand('folder-operations.getFoldersInWorkspace', async function () {

// get the workspaceFolder of the current file, check if multiple workspaceFolders
// a file must be opened
const wsFolders = await vscode.workspace.workspaceFolders;
if (!wsFolders) vscode.window.showErrorMessage('There is no workspacefolder open.')
const currentWorkSpace = await vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri);

// filter out files, keep folder names. Returns an array of string.
// no attempt made here to handle symbolic links for example - look at lstatSync if necessary

const allFilesFolders = fs.readdirSync(currentWorkSpace.uri.fsPath);
const onlyFolders = allFilesFolders.filter(f => fs.statSync(path.join(currentWorkSpace.uri.fsPath, f)).isDirectory());

// showQuickPick() takes an array of strings
return vscode.window.showQuickPick(onlyFolders);
});

context.subscriptions.push(disposable);
}

exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
activate,
deactivate
}
这是 folder-operations 的链接演示扩展,因此您可以查看完整的扩展代码及其 package.json。设置好扩展发布凭据后,发布更多内容真的很容易。

关于vscode-tasks - 有没有办法在 VS Code 任务中动态填充 pickString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57977832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com