diff --git a/packages/editor/src/browser/editor-command.ts b/packages/editor/src/browser/editor-command.ts index dd34b5cdc2369..ba2e4d874caf9 100644 --- a/packages/editor/src/browser/editor-command.ts +++ b/packages/editor/src/browser/editor-command.ts @@ -69,6 +69,11 @@ export namespace EditorCommands { category: EDITOR_CATEGORY, label: 'Change File Encoding' }; + export const REVERT_AND_CLOSE: Command = { + id: 'workbench.action.revertAndCloseActiveEditor', + category: 'View', + label: 'Revert and Close Editor' + }; /** * Command for going back to the last editor navigation location. @@ -229,6 +234,7 @@ export class EditorCommandContribution implements CommandContribution { registry.registerCommand(EditorCommands.CONFIG_EOL); registry.registerCommand(EditorCommands.INDENT_USING_SPACES); registry.registerCommand(EditorCommands.INDENT_USING_TABS); + registry.registerCommand(EditorCommands.REVERT_AND_CLOSE); registry.registerCommand(EditorCommands.CHANGE_LANGUAGE, { isEnabled: () => this.canConfigureLanguage(), isVisible: () => this.canConfigureLanguage(), diff --git a/packages/monaco/src/browser/monaco-command.ts b/packages/monaco/src/browser/monaco-command.ts index 52d1530cc56bd..d13c931b51a8c 100644 --- a/packages/monaco/src/browser/monaco-command.ts +++ b/packages/monaco/src/browser/monaco-command.ts @@ -17,8 +17,8 @@ import { injectable, inject, optional } from '@theia/core/shared/inversify'; import { Position, Location } from '@theia/core/shared/vscode-languageserver-types'; import { CommandContribution, CommandRegistry, CommandHandler } from '@theia/core/lib/common/command'; -import { CommonCommands, QuickInputService } from '@theia/core/lib/browser'; -import { EditorCommands } from '@theia/editor/lib/browser'; +import { CommonCommands, QuickInputService, ApplicationShell } from '@theia/core/lib/browser'; +import { EditorCommands, EditorManager } from '@theia/editor/lib/browser'; import { MonacoEditor } from './monaco-editor'; import { MonacoCommandRegistry, MonacoEditorCommandHandler } from './monaco-command-registry'; import { MonacoEditorService } from './monaco-editor-service'; @@ -69,6 +69,12 @@ export class MonacoEditorCommandHandlers implements CommandContribution { @inject(monaco.contextKeyService.ContextKeyService) protected readonly contextKeyService: monaco.contextKeyService.ContextKeyService; + @inject(ApplicationShell) + protected readonly shell: ApplicationShell; + + @inject(EditorManager) + protected editorManager: EditorManager; + registerCommands(): void { this.registerMonacoCommands(); this.registerEditorCommandHandlers(); @@ -185,6 +191,7 @@ export class MonacoEditorCommandHandlers implements CommandContribution { this.monacoCommandRegistry.registerHandler(EditorCommands.CONFIG_EOL.id, this.newConfigEolHandler()); this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_SPACES.id, this.newConfigTabSizeHandler(true)); this.monacoCommandRegistry.registerHandler(EditorCommands.INDENT_USING_TABS.id, this.newConfigTabSizeHandler(false)); + this.monacoCommandRegistry.registerHandler(EditorCommands.REVERT_AND_CLOSE.id, this.newRevertAndCloseActiveEditorHandler()); } protected newShowReferenceHandler(): MonacoEditorCommandHandler { @@ -222,10 +229,11 @@ export class MonacoEditorCommandHandlers implements CommandContribution { protected configureEol(editor: MonacoEditor): void { const items = ['LF', 'CRLF'].map(lineEnding => - ({ - label: lineEnding, - execute: () => this.setEol(editor, lineEnding) - })); + ({ + label: lineEnding, + execute: () => this.setEol(editor, lineEnding) + }) + ); this.quickInputService?.showQuickPick(items, { placeholder: 'Select End of Line Sequence' }); } @@ -251,15 +259,37 @@ export class MonacoEditorCommandHandlers implements CommandContribution { const { tabSize } = model.getOptions(); const sizes = Array.from(Array(8), (_, x) => x + 1); const tabSizeOptions = sizes.map(size => - ({ - label: size === tabSize ? `${size} Configured Tab Size` : size.toString(), - // eslint-disable-next-line @typescript-eslint/no-explicit-any - execute: (quickPick: any, lookFor: string) => model.updateOptions({ - tabSize: size || tabSize, - insertSpaces: useSpaces + ({ + label: size === tabSize ? `${size} Configured Tab Size` : size.toString(), + // eslint-disable-next-line @typescript-eslint/no-explicit-any + execute: (quickPick: any, lookFor: string) => model.updateOptions({ + tabSize: size || tabSize, + insertSpaces: useSpaces + }) }) - })); + ); this.quickInputService?.showQuickPick(tabSizeOptions, { placeholder: 'Select Tab Size for Current File' }); } } + + protected newRevertAndCloseActiveEditorHandler(): MonacoEditorCommandHandler { + return { + execute: async () => this.revertAndCloseActiveEditor() + }; + } + + protected async revertAndCloseActiveEditor(): Promise { + const editor = this.editorManager.currentEditor; + if (editor) { + const monacoEditor = MonacoEditor.getCurrent(this.editorManager); + if (monacoEditor) { + try { + await monacoEditor.document.revert(); + editor.close(); + } catch (error) { + await this.shell.closeWidget(editor.id, { save: false }); + } + } + } + } } diff --git a/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts b/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts index 58bfdacc39629..16e41a4c23b18 100755 --- a/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts +++ b/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts @@ -54,7 +54,6 @@ import { inject, injectable, optional } from '@theia/core/shared/inversify'; import { Position } from '@theia/plugin-ext/lib/common/plugin-api-rpc'; import { URI } from '@theia/core/shared/vscode-uri'; import { PluginServer } from '@theia/plugin-ext/lib/common/plugin-protocol'; -import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; import { TerminalFrontendContribution } from '@theia/terminal/lib/browser/terminal-frontend-contribution'; import { QuickOpenWorkspace } from '@theia/workspace/lib/browser/quick-open-workspace'; import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service'; @@ -399,23 +398,6 @@ export class PluginVscodeCommandsContribution implements CommandContribution { } }); - commands.registerCommand({ id: 'workbench.action.revertAndCloseActiveEditor' }, { - execute: async () => { - const editor = this.editorManager.currentEditor; - if (editor) { - const monacoEditor = MonacoEditor.getCurrent(this.editorManager); - if (monacoEditor) { - try { - await monacoEditor.document.revert(); - editor.close(); - } catch (error) { - await this.shell.closeWidget(editor.id, { save: false }); - } - } - } - } - }); - /** * TODO: * Open Next: workbench.action.openNextRecentlyUsedEditorInGroup