Skip to content

Commit de2e7e1

Browse files
authored
Support the file/newFile menu path (#12819)
Signed-off-by: Jonah Iden <[email protected]>
1 parent c483ccb commit de2e7e1

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

Diff for: examples/playwright/src/tests/theia-main-menu.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ test.describe('Theia Main Menu', () => {
4141
expect(await mainMenu.isOpen()).toBe(true);
4242
});
4343

44-
test("should show the menu items 'New File' and 'New Folder'", async () => {
44+
test("should show the menu items 'New Text File' and 'New Folder'", async () => {
4545
const mainMenu = await menuBar.openMenu('File');
4646
const menuItems = await mainMenu.visibleMenuItems();
47-
expect(menuItems).toContain('New File...');
47+
expect(menuItems).toContain('New Text File');
4848
expect(menuItems).toContain('New Folder...');
4949
});
5050

51-
test("should return menu item by name 'New File'", async () => {
51+
test("should return menu item by name 'New Text File'", async () => {
5252
const mainMenu = await menuBar.openMenu('File');
53-
const menuItem = await mainMenu.menuItemByName('New File...');
53+
const menuItem = await mainMenu.menuItemByName('New Text File');
5454
expect(menuItem).toBeDefined();
5555

5656
const label = await menuItem?.label();
57-
expect(label).toBe('New File...');
57+
expect(label).toBe('New Text File');
5858

5959
const shortCut = await menuItem?.shortCut();
6060
expect(shortCut).toBe(OSUtil.isMacOS ? '⌥ N' : 'Alt+N');
@@ -65,7 +65,7 @@ test.describe('Theia Main Menu', () => {
6565

6666
test('should detect whether menu item has submenu', async () => {
6767
const mainMenu = await menuBar.openMenu('File');
68-
const newFileItem = await mainMenu.menuItemByName('New File...');
68+
const newFileItem = await mainMenu.menuItemByName('New Text File');
6969
const settingsItem = await mainMenu.menuItemByName('Preferences');
7070

7171
expect(await newFileItem?.hasSubmenu()).toBe(false);

Diff for: packages/core/src/browser/common-frontend-contribution.ts

+73-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { UTF8 } from '../common/encodings';
5252
import { EnvVariablesServer } from '../common/env-variables';
5353
import { AuthenticationService } from './authentication-service';
5454
import { FormatType, Saveable, SaveOptions } from './saveable';
55-
import { QuickInputService, QuickPickItem, QuickPickItemOrSeparator } from './quick-input';
55+
import { QuickInputService, QuickPickItem, QuickPickItemOrSeparator, QuickPickSeparator } from './quick-input';
5656
import { AsyncLocalizationProvider } from '../common/i18n/localization';
5757
import { nls } from '../common/nls';
5858
import { CurrentWidgetCommandAdapter } from './shell/current-widget-command-adapter';
@@ -69,6 +69,7 @@ import { LanguageQuickPickService } from './i18n/language-quick-pick-service';
6969
export namespace CommonMenus {
7070

7171
export const FILE = [...MAIN_MENU_BAR, '1_file'];
72+
export const FILE_NEW_TEXT = [...FILE, '1_new_text'];
7273
export const FILE_NEW = [...FILE, '1_new'];
7374
export const FILE_OPEN = [...FILE, '2_open'];
7475
export const FILE_SAVE = [...FILE, '3_save'];
@@ -79,6 +80,8 @@ export namespace CommonMenus {
7980
export const FILE_SETTINGS_SUBMENU_THEME = [...FILE_SETTINGS_SUBMENU, '2_settings_submenu_theme'];
8081
export const FILE_CLOSE = [...FILE, '6_close'];
8182

83+
export const FILE_NEW_CONTRIBUTIONS = 'file/newFile';
84+
8285
export const EDIT = [...MAIN_MENU_BAR, '2_edit'];
8386
export const EDIT_UNDO = [...EDIT, '1_undo'];
8487
export const EDIT_CLIPBOARD = [...EDIT, '2_clipboard'];
@@ -108,6 +111,7 @@ export namespace CommonCommands {
108111

109112
export const FILE_CATEGORY = 'File';
110113
export const VIEW_CATEGORY = 'View';
114+
export const CREATE_CATEGORY = 'Create';
111115
export const PREFERENCES_CATEGORY = 'Preferences';
112116
export const FILE_CATEGORY_KEY = nls.getDefaultKey(FILE_CATEGORY);
113117
export const VIEW_CATEGORY_KEY = nls.getDefaultKey(VIEW_CATEGORY);
@@ -272,11 +276,16 @@ export namespace CommonCommands {
272276
category: VIEW_CATEGORY,
273277
label: 'Toggle Menu Bar'
274278
});
275-
export const NEW_UNTITLED_FILE = Command.toDefaultLocalizedCommand({
276-
id: 'workbench.action.files.newUntitledFile',
279+
export const NEW_UNTITLED_TEXT_FILE = Command.toDefaultLocalizedCommand({
280+
id: 'workbench.action.files.newUntitledTextFile',
277281
category: FILE_CATEGORY,
278282
label: 'New Untitled Text File'
279283
});
284+
export const NEW_UNTITLED_FILE = Command.toDefaultLocalizedCommand({
285+
id: 'workbench.action.files.newUntitledFile',
286+
category: CREATE_CATEGORY,
287+
label: 'New File...'
288+
});
280289
export const SAVE = Command.toDefaultLocalizedCommand({
281290
id: 'core.save',
282291
category: FILE_CATEGORY,
@@ -371,6 +380,9 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
371380
@inject(CommandRegistry)
372381
protected readonly commandRegistry: CommandRegistry;
373382

383+
@inject(MenuModelRegistry)
384+
protected readonly menuRegistry: MenuModelRegistry;
385+
374386
@inject(StorageService)
375387
protected readonly storageService: StorageService;
376388

@@ -545,6 +557,9 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
545557
registry.registerSubmenu(CommonMenus.VIEW, nls.localizeByDefault('View'));
546558
registry.registerSubmenu(CommonMenus.HELP, nls.localizeByDefault('Help'));
547559

560+
// For plugins contributing create new file commands/menu-actions
561+
registry.registerIndependentSubmenu(CommonMenus.FILE_NEW_CONTRIBUTIONS, nls.localizeByDefault('New File...'));
562+
548563
registry.registerMenuAction(CommonMenus.FILE_SAVE, {
549564
commandId: CommonCommands.SAVE.id
550565
});
@@ -693,10 +708,16 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
693708

694709
registry.registerSubmenu(CommonMenus.VIEW_APPEARANCE_SUBMENU, nls.localizeByDefault('Appearance'));
695710

696-
registry.registerMenuAction(CommonMenus.FILE_NEW, {
711+
registry.registerMenuAction(CommonMenus.FILE_NEW_TEXT, {
712+
commandId: CommonCommands.NEW_UNTITLED_TEXT_FILE.id,
713+
label: nls.localizeByDefault('New Text File'),
714+
order: 'a'
715+
});
716+
717+
registry.registerMenuAction(CommonMenus.FILE_NEW_TEXT, {
697718
commandId: CommonCommands.NEW_UNTITLED_FILE.id,
698719
label: nls.localizeByDefault('New File...'),
699-
order: 'a'
720+
order: 'a1'
700721
});
701722
}
702723

@@ -941,13 +962,17 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
941962
execute: () => this.toggleBreadcrumbs(),
942963
isToggled: () => this.isBreadcrumbsEnabled(),
943964
});
944-
commandRegistry.registerCommand(CommonCommands.NEW_UNTITLED_FILE, {
965+
commandRegistry.registerCommand(CommonCommands.NEW_UNTITLED_TEXT_FILE, {
945966
execute: async () => {
946967
const untitledUri = this.untitledResourceResolver.createUntitledURI('', await this.workingDirProvider.getUserWorkingDir());
947968
this.untitledResourceResolver.resolve(untitledUri);
948969
return open(this.openerService, untitledUri);
949970
}
950971
});
972+
commandRegistry.registerCommand(CommonCommands.NEW_UNTITLED_FILE, {
973+
execute: async () => this.showNewFilePicker()
974+
});
975+
951976
for (const [index, ordinal] of this.getOrdinalNumbers().entries()) {
952977
commandRegistry.registerCommand({ id: `workbench.action.focus${ordinal}EditorGroup`, label: index === 0 ? nls.localizeByDefault('Focus First Editor Group') : '', category: nls.localize(CommonCommands.VIEW_CATEGORY_KEY, CommonCommands.VIEW_CATEGORY) }, {
953978
isEnabled: () => this.shell.mainAreaTabBars.length > index,
@@ -1097,8 +1122,12 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
10971122
when: 'activeEditorIsPinned'
10981123
},
10991124
{
1100-
command: CommonCommands.NEW_UNTITLED_FILE.id,
1125+
command: CommonCommands.NEW_UNTITLED_TEXT_FILE.id,
11011126
keybinding: this.isElectron() ? 'ctrlcmd+n' : 'alt+n',
1127+
},
1128+
{
1129+
command: CommonCommands.NEW_UNTITLED_FILE.id,
1130+
keybinding: 'ctrlcmd+alt+n'
11021131
}
11031132
);
11041133
for (const [index, ordinal] of this.getOrdinalNumbers().entries()) {
@@ -1294,6 +1323,43 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
12941323
});
12951324
}
12961325

1326+
/**
1327+
* @todo https://github.com/eclipse-theia/theia/issues/12824
1328+
*/
1329+
protected async showNewFilePicker(): Promise<void> {
1330+
const newFileContributions = this.menuRegistry.getMenuNode(CommonMenus.FILE_NEW_CONTRIBUTIONS); // Add menus
1331+
const items: QuickPickItemOrSeparator[] = [
1332+
{
1333+
label: nls.localizeByDefault('New Text File'),
1334+
execute: async () => this.commandRegistry.executeCommand(CommonCommands.NEW_UNTITLED_TEXT_FILE.id)
1335+
},
1336+
...newFileContributions.children
1337+
.flatMap(node => {
1338+
if (node.children && node.children.length > 0) {
1339+
return node.children;
1340+
}
1341+
return node;
1342+
})
1343+
.filter(node => node.role || node.command)
1344+
.map(node => {
1345+
if (node.role) {
1346+
return { type: 'separator' } as QuickPickSeparator;
1347+
}
1348+
const command = this.commandRegistry.getCommand(node.command!);
1349+
return {
1350+
label: command!.label!,
1351+
execute: async () => this.commandRegistry.executeCommand(command!.id!)
1352+
};
1353+
1354+
})
1355+
];
1356+
this.quickInputService.showQuickPick(items, {
1357+
title: nls.localizeByDefault('New File...'),
1358+
placeholder: nls.localizeByDefault('Select File Type or Enter File Name...'),
1359+
canSelectMany: false
1360+
});
1361+
}
1362+
12971363
registerColors(colors: ColorRegistry): void {
12981364
colors.register(
12991365
// Base Colors should be aligned with https://code.visualstudio.com/api/references/theme-color#base-colors

Diff for: packages/core/src/browser/window-contribution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class WindowContribution implements CommandContribution, KeybindingContri
5151
}
5252

5353
registerMenus(registry: MenuModelRegistry): void {
54-
registry.registerMenuAction(CommonMenus.FILE_NEW, {
54+
registry.registerMenuAction(CommonMenus.FILE_NEW_TEXT, {
5555
commandId: WindowCommands.NEW_WINDOW.id,
5656
order: 'c'
5757
});

Diff for: packages/workspace/src/browser/workspace-commands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export namespace WorkspaceCommands {
153153
export class FileMenuContribution implements MenuContribution {
154154

155155
registerMenus(registry: MenuModelRegistry): void {
156-
registry.registerMenuAction(CommonMenus.FILE_NEW, {
156+
registry.registerMenuAction(CommonMenus.FILE_NEW_TEXT, {
157157
commandId: WorkspaceCommands.NEW_FOLDER.id,
158158
order: 'b'
159159
});

0 commit comments

Comments
 (0)