Skip to content

Commit

Permalink
Prioritize New File even more
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Kearl committed Apr 6, 2022
1 parent 37049cd commit f050b17
Showing 1 changed file with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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({
Expand All @@ -36,7 +34,6 @@ registerAction2(class extends Action2 {
},
menu: {
id: MenuId.MenubarFileMenu,
when: HasMultipleNewFileEntries,
group: '1_new',
order: 2
}
Expand Down Expand Up @@ -68,26 +65,20 @@ 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[] {
const items: NewFileItem[] = [];
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) {
Expand All @@ -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> = {
Expand All @@ -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);
Expand Down

0 comments on commit f050b17

Please sign in to comment.