Home > Back-end >  How to avoid opening a QuickInput when choosing files to compare in a vscode extension?
How to avoid opening a QuickInput when choosing files to compare in a vscode extension?

Time:08-25

In my case I want to compare two files. But I don't want the user to select the File in the QuickInput form, i want to choose this directly for him.

vscode.commands.executeCommand("workbench.files.action.compareFileWith", filePath)

This results in

VSCode Quickpick

Meaning that filePath is ignored and a QuickInput is displayed instead. Is there a way to directly select a file programmatically instead of showing the QuickInput first?

CodePudding user response:

While the compareFileWith command probably requires a QuickInput panel to use, you can use the other compare commands to do what you want:

// using the current file for the 'compareWith' file
const currentFileUri = vscode.window.activeTextEditor.document.uri;

// create a Uri from some filePath
const compareWithSelectedUri = vscode.Uri.file('C:\\Users\\Mark\\OneDrive\\Test Bed\\BAR.txt');

await vscode.commands.executeCommand('selectForCompare', currentFileUri)
await vscode.commands.executeCommand('compareFiles', compareWithSelectedUri);

This works in my testing.


Looking at compareFileWith in https://github.com/microsoft/vscode/blob/9b9361cfd1b0678f0bb0b32bf9925b6520bb9926/src/vs/workbench/contrib/files/browser/fileActions.ts I don't think there is any way to avoid the QuickInput opening.

Alternatively, what you are asking for would be "easy" if an open method were supported on TabGroups api like the close methods. You would create a tab of kind TabInputTextDiff with an original uri and a modifieed uri.

When the TabGroups api was being developed there was an open tab method but it was removed prior to release and hasn't seen any love since. See https://github.com/microsoft/vscode/commit/aa69f3d7623c464aba726d12ea0d83428f43e8b9#commitcomment-71831337.

I'll open an issue to see if it will help (and post the link here later).

  • Related