Skip to content

Commit 5fa87f9

Browse files
authored
SCM - worktree context menu improvements (microsoft#257242)
1 parent 226f43e commit 5fa87f9

File tree

8 files changed

+63
-26
lines changed

8 files changed

+63
-26
lines changed

extensions/git/package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,14 @@
13351335
"command": "git.deleteWorktree",
13361336
"when": "false"
13371337
},
1338+
{
1339+
"command": "git.openWorktree",
1340+
"when": "false"
1341+
},
1342+
{
1343+
"command": "git.openWorktreeInNewWindow",
1344+
"when": "false"
1345+
},
13381346
{
13391347
"command": "git.deleteWorktreeFromPalette",
13401348
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount > 1"
@@ -1690,13 +1698,13 @@
16901698
},
16911699
{
16921700
"command": "git.openWorktree",
1693-
"group": "worktree@1",
1694-
"when": "scmProvider == git && gitOpenRepositoryCount > 1"
1701+
"group": "1_worktree@1",
1702+
"when": "scmProvider == git && scmProviderContext == worktree"
16951703
},
16961704
{
16971705
"command": "git.openWorktreeInNewWindow",
1698-
"group": "worktree@2",
1699-
"when": "scmProvider == git && gitOpenRepositoryCount > 1"
1706+
"group": "1_worktree@2",
1707+
"when": "scmProvider == git && scmProviderContext == worktree"
17001708
}
17011709
],
17021710
"scm/resourceGroup/context": [

extensions/git/src/commands.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,9 +3482,8 @@ export class CommandCenter {
34823482
}
34833483

34843484
@command('git.openWorktree', { repository: true })
3485-
async openWorktreeInCurrentWindow(repository: Repository, ...args: SourceControl[]): Promise<void> {
3486-
// If multiple repositories are selected, no action is taken
3487-
if (args.length > 0) {
3485+
async openWorktreeInCurrentWindow(repository: Repository): Promise<void> {
3486+
if (!repository) {
34883487
return;
34893488
}
34903489

@@ -3493,9 +3492,8 @@ export class CommandCenter {
34933492
}
34943493

34953494
@command('git.openWorktreeInNewWindow', { repository: true })
3496-
async openWorktreeInNewWindow(repository: Repository, ...args: SourceControl[]): Promise<void> {
3497-
// If multiple repositories are selected, no action is taken
3498-
if (args.length > 0) {
3495+
async openWorktreeInNewWindow(repository: Repository): Promise<void> {
3496+
if (!repository) {
34993497
return;
35003498
}
35013499

src/vs/workbench/contrib/scm/browser/menus.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,10 @@ export class SCMRepositoryMenus implements ISCMRepositoryMenus, IDisposable {
180180
private genericRepositoryMenu: IMenu | undefined;
181181
private contextualRepositoryMenus: Map<string /* contextValue */, IContextualMenuItem> | undefined;
182182

183-
private readonly resourceGroupMenusItems = new Map<ISCMResourceGroup, SCMMenusItem>();
184-
185-
private _repositoryContextMenu: IMenu | undefined;
186-
get repositoryContextMenu(): IMenu {
187-
if (!this._repositoryContextMenu) {
188-
this._repositoryContextMenu = this.menuService.createMenu(MenuId.SCMSourceControl, this.contextKeyService);
189-
this.disposables.add(this._repositoryContextMenu);
190-
}
183+
private genericRepositoryContextMenu: IMenu | undefined;
184+
private contextualRepositoryContextMenus: Map<string /* contextValue */, IContextualMenuItem> | undefined;
191185

192-
return this._repositoryContextMenu;
193-
}
186+
private readonly resourceGroupMenusItems = new Map<ISCMResourceGroup, SCMMenusItem>();
194187

195188
private readonly disposables = new DisposableStore();
196189

@@ -247,6 +240,38 @@ export class SCMRepositoryMenus implements ISCMRepositoryMenus, IDisposable {
247240
return item.menu;
248241
}
249242

243+
getRepositoryContextMenu(repository: ISCMRepository): IMenu {
244+
const contextValue = repository.provider.contextValue.get();
245+
if (typeof contextValue === 'undefined') {
246+
if (!this.genericRepositoryContextMenu) {
247+
this.genericRepositoryContextMenu = this.menuService.createMenu(MenuId.SCMSourceControl, this.contextKeyService);
248+
}
249+
250+
return this.genericRepositoryContextMenu;
251+
}
252+
253+
if (!this.contextualRepositoryContextMenus) {
254+
this.contextualRepositoryContextMenus = new Map<string, IContextualMenuItem>();
255+
}
256+
257+
let item = this.contextualRepositoryContextMenus.get(contextValue);
258+
259+
if (!item) {
260+
const contextKeyService = this.contextKeyService.createOverlay([['scmProviderContext', contextValue]]);
261+
const menu = this.menuService.createMenu(MenuId.SCMSourceControl, contextKeyService);
262+
263+
item = {
264+
menu, dispose() {
265+
menu.dispose();
266+
}
267+
};
268+
269+
this.contextualRepositoryContextMenus.set(contextValue, item);
270+
}
271+
272+
return item.menu;
273+
}
274+
250275
getResourceGroupMenu(group: ISCMResourceGroup): IMenu {
251276
return this.getOrCreateResourceGroupMenusItem(group).getResourceGroupMenu(group);
252277
}

src/vs/workbench/contrib/scm/browser/scm.contribution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ CommandsRegistry.registerCommand('scm.setActiveProvider', async (accessor) => {
557557
});
558558

559559
MenuRegistry.appendMenuItem(MenuId.SCMSourceControl, {
560-
group: '100_end',
560+
group: '99_terminal',
561561
command: {
562562
id: 'scm.openInTerminal',
563563
title: localize('open in external terminal', "Open in External Terminal")
@@ -571,7 +571,7 @@ MenuRegistry.appendMenuItem(MenuId.SCMSourceControl, {
571571
});
572572

573573
MenuRegistry.appendMenuItem(MenuId.SCMSourceControl, {
574-
group: '100_end',
574+
group: '99_terminal',
575575
command: {
576576
id: 'scm.openInIntegratedTerminal',
577577
title: localize('open in integrated terminal', "Open in Integrated Terminal")

src/vs/workbench/contrib/scm/browser/scmRepositoriesViewPane.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class SCMRepositoriesViewPane extends ViewPane {
241241

242242
const provider = e.element.provider;
243243
const menus = this.scmViewService.menus.getRepositoryMenus(provider);
244-
const menu = menus.repositoryContextMenu;
244+
const menu = menus.getRepositoryContextMenu(e.element);
245245
const actions = collectContextMenuActions(menu);
246246

247247
const disposables = new DisposableStore();

src/vs/workbench/contrib/scm/browser/scmRepositoryRenderer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ export class RepositoryActionRunner extends ActionRunner {
3737
return super.runAction(action, context);
3838
}
3939

40+
const actionContext = [context];
41+
42+
// If the selection contains the repository, add the
43+
// other selected repositories to the action context
4044
const selection = this.getSelectedRepositories().map(r => r.provider);
41-
const actionContext = selection.some(s => s === context) ? selection : [context];
45+
if (selection.some(s => s === context)) {
46+
actionContext.push(...selection.filter(s => s !== context));
47+
}
4248

4349
await action.run(...actionContext);
4450
}

src/vs/workbench/contrib/scm/browser/scmViewPane.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ export class SCMViewPane extends ViewPane {
26252625

26262626
if (isSCMRepository(element)) {
26272627
const menus = this.scmViewService.menus.getRepositoryMenus(element.provider);
2628-
const menu = menus.repositoryContextMenu;
2628+
const menu = menus.getRepositoryContextMenu(element);
26292629
context = element.provider;
26302630
actionRunner = new RepositoryActionRunner(() => this.getSelectedRepositories());
26312631
disposables.add(actionRunner);

src/vs/workbench/contrib/scm/common/scm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ export interface ISCMTitleMenu {
193193

194194
export interface ISCMRepositoryMenus {
195195
readonly titleMenu: ISCMTitleMenu;
196-
readonly repositoryContextMenu: IMenu;
197196
getRepositoryMenu(repository: ISCMRepository): IMenu;
197+
getRepositoryContextMenu(repository: ISCMRepository): IMenu;
198198
getResourceGroupMenu(group: ISCMResourceGroup): IMenu;
199199
getResourceMenu(resource: ISCMResource): IMenu;
200200
getResourceFolderMenu(group: ISCMResourceGroup): IMenu;

0 commit comments

Comments
 (0)