Prioritize New File even more

Fix #144667
This commit is contained in:
Jackson Kearl 2022-04-05 17:29:01 -07:00
parent 37049cd22f
commit f050b17dac
No known key found for this signature in database
GPG key ID: DA09A59C409FC400

View file

@ -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,7 +34,6 @@ registerAction2(class extends Action2 {
},
menu: {
id: MenuId.MenubarFileMenu,
when: HasMultipleNewFileEntries,
group: '1_new',
order: 2
}
@ -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,17 +72,13 @@ 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() {
const entries = this.allEntries();
if (entries.length === 0) {
@ -108,12 +99,20 @@ class NewFileTemplatesManager extends Disposable {
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 +124,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);