Home > OS >  vscode extension: Can you pass a value when executing a command which opens a text input?
vscode extension: Can you pass a value when executing a command which opens a text input?

Time:06-21

I am trying to write a little helper extension to ease the creation of feature branches. I already have a view of recently active work items and a button to create a feature branch from a specific item. The name is already being formatted correctly, but I do not want to be responsible for the branch creation logic when the functionality is already built into vscode.

My first attempt was to simply call executeCommand and pass in the command and requested branch name like this:

vscode.commands.executeCommand("git.branch", branchName);

Sadly the branchName parameter is simply being ignored and a text input appears just as it would when executing the command via the command palette. The easy solution would be to simply steal borrow the branch creation logic or make a cli call, but I am stubborn and would like to avoid doing that if possible.

I can barely find any information regarding this topic anywhere.

https://code.visualstudio.com/api/references/commands does not cover git commands

https://code.visualstudio.com/api/extension-guides/command#command-uris covers passing parameters to commands via uris, but this too does not work for me

I would appreciate any leads.

CodePudding user response:

Tou can avoid the InputBox opening by using the API that the git extension exposes, see git extension api.

The Git extension exposes an API, reachable by any other extension.

Copy src/api/git.d.ts to your extension's sources;

Include git.d.ts in your extension's compilation.

Get a hold of the API with the following snippet:

const gitExtension = vscode.extensions.getExtension('vscode.git').exports;
const git = gitExtension.getAPI(1);

The above is from the extension description for the @builtin git extension description.

So you could use:

const branchName = 'test';

const gitExtension = await vscode.extensions.getExtension('vscode.git').exports;

const gitAPI = await gitExtension.getAPI(1);

// these two calls below are not working for me??  whether I use .git extension or not
// const repoURI = await gitAPI.openRepository(vscode.Uri.file('https://github.com/Fred/aRepository.git'));
// const repoURI = gitAPI.getRepository(vscode.Uri.file('https://github.com/Fred/aRepository.git'));

// const repo = await vscode.commands.executeCommand('git.openRepository', vscode.Uri.file('https://github.com/Fred/aRepository.git'));

// just hardcodes the first open repository, the boolean is whether to checkout
await gitAPI.repositories[0].createBranch(branchName, true);

This works for me. But you should be able to API.getRepository(uri) or await API.openRepository(uri) according to the API link above. Perhaps the problem is related to Git API call getRepository(uri) returns null on any Uri that is not opened in the workspace or maybe it just won't work in the Extension Development Host - I don't know.

Maybe someone can help on this point of using getRepository - it always returns null for me even though the Extension Development Host is opening a workspace with a repository associated with it.

CodePudding user response:

It seems the git.branch command does not support parameters, as you can see here

https://github.com/microsoft/vscode/blob/10e6e876822fa2da15fb68e8a6f1fb9aa8d94ce2/extensions/git/src/commands.ts#L1884-L1887

In this case, I suggest you should to open an issue in VS Code repo and ask to the VS Code team to expand the command, to support such scenario.

They recently added a random branch name feature, so I suppose such request would be well received.

Hope this helps

  • Related