diff options
Diffstat (limited to 'src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts')
-rw-r--r-- | src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts b/src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts index b78599440db..8b87c52e229 100644 --- a/src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts +++ b/src/vs/workbench/contrib/welcomeViews/common/newFile.contribution.ts @@ -9,7 +9,7 @@ import { assertIsDefined } from 'vs/base/common/types'; import { localize } from 'vs/nls'; import { Action2, IMenuService, MenuId, registerAction2, IMenu, MenuRegistry, MenuItemAction } from 'vs/platform/actions/common/actions'; import { ICommandService } from 'vs/platform/commands/common/commands'; -import { IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey'; +import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; @@ -18,11 +18,9 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions'; import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle'; - +const builtInSource = localize('Built-In', "Built-In"); const category = localize('Create', "Create"); -export const HasMultipleNewFileEntries = new RawContextKey<boolean>('hasMultipleNewFileEntries', false); - registerAction2(class extends Action2 { constructor() { super({ @@ -36,15 +34,14 @@ registerAction2(class extends Action2 { }, menu: { id: MenuId.MenubarFileMenu, - when: HasMultipleNewFileEntries, group: '1_new', order: 2 } }); } - run(accessor: ServicesAccessor) { - assertIsDefined(NewFileTemplatesManager.Instance).run(); + async run(accessor: ServicesAccessor): Promise<boolean> { + return assertIsDefined(NewFileTemplatesManager.Instance).run(); } }); @@ -68,8 +65,6 @@ class NewFileTemplatesManager extends Disposable { this._register({ dispose() { if (NewFileTemplatesManager.Instance === this) { NewFileTemplatesManager.Instance = undefined; } } }); this.menu = menuService.createMenu(MenuId.NewFile, contextKeyService); - this.updateContextKeys(); - this._register(this.menu.onDidChange(() => { this.updateContextKeys(); })); } private allEntries(): NewFileItem[] { @@ -77,43 +72,53 @@ class NewFileTemplatesManager extends Disposable { for (const [groupName, group] of this.menu.getActions({ renderShortTitle: true })) { for (const action of group) { if (action instanceof MenuItemAction) { - items.push({ commandID: action.item.id, from: action.item.source ?? localize('Built-In', "Built-In"), title: action.label, group: groupName }); + items.push({ commandID: action.item.id, from: action.item.source ?? builtInSource, title: action.label, group: groupName }); } } } return items; } - private updateContextKeys() { - HasMultipleNewFileEntries.bindTo(this.contextKeyService).set(this.allEntries().length > 1); - } - - run() { + async run(): Promise<boolean> { const entries = this.allEntries(); if (entries.length === 0) { throw Error('Unexpected empty new items list'); } else if (entries.length === 1) { this.commandService.executeCommand(entries[0].commandID); + return true; } else { - this.selectNewEntry(entries); + return this.selectNewEntry(entries); } } - private async selectNewEntry(entries: NewFileItem[]) { + private async selectNewEntry(entries: NewFileItem[]): Promise<boolean> { + let resolveResult: (res: boolean) => void; + const resultPromise = new Promise<boolean>(resolve => { + resolveResult = resolve; + }); + const disposables = new DisposableStore(); const qp = this.quickInputService.createQuickPick(); qp.title = localize('createNew', "Create New..."); qp.matchOnDetail = true; qp.matchOnDescription = true; - const sortCategories = (a: string, b: string): number => { + const sortCategories = (a: NewFileItem, b: NewFileItem): number => { const categoryPriority: Record<string, number> = { 'file': 1, 'notebook': 2 }; - if (categoryPriority[a] && categoryPriority[b]) { return categoryPriority[b] - categoryPriority[a]; } - if (categoryPriority[a]) { return 1; } - if (categoryPriority[b]) { return -1; } - return a.localeCompare(b); + if (categoryPriority[a.group] && categoryPriority[b.group]) { + if (categoryPriority[a.group] !== categoryPriority[b.group]) { + return categoryPriority[b.group] - categoryPriority[a.group]; + } + } + else if (categoryPriority[a.group]) { return 1; } + else if (categoryPriority[b.group]) { return -1; } + + if (a.from === builtInSource) { return 1; } + if (b.from === builtInSource) { return -1; } + + return a.from.localeCompare(b.from); }; const displayCategory: Record<string, string> = { @@ -125,7 +130,7 @@ class NewFileTemplatesManager extends Disposable { const items: (((IQuickPickItem & NewFileItem) | IQuickPickSeparator))[] = []; let lastSeparator: string | undefined; entries - .sort((a, b) => -sortCategories(a.group, b.group)) + .sort((a, b) => -sortCategories(a, b)) .forEach((entry) => { const command = entry.commandID; const keybinding = this.keybindingService.lookupKeybinding(command || '', this.contextKeyService); @@ -159,6 +164,8 @@ class NewFileTemplatesManager extends Disposable { disposables.add(qp.onDidAccept(async e => { const selected = qp.selectedItems[0] as (IQuickPickItem & NewFileItem); + resolveResult(!!selected); + qp.hide(); if (selected) { await this.commandService.executeCommand(selected.commandID); } })); @@ -166,23 +173,26 @@ class NewFileTemplatesManager extends Disposable { disposables.add(qp.onDidHide(() => { qp.dispose(); disposables.dispose(); + resolveResult(false); })); disposables.add(qp.onDidTriggerItemButton(e => { qp.hide(); this.commandService.executeCommand('workbench.action.openGlobalKeybindings', (e.item as (IQuickPickItem & NewFileItem)).commandID); + resolveResult(false); })); qp.show(); - } + return resultPromise; + } } Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench) .registerWorkbenchContribution(NewFileTemplatesManager, LifecyclePhase.Restored); MenuRegistry.appendMenuItem(MenuId.NewFile, { - group: 'File', + group: 'file', command: { id: 'workbench.action.files.newUntitledFile', title: localize('miNewFile2', "Text File") |