1

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?

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 1
    I updated my answer to show a way to do what you want using the other `compare` commands. – Mark Aug 24 '22 at 16:05

1 Answers1

2

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).

Mark
  • 143,421
  • 24
  • 428
  • 436
  • thank you very much, this works fine! Interesting to see that `selectForCompare` and `compareFiles` are not prefixed with `workbench.action...` or similar. The command for "compareWith" was `workbench.files.action.compareFileWith` for example. This seems arbitrary for me on first glance... – PeterUstinox Aug 24 '22 at 20:59
  • 1
    Yes, there are a small number of such commands, maybe old ones that were implemented before a more systematic naming scheme was developed... – Mark Aug 24 '22 at 22:39