0

In the VSCode documentation for variable substitution, it says there is now a way of using an extension to generate a list of options. Specifically, it gives an example and then says "It is assumed that some extension provides an extension.mochaSupport.testPicker command...".

I accept there is help provided in how to get started with extensions of various kinds, but I was wondering if anyone had indeed already written something equivalent to extension.mochaSupport.testPicker?

I appreciate it's probably trivial for someone not starting from scratch, but the graphic simulation on the first page implies someone has indeed already written such an extension (to generate the graphic), so my question is "does anyone know of an extension that is documented anywhere that complements this example to select an input from a list?"

  • Are you referring to inputs to a task in `tasks.json`? If you are, you can use them as a parameter like this: `${input:pickFromList}`, and define an `"input"` of type `pickString`. Frankly, I've found the documentation for Visual Studio to be fairly sparse and hard to use, so I feel your pain. – AlainD Nov 08 '19 at 13:35
  • Thanks for your feedback/comment. I've got the scenario you describe working ok (in another case), but the difference here is that I want to build the list of options _dynamically_ (using a bespoke algorithm), rather than creating one big list of all possible entries. – Steve Adcock Nov 11 '19 at 10:42

1 Answers1

2

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"
}
Gian U.
  • 739
  • 4
  • 14
  • 1
    Many thanks for this answer! Subsequent to writing this original question, it would appear a request went in to get a [VSCode modification to do this natively](https://github.com/microsoft/vscode/issues/109789), which appears to have been rejected because it wasn't deemed necessary. However, there is an [another independent question](https://stackoverflow.com/questions/57977832/is-there-a-way-to-get-a-pickstring-dynamically-populated-in-a-vs-code-task) with a solution and [VSCode's tasks documentation](https://code.visualstudio.com/docs/editor/tasks) has been updated. – Steve Adcock Oct 19 '21 at 09:06