0

You can set up a multiple selection in the VS Code explorer pane. Right-clicking on the selection fires a context menu and as you can see in the screen capture I have contributed a Print item to this menu.

enter image description here

The menu item contribution binds it to a command.

        "menus": {
            "explorer/context": [
                {
                    "command": "vsc-print.print",
                    "group": "navigation"
                }
            ],

Activation registers the command.

    context.subscriptions.push(vscode.commands.registerCommand("vsc-print.print", printCommand));

When the command executes this is called.

function printCommand(cmdArgs: any): PrintSession {
    logger.debug("Print command was invoked");
    const printSession = new PrintSession(cmdArgs, false);
    printSessions.set(printSession.sessionId, printSession);
    return printSession;
}

Contrary to expectation, cmdArgs is not an array of vscode.Uri. All you get is the vscode.Uri for the file under the right-click, highlighted by a blue border in the illustration above.

Documentation describes only the contribution of additional view containers. There is no mention of how to get my hooks into baked-in stuff. Can anyone tell me how to get the list of selected files?

Peter Wone
  • 17,965
  • 12
  • 82
  • 134

1 Answers1

1

See How to get the Multi Selected files from the File Explorer in an extension command for more. Adding an answer here mainly for the typescript addition.

Try this:

function printCommand(...cmdArgs: [vscode.Uri, vscode.Uri[]]): PrintSession {

// or

function printCommand(contextSelection: vscode.Uri, allSelections: vscode.Uri[]): PrintSession {

Using the spread operator ... will cause the cmdArgs to collect all passed arguments into an unusual array of the Explorer item you right-clicked on and all the files that were highlighted - including the file in the first array position.

Upon further examination @PeterWone indicated that the first version ...cmdArgs can be resolved to the second version contextSelection: vscode.Uri, allSelections: vscode.Uri[].

I gave them those names in an attempt to explain what they are. The first argument contextSelection is the file, actually the vscode.Uri, that you right-clicked on to bring up the context menu where your extension command is located.

The second argument allSelections is an array of vscode.Uri's that does include the item you right-clicked on and any other files that might have been selected at the time. Interestingly, there does appear to be a specific order to that array. It is in the order of the selections made - so the first item in the array could be below or above the next file selected in the Explorer. The first item selected will appear first in the allSelections array, and so on. I don't know how often that the order will typically be of much use to the extension author...

In my testing it looks like cmdArgs will be typed as [vscode.Uri, vscode.Uri[]] if you care to move away from the any.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 1
    I figured this out and was coming back to write an answer, but you beat me to it. A signature of `(cmdArgs: vscode.Uri, multiselection: Array)` also works. With the weird nomenclature he's chosen ("Multi Selected") I'm not surprised I didn't find that. – Peter Wone Feb 23 '23 at 07:15
  • I'm going to add your findings to the answer, plus a little more I learned on testing. – Mark Feb 26 '23 at 23:27