@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
960 lines • 124 kB
JavaScript
"use strict";
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonFrontendContribution = exports.CLASSNAME_OS_LINUX = exports.CLASSNAME_OS_WINDOWS = exports.CLASSNAME_OS_MAC = exports.RECENT_COMMANDS_STORAGE_KEY = exports.supportPaste = exports.supportCopy = exports.supportCut = exports.CommonCommands = exports.CommonMenus = void 0;
const tslib_1 = require("tslib");
/* eslint-disable max-len, @typescript-eslint/indent */
const debounce = require("lodash.debounce");
const inversify_1 = require("inversify");
const menu_1 = require("../common/menu");
const common_menus_1 = require("./common-menus");
Object.defineProperty(exports, "CommonMenus", { enumerable: true, get: function () { return common_menus_1.CommonMenus; } });
const command_1 = require("../common/command");
const common_commands_1 = require("./common-commands");
Object.defineProperty(exports, "CommonCommands", { enumerable: true, get: function () { return common_commands_1.CommonCommands; } });
const uri_command_handler_1 = require("../common/uri-command-handler");
const selection_service_1 = require("../common/selection-service");
const message_service_1 = require("../common/message-service");
const opener_service_1 = require("../browser/opener-service");
const application_shell_1 = require("./shell/application-shell");
const tab_bars_1 = require("./shell/tab-bars");
const about_dialog_1 = require("./about-dialog");
const browser = require("./browser");
const uri_1 = require("../common/uri");
const context_key_service_1 = require("./context-key-service");
const os_1 = require("../common/os");
const resource_context_key_1 = require("./resource-context-key");
const selection_1 = require("../common/selection");
const storage_service_1 = require("./storage-service");
const navigatable_1 = require("./navigatable");
const quick_view_service_1 = require("./quick-input/quick-view-service");
const environment_1 = require("@theia/application-package/lib/environment");
const icon_theme_service_1 = require("./icon-theme-service");
const color_1 = require("../common/color");
const core_preferences_1 = require("../common/core-preferences");
const theming_1 = require("./theming");
const clipboard_service_1 = require("./clipboard-service");
const encoding_registry_1 = require("./encoding-registry");
const encodings_1 = require("../common/encodings");
const env_variables_1 = require("../common/env-variables");
const authentication_service_1 = require("./authentication-service");
const saveable_1 = require("./saveable");
const quick_input_1 = require("./quick-input");
const localization_1 = require("../common/i18n/localization");
const nls_1 = require("../common/nls");
const current_widget_command_adapter_1 = require("./shell/current-widget-command-adapter");
const dialogs_1 = require("./dialogs");
const window_service_1 = require("./window/window-service");
const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
const decoration_style_1 = require("./decoration-style");
const widgets_1 = require("./widgets");
const saveable_service_1 = require("./saveable-service");
const user_working_directory_provider_1 = require("./user-working-directory-provider");
const common_1 = require("../common");
const language_quick_pick_service_1 = require("./i18n/language-quick-pick-service");
const undo_redo_handler_1 = require("./undo-redo-handler");
const promise_util_1 = require("../common/promise-util");
exports.supportCut = environment_1.environment.electron.is() || document.queryCommandSupported('cut');
exports.supportCopy = environment_1.environment.electron.is() || document.queryCommandSupported('copy');
// Chrome incorrectly returns true for document.queryCommandSupported('paste')
// when the paste feature is available but the calling script has insufficient
// privileges to actually perform the action
exports.supportPaste = environment_1.environment.electron.is() || (!browser.isChrome && document.queryCommandSupported('paste'));
exports.RECENT_COMMANDS_STORAGE_KEY = 'commands';
exports.CLASSNAME_OS_MAC = 'mac';
exports.CLASSNAME_OS_WINDOWS = 'windows';
exports.CLASSNAME_OS_LINUX = 'linux';
let CommonFrontendContribution = class CommonFrontendContribution {
constructor(shell, selectionService, messageService, openerService, aboutDialog, localizationProvider, saveResourceService) {
this.shell = shell;
this.selectionService = selectionService;
this.messageService = messageService;
this.openerService = openerService;
this.aboutDialog = aboutDialog;
this.localizationProvider = localizationProvider;
this.saveResourceService = saveResourceService;
this.commonDecorationsStyleSheet = decoration_style_1.DecorationStyle.createStyleSheet('coreCommonDecorationsStyle');
this.shouldPreventClose = false;
}
async configure(app) {
// FIXME: This request blocks valuable startup time (~200ms).
const configDirUri = await this.environments.getConfigDirUri();
// Global settings
this.encodingRegistry.registerOverride({
encoding: encodings_1.UTF8,
parent: new uri_1.default(configDirUri)
});
this.contextKeyService.createKey('isLinux', os_1.OS.type() === os_1.OS.Type.Linux);
this.contextKeyService.createKey('isMac', os_1.OS.type() === os_1.OS.Type.OSX);
this.contextKeyService.createKey('isWindows', os_1.OS.type() === os_1.OS.Type.Windows);
this.contextKeyService.createKey('isWeb', !this.isElectron());
this.inputFocus = this.contextKeyService.createKey('inputFocus', false);
this.updateInputFocus();
browser.onDomEvent(document, 'focusin', () => this.updateInputFocus());
this.pinnedKey = this.contextKeyService.createKey('activeEditorIsPinned', false);
this.updatePinnedKey();
this.shell.onDidChangeActiveWidget(() => this.updatePinnedKey());
this.initResourceContextKeys();
this.registerCtrlWHandling();
this.setOsClass();
this.updateStyles();
this.preferences.ready.then(() => this.setSashProperties());
this.preferences.onPreferenceChanged(e => this.handlePreferenceChange(e, app));
app.shell.initialized.then(() => {
app.shell.leftPanelHandler.addBottomMenu({
id: 'settings-menu',
iconClass: (0, widgets_1.codicon)('settings-gear'),
title: nls_1.nls.localizeByDefault(common_commands_1.CommonCommands.MANAGE_CATEGORY),
menuPath: menu_1.MANAGE_MENU,
order: 0,
});
const accountsMenu = {
id: 'accounts-menu',
iconClass: (0, widgets_1.codicon)('account'),
title: nls_1.nls.localizeByDefault('Accounts'),
menuPath: menu_1.ACCOUNTS_MENU,
order: 1,
onDidBadgeChange: this.authenticationService.onDidUpdateSignInCount
};
this.authenticationService.onDidRegisterAuthenticationProvider(() => {
app.shell.leftPanelHandler.addBottomMenu(accountsMenu);
});
this.authenticationService.onDidUnregisterAuthenticationProvider(() => {
if (this.authenticationService.getProviderIds().length === 0) {
app.shell.leftPanelHandler.removeBottomMenu(accountsMenu.id);
}
});
});
}
setOsClass() {
if (os_1.isOSX) {
document.body.classList.add(exports.CLASSNAME_OS_MAC);
}
else if (os_1.isWindows) {
document.body.classList.add(exports.CLASSNAME_OS_WINDOWS);
}
else {
document.body.classList.add(exports.CLASSNAME_OS_LINUX);
}
}
updateStyles() {
document.body.classList.remove('theia-editor-highlightModifiedTabs');
if (this.preferences['workbench.editor.highlightModifiedTabs']) {
document.body.classList.add('theia-editor-highlightModifiedTabs');
}
}
updateInputFocus() {
var _a, _b;
const activeElement = document.activeElement;
if (activeElement) {
const isInput = ((_a = activeElement.tagName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'input'
|| ((_b = activeElement.tagName) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'textarea';
this.inputFocus.set(isInput);
}
}
updatePinnedKey() {
const activeTab = this.shell.findTabBar();
const pinningTarget = activeTab && this.shell.findTitle(activeTab);
const value = pinningTarget && (0, widgets_1.isPinned)(pinningTarget);
this.pinnedKey.set(value);
}
handlePreferenceChange(e, app) {
switch (e.preferenceName) {
case 'workbench.editor.highlightModifiedTabs': {
this.updateStyles();
break;
}
case 'window.menuBarVisibility': {
const { newValue } = e;
const mainMenuId = 'main-menu';
if (newValue === 'compact') {
this.shell.leftPanelHandler.addTopMenu({
id: mainMenuId,
iconClass: `theia-compact-menu ${(0, widgets_1.codicon)('menu')}`,
title: nls_1.nls.localizeByDefault('Application Menu'),
menuPath: menu_1.MAIN_MENU_BAR,
order: 0,
});
}
else {
app.shell.leftPanelHandler.removeTopMenu(mainMenuId);
}
break;
}
case 'workbench.sash.hoverDelay':
case 'workbench.sash.size': {
this.setSashProperties();
break;
}
}
}
setSashProperties() {
const sashRule = `:root {
--theia-sash-hoverDelay: ${this.preferences['workbench.sash.hoverDelay']}ms;
--theia-sash-width: ${this.preferences['workbench.sash.size']}px;
}`;
decoration_style_1.DecorationStyle.deleteStyleRule(':root', this.commonDecorationsStyleSheet);
this.commonDecorationsStyleSheet.insertRule(sashRule);
}
onStart() {
this.storageService.getData(exports.RECENT_COMMANDS_STORAGE_KEY, { recent: [] })
.then(tasks => this.commandRegistry.recent = tasks.recent);
}
onStop() {
const recent = this.commandRegistry.recent;
this.storageService.setData(exports.RECENT_COMMANDS_STORAGE_KEY, { recent });
window.localStorage.setItem(icon_theme_service_1.IconThemeService.STORAGE_KEY, this.iconThemes.current);
window.localStorage.setItem(theming_1.ThemeService.STORAGE_KEY, this.themeService.getCurrentTheme().id);
}
initResourceContextKeys() {
const updateContextKeys = () => {
const selection = this.selectionService.selection;
const resourceUri = navigatable_1.Navigatable.is(selection) && selection.getResourceUri() || selection_1.UriSelection.getUri(selection);
this.resourceContextKey.set(resourceUri);
};
updateContextKeys();
this.selectionService.onSelectionChanged(updateContextKeys);
}
registerMenus(registry) {
registry.registerSubmenu(common_menus_1.CommonMenus.FILE, nls_1.nls.localizeByDefault('File'));
registry.registerSubmenu(common_menus_1.CommonMenus.EDIT, nls_1.nls.localizeByDefault('Edit'));
registry.registerSubmenu(common_menus_1.CommonMenus.VIEW, nls_1.nls.localizeByDefault('View'));
registry.registerSubmenu(common_menus_1.CommonMenus.HELP, nls_1.nls.localizeByDefault('Help'));
// For plugins contributing create new file commands/menu-actions
registry.registerSubmenu(common_menus_1.CommonMenus.FILE_NEW_CONTRIBUTIONS, nls_1.nls.localizeByDefault('New File...'));
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_SAVE, {
commandId: common_commands_1.CommonCommands.SAVE.id
});
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_SAVE, {
commandId: common_commands_1.CommonCommands.SAVE_ALL.id
});
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_AUTOSAVE, {
commandId: common_commands_1.CommonCommands.AUTO_SAVE.id
});
registry.registerSubmenu(common_menus_1.CommonMenus.FILE_SETTINGS_SUBMENU, nls_1.nls.localizeByDefault(common_commands_1.CommonCommands.PREFERENCES_CATEGORY));
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_UNDO, {
commandId: common_commands_1.CommonCommands.UNDO.id,
order: '0'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_UNDO, {
commandId: common_commands_1.CommonCommands.REDO.id,
order: '1'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_FIND, {
commandId: common_commands_1.CommonCommands.FIND.id,
order: '0'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_FIND, {
commandId: common_commands_1.CommonCommands.REPLACE.id,
order: '1'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_CLIPBOARD, {
commandId: common_commands_1.CommonCommands.CUT.id,
order: '0'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_CLIPBOARD, {
commandId: common_commands_1.CommonCommands.COPY.id,
order: '1'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_CLIPBOARD, {
commandId: common_commands_1.CommonCommands.PASTE.id,
order: '2'
});
registry.registerMenuAction(common_menus_1.CommonMenus.EDIT_CLIPBOARD, {
commandId: common_commands_1.CommonCommands.COPY_PATH.id,
order: '3'
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU_BAR, {
commandId: common_commands_1.CommonCommands.TOGGLE_BOTTOM_PANEL.id,
order: '1'
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU_BAR, {
commandId: common_commands_1.CommonCommands.TOGGLE_STATUS_BAR.id,
order: '2',
label: nls_1.nls.localizeByDefault('Toggle Status Bar Visibility')
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU_BAR, {
commandId: common_commands_1.CommonCommands.COLLAPSE_ALL_PANELS.id,
order: '3'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_CLOSE, {
commandId: common_commands_1.CommonCommands.CLOSE_TAB.id,
label: nls_1.nls.localizeByDefault('Close'),
order: '0'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_CLOSE, {
commandId: common_commands_1.CommonCommands.CLOSE_OTHER_TABS.id,
label: nls_1.nls.localizeByDefault('Close Others'),
order: '1'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_CLOSE, {
commandId: common_commands_1.CommonCommands.CLOSE_RIGHT_TABS.id,
label: nls_1.nls.localizeByDefault('Close to the Right'),
order: '2'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_CLOSE, {
commandId: common_commands_1.CommonCommands.CLOSE_SAVED_TABS.id,
label: nls_1.nls.localizeByDefault('Close Saved'),
order: '3',
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_CLOSE, {
commandId: common_commands_1.CommonCommands.CLOSE_ALL_TABS.id,
label: nls_1.nls.localizeByDefault('Close All'),
order: '4'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_SPLIT, {
commandId: common_commands_1.CommonCommands.COLLAPSE_PANEL.id,
label: common_commands_1.CommonCommands.COLLAPSE_PANEL.label,
order: '5'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_SPLIT, {
commandId: common_commands_1.CommonCommands.TOGGLE_MAXIMIZED.id,
label: common_commands_1.CommonCommands.TOGGLE_MAXIMIZED.label,
order: '6'
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU_SCREEN, {
commandId: common_commands_1.CommonCommands.TOGGLE_MAXIMIZED.id,
label: common_commands_1.CommonCommands.TOGGLE_MAXIMIZED.label,
order: '6'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_COPY, {
commandId: common_commands_1.CommonCommands.COPY_PATH.id,
label: common_commands_1.CommonCommands.COPY_PATH.label,
order: '1',
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU_BAR, {
commandId: common_commands_1.CommonCommands.SHOW_MENU_BAR.id,
label: nls_1.nls.localizeByDefault('Toggle Menu Bar'),
order: '0'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_PIN, {
commandId: common_commands_1.CommonCommands.PIN_TAB.id,
label: nls_1.nls.localizeByDefault('Pin'),
order: '7'
});
registry.registerMenuAction(tab_bars_1.SHELL_TABBAR_CONTEXT_PIN, {
commandId: common_commands_1.CommonCommands.UNPIN_TAB.id,
label: nls_1.nls.localizeByDefault('Unpin'),
order: '8'
});
registry.registerMenuAction(common_menus_1.CommonMenus.HELP, {
commandId: common_commands_1.CommonCommands.ABOUT_COMMAND.id,
label: common_commands_1.CommonCommands.ABOUT_COMMAND.label,
order: '9'
});
registry.registerMenuAction(common_menus_1.CommonMenus.VIEW_PRIMARY, {
commandId: common_commands_1.CommonCommands.OPEN_VIEW.id
});
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_SETTINGS_SUBMENU_THEME, {
commandId: common_commands_1.CommonCommands.SELECT_COLOR_THEME.id
});
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_SETTINGS_SUBMENU_THEME, {
commandId: common_commands_1.CommonCommands.SELECT_ICON_THEME.id
});
registry.registerSubmenu(common_menus_1.CommonMenus.MANAGE_SETTINGS_THEMES, nls_1.nls.localizeByDefault('Themes'), { sortString: 'a50' });
registry.registerMenuAction(common_menus_1.CommonMenus.MANAGE_SETTINGS_THEMES, {
commandId: common_commands_1.CommonCommands.SELECT_COLOR_THEME.id,
order: '0'
});
registry.registerMenuAction(common_menus_1.CommonMenus.MANAGE_SETTINGS_THEMES, {
commandId: common_commands_1.CommonCommands.SELECT_ICON_THEME.id,
order: '1'
});
registry.registerSubmenu(common_menus_1.CommonMenus.VIEW_APPEARANCE_SUBMENU, nls_1.nls.localizeByDefault('Appearance'));
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_NEW_TEXT, {
commandId: common_commands_1.CommonCommands.NEW_UNTITLED_TEXT_FILE.id,
label: nls_1.nls.localizeByDefault('New Text File'),
order: 'a'
});
registry.registerMenuAction(common_menus_1.CommonMenus.FILE_NEW_TEXT, {
commandId: common_commands_1.CommonCommands.PICK_NEW_FILE.id,
label: nls_1.nls.localizeByDefault('New File...'),
order: 'a1'
});
}
registerCommands(commandRegistry) {
commandRegistry.registerCommand(common_commands_1.CommonCommands.OPEN, uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, {
execute: uris => uris.map(uri => (0, opener_service_1.open)(this.openerService, uri)),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CUT, {
execute: () => {
if (exports.supportCut) {
document.execCommand('cut');
}
else {
this.messageService.warn(nls_1.nls.localize('theia/core/cutWarn', "Please use the browser's cut command or shortcut."));
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.COPY, {
execute: () => {
if (exports.supportCopy) {
document.execCommand('copy');
}
else {
this.messageService.warn(nls_1.nls.localize('theia/core/copyWarn', "Please use the browser's copy command or shortcut."));
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PASTE, {
execute: () => {
if (exports.supportPaste) {
document.execCommand('paste');
}
else {
this.messageService.warn(nls_1.nls.localize('theia/core/pasteWarn', "Please use the browser's paste command or shortcut."));
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.COPY_PATH, uri_command_handler_1.UriAwareCommandHandler.MultiSelect(this.selectionService, {
isVisible: uris => Array.isArray(uris) && uris.some(uri => uri instanceof uri_1.default),
isEnabled: uris => Array.isArray(uris) && uris.some(uri => uri instanceof uri_1.default),
execute: async (uris) => {
if (uris.length) {
const lineDelimiter = os_1.EOL;
const text = uris.map(resource => resource.path.fsPath()).join(lineDelimiter);
await this.clipboardService.writeText(text);
}
else {
await this.messageService.info(nls_1.nls.localize('theia/core/copyInfo', 'Open a file first to copy its path'));
}
}
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.UNDO, {
execute: () => {
this.undoRedoHandlerService.undo();
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.REDO, {
execute: () => {
this.undoRedoHandlerService.redo();
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SELECT_ALL, {
execute: () => document.execCommand('selectAll')
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.FIND, {
execute: () => { }
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.REPLACE, {
execute: () => { }
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.NEXT_TAB, {
isEnabled: () => this.shell.currentTabBar !== undefined,
execute: () => this.shell.activateNextTab()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PREVIOUS_TAB, {
isEnabled: () => this.shell.currentTabBar !== undefined,
execute: () => this.shell.activatePreviousTab()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.NEXT_TAB_IN_GROUP, {
isEnabled: () => this.shell.nextTabIndexInTabBar() !== -1,
execute: () => this.shell.activateNextTabInTabBar()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PREVIOUS_TAB_IN_GROUP, {
isEnabled: () => this.shell.previousTabIndexInTabBar() !== -1,
execute: () => this.shell.activatePreviousTabInTabBar()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.NEXT_TAB_GROUP, {
isEnabled: () => this.shell.nextTabBar() !== undefined,
execute: () => this.shell.activateNextTabBar()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PREVIOUS_TAB_GROUP, {
isEnabled: () => this.shell.previousTabBar() !== undefined,
execute: () => this.shell.activatePreviousTabBar()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_TAB, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: title => Boolean(title === null || title === void 0 ? void 0 : title.closable),
execute: (title, tabBar) => tabBar && this.shell.closeTabs(tabBar, candidate => candidate === title),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_OTHER_TABS, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: (title, tabbar) => Boolean(tabbar === null || tabbar === void 0 ? void 0 : tabbar.titles.some(candidate => candidate !== title && candidate.closable)),
execute: (title, tabbar) => tabbar && this.shell.closeTabs(tabbar, candidate => candidate !== title && candidate.closable),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_SAVED_TABS, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: (_title, tabbar) => Boolean(tabbar === null || tabbar === void 0 ? void 0 : tabbar.titles.some(candidate => candidate.closable && !saveable_1.Saveable.isDirty(candidate.owner))),
execute: (_title, tabbar) => tabbar && this.shell.closeTabs(tabbar, candidate => candidate.closable && !saveable_1.Saveable.isDirty(candidate.owner)),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_RIGHT_TABS, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: (title, tabbar) => {
let targetSeen = false;
return Boolean(tabbar === null || tabbar === void 0 ? void 0 : tabbar.titles.some(candidate => {
if (targetSeen && candidate.closable) {
return true;
}
;
if (candidate === title) {
targetSeen = true;
}
;
}));
},
isVisible: (_title, tabbar) => {
var _a;
const area = (_a = (tabbar && this.shell.getAreaFor(tabbar))) !== null && _a !== void 0 ? _a : this.shell.currentTabArea;
return area !== undefined && area !== 'left' && area !== 'right';
},
execute: (title, tabbar) => {
if (tabbar) {
let targetSeen = false;
this.shell.closeTabs(tabbar, candidate => {
if (targetSeen && candidate.closable) {
return true;
}
;
if (candidate === title) {
targetSeen = true;
}
;
return false;
});
}
}
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_ALL_TABS, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: (_title, tabbar) => Boolean(tabbar === null || tabbar === void 0 ? void 0 : tabbar.titles.some(title => title.closable)),
execute: (_title, tabbar) => tabbar && this.shell.closeTabs(tabbar, candidate => candidate.closable),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_MAIN_TAB, {
isEnabled: () => {
const currentWidget = this.shell.getCurrentWidget('main');
return currentWidget !== undefined && currentWidget.title.closable;
},
execute: () => this.shell.getCurrentWidget('main').close()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_OTHER_MAIN_TABS, {
isEnabled: () => {
const currentWidget = this.shell.getCurrentWidget('main');
return currentWidget !== undefined &&
this.shell.mainAreaTabBars.some(tb => tb.titles.some(title => title.owner !== currentWidget && title.closable));
},
execute: () => {
const currentWidget = this.shell.getCurrentWidget('main');
this.shell.closeTabs('main', title => title.owner !== currentWidget && title.closable);
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.CLOSE_ALL_MAIN_TABS, {
isEnabled: () => this.shell.mainAreaTabBars.some(tb => tb.titles.some(title => title.closable)),
execute: () => this.shell.closeTabs('main', title => title.closable)
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.COLLAPSE_PANEL, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: (_title, tabbar) => {
if (tabbar) {
const area = this.shell.getAreaFor(tabbar);
return application_shell_1.ApplicationShell.isSideArea(area) && this.shell.isExpanded(area);
}
return false;
},
isVisible: (_title, tabbar) => Boolean(tabbar && application_shell_1.ApplicationShell.isSideArea(this.shell.getAreaFor(tabbar))),
execute: (_title, tabbar) => tabbar && this.shell.collapsePanel(this.shell.getAreaFor(tabbar))
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.COLLAPSE_ALL_PANELS, {
execute: () => {
this.shell.collapsePanel('left');
this.shell.collapsePanel('right');
this.shell.collapsePanel('bottom');
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_BOTTOM_PANEL, {
isEnabled: () => this.shell.getWidgets('bottom').length > 0,
execute: () => {
if (this.shell.isExpanded('bottom')) {
this.shell.collapsePanel('bottom');
}
else {
this.shell.expandPanel('bottom');
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_LEFT_PANEL, {
isEnabled: () => this.shell.getWidgets('left').length > 0,
execute: () => {
if (this.shell.isExpanded('left')) {
this.shell.collapsePanel('left');
}
else {
this.shell.expandPanel('left');
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_RIGHT_PANEL, {
isEnabled: () => this.shell.getWidgets('right').length > 0,
execute: () => {
if (this.shell.isExpanded('right')) {
this.shell.collapsePanel('right');
}
else {
this.shell.expandPanel('right');
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_STATUS_BAR, {
execute: () => this.preferenceService.updateValue('workbench.statusBar.visible', !this.preferences['workbench.statusBar.visible'])
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_MAXIMIZED, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: title => Boolean((title === null || title === void 0 ? void 0 : title.owner) && this.shell.canToggleMaximized(title === null || title === void 0 ? void 0 : title.owner)),
isVisible: title => Boolean((title === null || title === void 0 ? void 0 : title.owner) && this.shell.canToggleMaximized(title === null || title === void 0 ? void 0 : title.owner)),
execute: title => (title === null || title === void 0 ? void 0 : title.owner) && this.shell.toggleMaximized(title === null || title === void 0 ? void 0 : title.owner),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.SHOW_MENU_BAR, {
isEnabled: () => !os_1.isOSX,
isVisible: () => !os_1.isOSX,
execute: () => {
const menuBarVisibility = 'window.menuBarVisibility';
const visibility = this.preferences[menuBarVisibility];
if (visibility !== 'compact') {
this.preferenceService.updateValue(menuBarVisibility, 'compact');
}
else {
this.preferenceService.updateValue(menuBarVisibility, 'classic');
}
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SAVE, {
execute: () => this.save({ formatType: 1 /* FormatType.ON */ })
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SAVE_AS, {
isEnabled: () => this.saveResourceService.canSaveAs(this.shell.currentWidget),
execute: () => {
const { currentWidget } = this.shell;
// No clue what could have happened between `isEnabled` and `execute`
// when fetching currentWidget, so better to double-check:
if (this.saveResourceService.canSaveAs(currentWidget)) {
this.saveResourceService.saveAs(currentWidget);
}
else {
this.messageService.error(nls_1.nls.localize('theia/workspace/failSaveAs', 'Cannot run "{0}" for the current widget.', common_commands_1.CommonCommands.SAVE_AS.label));
}
},
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SAVE_WITHOUT_FORMATTING, {
execute: () => this.save({ formatType: 2 /* FormatType.OFF */ })
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SAVE_ALL, {
execute: () => this.shell.saveAll({ formatType: 3 /* FormatType.DIRTY */ })
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.ABOUT_COMMAND, {
execute: () => this.openAbout()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.OPEN_VIEW, {
execute: () => { var _a; return (_a = this.quickInputService) === null || _a === void 0 ? void 0 : _a.open(quick_view_service_1.QuickViewService.PREFIX); }
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SELECT_COLOR_THEME, {
execute: () => this.selectColorTheme()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.SELECT_ICON_THEME, {
execute: () => this.selectIconTheme()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PIN_TAB, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: title => Boolean(title && !(0, widgets_1.isPinned)(title)),
execute: title => this.togglePinned(title),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.UNPIN_TAB, new current_widget_command_adapter_1.CurrentWidgetCommandAdapter(this.shell, {
isEnabled: title => Boolean(title && (0, widgets_1.isPinned)(title)),
execute: title => this.togglePinned(title),
}));
commandRegistry.registerCommand(common_commands_1.CommonCommands.CONFIGURE_DISPLAY_LANGUAGE, {
execute: () => this.configureDisplayLanguage()
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.TOGGLE_BREADCRUMBS, {
execute: () => this.toggleBreadcrumbs(),
isToggled: () => this.isBreadcrumbsEnabled(),
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.NEW_UNTITLED_TEXT_FILE, {
execute: async () => {
const untitledUri = this.untitledResourceResolver.createUntitledURI('', await this.workingDirProvider.getUserWorkingDir());
this.untitledResourceResolver.resolve(untitledUri);
const editor = await (0, opener_service_1.open)(this.openerService, untitledUri);
// Wait for all of the listeners of the `onDidOpen` event to be notified
await (0, promise_util_1.timeout)(50);
// Afterwards, we can return from the command with the newly created editor
// If we don't wait, we return from the command before the plugin API has been notified of the new editor
return editor;
}
});
commandRegistry.registerCommand(common_commands_1.CommonCommands.PICK_NEW_FILE, {
execute: async () => this.showNewFilePicker()
});
for (const [index, ordinal] of this.getOrdinalNumbers().entries()) {
commandRegistry.registerCommand({ id: `workbench.action.focus${ordinal}EditorGroup`, label: index === 0 ? nls_1.nls.localizeByDefault('Focus First Editor Group') : '', category: nls_1.nls.localize(common_commands_1.CommonCommands.VIEW_CATEGORY_KEY, common_commands_1.CommonCommands.VIEW_CATEGORY) }, {
isEnabled: () => this.shell.mainAreaTabBars.length > index,
execute: () => {
var _a, _b;
const widget = (_b = (_a = this.shell.mainAreaTabBars[index]) === null || _a === void 0 ? void 0 : _a.currentTitle) === null || _b === void 0 ? void 0 : _b.owner;
if (widget) {
this.shell.activateWidget(widget.id);
}
}
});
}
}
getOrdinalNumbers() {
return ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth'];
}
isElectron() {
return environment_1.environment.electron.is();
}
togglePinned(title) {
if (title) {
(0, widgets_1.togglePinned)(title);
this.updatePinnedKey();
}
}
registerKeybindings(registry) {
if (exports.supportCut) {
registry.registerKeybinding({
command: common_commands_1.CommonCommands.CUT.id,
keybinding: 'ctrlcmd+x'
});
}
if (exports.supportCopy) {
registry.registerKeybinding({
command: common_commands_1.CommonCommands.COPY.id,
keybinding: 'ctrlcmd+c'
});
}
if (exports.supportPaste) {
registry.registerKeybinding({
command: common_commands_1.CommonCommands.PASTE.id,
keybinding: 'ctrlcmd+v'
});
}
registry.registerKeybinding({
command: common_commands_1.CommonCommands.COPY_PATH.id,
keybinding: os_1.isWindows ? 'shift+alt+c' : 'ctrlcmd+alt+c',
when: '!editorFocus'
});
registry.registerKeybindings(
// Edition
{
command: common_commands_1.CommonCommands.UNDO.id,
keybinding: 'ctrlcmd+z'
}, {
command: common_commands_1.CommonCommands.REDO.id,
keybinding: os_1.isOSX ? 'ctrlcmd+shift+z' : 'ctrlcmd+y'
}, {
command: common_commands_1.CommonCommands.SELECT_ALL.id,
keybinding: 'ctrlcmd+a'
}, {
command: common_commands_1.CommonCommands.FIND.id,
keybinding: 'ctrlcmd+f'
}, {
command: common_commands_1.CommonCommands.REPLACE.id,
keybinding: 'ctrlcmd+alt+f'
},
// Tabs
{
command: common_commands_1.CommonCommands.NEXT_TAB.id,
keybinding: 'ctrl+tab'
}, {
command: common_commands_1.CommonCommands.NEXT_TAB.id,
keybinding: 'ctrlcmd+alt+d'
}, {
command: common_commands_1.CommonCommands.PREVIOUS_TAB.id,
keybinding: 'ctrl+shift+tab'
}, {
command: common_commands_1.CommonCommands.PREVIOUS_TAB.id,
keybinding: 'ctrlcmd+alt+a'
}, {
command: common_commands_1.CommonCommands.CLOSE_MAIN_TAB.id,
keybinding: this.isElectron() ? (os_1.isWindows ? 'ctrl+f4' : 'ctrlcmd+w') : 'alt+w'
}, {
command: common_commands_1.CommonCommands.CLOSE_OTHER_MAIN_TABS.id,
keybinding: 'ctrlcmd+alt+t'
}, {
command: common_commands_1.CommonCommands.CLOSE_ALL_MAIN_TABS.id,
keybinding: this.isElectron() ? 'ctrlCmd+k ctrlCmd+w' : 'alt+shift+w'
},
// Panels
{
command: common_commands_1.CommonCommands.COLLAPSE_PANEL.id,
keybinding: 'alt+c'
}, {
command: common_commands_1.CommonCommands.TOGGLE_BOTTOM_PANEL.id,
keybinding: 'ctrlcmd+j',
}, {
command: common_commands_1.CommonCommands.COLLAPSE_ALL_PANELS.id,
keybinding: 'alt+shift+c',
}, {
command: common_commands_1.CommonCommands.TOGGLE_MAXIMIZED.id,
keybinding: 'alt+m',
},
// Saving
{
command: common_commands_1.CommonCommands.SAVE.id,
keybinding: 'ctrlcmd+s'
}, {
command: common_commands_1.CommonCommands.SAVE_WITHOUT_FORMATTING.id,
keybinding: 'ctrlcmd+k s'
}, {
command: common_commands_1.CommonCommands.SAVE_ALL.id,
keybinding: 'ctrlcmd+alt+s'
},
// Theming
{
command: common_commands_1.CommonCommands.SELECT_COLOR_THEME.id,
keybinding: 'ctrlcmd+k ctrlcmd+t'
}, {
command: common_commands_1.CommonCommands.PIN_TAB.id,
keybinding: 'ctrlcmd+k shift+enter',
when: '!activeEditorIsPinned'
}, {
command: common_commands_1.CommonCommands.UNPIN_TAB.id,
keybinding: 'ctrlcmd+k shift+enter',
when: 'activeEditorIsPinned'
}, {
command: common_commands_1.CommonCommands.NEW_UNTITLED_TEXT_FILE.id,
keybinding: this.isElectron() ? 'ctrlcmd+n' : 'alt+n',
}, {
command: common_commands_1.CommonCommands.PICK_NEW_FILE.id,
keybinding: 'ctrlcmd+alt+n'
});
for (const [index, ordinal] of this.getOrdinalNumbers().entries()) {
registry.registerKeybinding({
command: `workbench.action.focus${ordinal}EditorGroup`,
keybinding: `ctrlcmd+${(index + 1) % 10}`,
});
}
}
async save(options) {
const widget = this.shell.currentWidget;
this.saveResourceService.save(widget, options);
}
async openAbout() {
this.aboutDialog.open(false);
}
/**
* registers event listener which make sure that
* window doesn't get closed if CMD/CTRL W is pressed.
* Too many users have that in their muscle memory.
* Chrome doesn't let us rebind or prevent default the keybinding, so this
* at least doesn't close the window immediately.
*/
registerCtrlWHandling() {
function isCtrlCmd(event) {
return (os_1.isOSX && event.metaKey) || (!os_1.isOSX && event.ctrlKey);
}
window.document.addEventListener('keydown', event => {
this.shouldPreventClose = isCtrlCmd(event) && event.code === 'KeyW';
});
window.document.addEventListener('keyup', () => {
this.shouldPreventClose = false;
});
}
onWillStop() {
if (this.shouldPreventClose || this.shell.canSaveAll()) {
return {
reason: 'Dirty editors present',
action: async () => {
const captionsToSave = this.unsavedTabsCaptions();
const untitledCaptionsToSave = this.unsavedUntitledTabsCaptions();
const shouldExit = await this.confirmExitWithOrWithoutSaving(captionsToSave, async () => {
await this.saveDirty(untitledCaptionsToSave);
await this.shell.saveAll();
});
const allSavedOrDoNotSave = (shouldExit === true && untitledCaptionsToSave.length === 0 // Should save and cancel if any captions failed to save
) || shouldExit === false; // Do not save
this.shouldPreventClose = !allSavedOrDoNotSave;
return allSavedOrDoNotSave;
}
};
}
}
// Asks the user to confirm whether they want to exit with or without saving the changes
async confirmExitWithOrWithoutSaving(captionsToSave, performSave) {
const div = document.createElement('div');
div.innerText = nls_1.nls.localizeByDefault("Your changes will be lost if you don't save them.");
let result;
if (captionsToSave.length > 0) {
const span = document.createElement('span');
span.appendChild(document.createElement('br'));
captionsToSave.forEach(cap => {
const b = document.createElement('b');
b.innerText = cap;
span.appendChild(b);
span.appendChild(document.createElement('br'));
});
span.appendChild(document.createElement('br'));
div.appendChild(span);
result = await new dialogs_1.ConfirmSaveDialog({
title: nls_1.nls.localizeByDefault('Do you want to save the changes to the following {0} files?', captionsToSave.length),
msg: div,
dontSave: nls_1.nls.localizeByDefault("Don't Save"),
save: nls_1.nls.localizeByDefault('Save All'),
cancel: dialogs_1.Dialog.CANCEL
}).open();
if (result) {
await performSave();
}
}
else {
// fallback if not passed with an empty caption-list.
result = (0, dialogs_1.confirmExit)();
}
if (result !== undefined) {
return result === true;
}
else {
return undefined;
}
;
}
unsavedTabsCaptions() {
return this.shell.widgets
.filter(widget => this.saveResourceService.canSave(widget))
.map(widget => widget.title.label);
}
unsavedUntitledTabsCaptions() {
return this.shell.widgets.filter(widget => { var _a; return ((_a = navigatable_1.NavigatableWidget.getUri(widget)) === null || _a === void 0 ? void 0 : _a.scheme) === common_1.UNTITLED_SCHEME && this.saveResourceService.canSaveAs(widget); });
}
async configureDisplayLanguage() {
var _a, _b;
const languageInfo = await this.languageQuickPickService.pickDisplayLanguage();
if (languageInfo && !nls_1.nls.isSelectedLocale(languageInfo.languageId) && await this.confirmRestart((_b = (_a = languageInfo.localizedLanguageName) !== null && _a !== void 0 ? _a : languageInfo.languageName) !== null && _b !== void 0 ? _b : languageInfo.languageId)) {
nls_1.nls.setLocale(languageInfo.languageId);
this.windowService.setSafeToShutDown();
this.windowService.reload();
}
}
/**
* saves any dirty widget in toSave
* side effect - will pop all widgets from toSave that was saved
* @param toSave
*/
async saveDirty(toSave) {
for (const widget of toSave) {
const saveable = saveable_1.Saveable.get(widget);
if (saveable === null || saveable === void 0 ? void 0 : saveable.dirty) {
await this.saveResourceService.save(widget);
if (!this.saveResourceService.canSave(widget)) {
toSave.pop();
}
}
}
}
toggleBreadcrumbs() {
const value = this.preferenceService.get('breadcrumbs.enabled');
this.preferenceService.set('breadcrumbs.enabled', !value, common_1.PreferenceScope.User);
}
isBreadcrumbsEnabled() {
return !!this.preferenceService.get('breadcrumbs.enabled');
}
async confirmRestart(languageName) {
const appName = frontend_application_config_provider_1.FrontendApplicationConfigProvider.get().applicationName;
const shouldRestart = await new dialogs_1.ConfirmDialog({
title: nls_1.nls.localizeByDefault('Restart {0} to switch to {1}?', appName, languageName),
msg: nls_1.nls.localizeByDefault('To ch