I was faced with the same issue, so I wrote a small example extension. You can find the sample code on https://github.com/GianUlli/vscode-custom-command-input-example.
The basic idea is to register a command that returns a string. Following the official VS Code guide, you start by registering the command when the extension is activated in extension.ts
in the activate
function:
let disposable = vscode.commands.registerCommand(
'vscode-custom-command-input-example.pickTestCase',
async () => {
return pickTestCase().catch(console.error);
}
);
context.subscriptions.push(disposable);
pickTestCase
is the custom function that extracts all test cases from the workspace using a glob search.
export async function pickTestCase() {
let workingFolder = getWorkspaceFolder();
return new Promise<string[]>((resolve, reject) => {
glob('*.json', { cwd: workingFolder }, (err, files) => {
if (err) {
return reject(err);
}
resolve(files);
});
})
.then((files) => {
if (files.length > 0) {
return window.showQuickPick(files);
} else {
window.showErrorMessage('Could not find any test cases.');
return undefined;
}
})
.then((testCase) => {
return testCase;
});
}
Do not forget to register the command in package.json
:
"contributes": {
"commands": [
{
"command": "vscode-custom-command-input-example.pickTestCase",
"title": "Pick a test case"
}
]
}
With those pieces, the user is now able to put ${command:vscode-custom-command-input-example.pickTestCase}
in his task definitions (.vscode/tasks.json
) and launch configurations (.vscode/launch.json
), for example in a build task:
{
"label": "Echo example",
"command": "echo",
"type": "shell",
"args": ["${command:vscode-custom-command-input-example.pickTestCase}"],
"presentation": {
"reveal": "always"
},
"group": "build"
}