diff --git a/packages/monaco/src/browser/monaco-command-registry.ts b/packages/monaco/src/browser/monaco-command-registry.ts index b8530b9edc08a..e8faf0f237715 100644 --- a/packages/monaco/src/browser/monaco-command-registry.ts +++ b/packages/monaco/src/browser/monaco-command-registry.ts @@ -16,8 +16,9 @@ import { injectable, inject } from 'inversify'; import { Command, CommandHandler, CommandRegistry, SelectionService } from '@theia/core'; -import { EditorManager, TextEditorSelection } from '@theia/editor/lib/browser'; +import { TextEditorSelection } from '@theia/editor/lib/browser'; import { MonacoEditor } from './monaco-editor'; +import { MonacoEditorProvider } from './monaco-editor-provider'; export interface MonacoEditorCommandHandler { // tslint:disable-next-line:no-any @@ -30,11 +31,12 @@ export class MonacoCommandRegistry { public static MONACO_COMMAND_PREFIX = 'monaco.'; - constructor( - @inject(CommandRegistry) protected readonly commands: CommandRegistry, - @inject(EditorManager) protected readonly editorManager: EditorManager, - @inject(SelectionService) protected readonly selectionService: SelectionService - ) { } + @inject(MonacoEditorProvider) + protected readonly monacoEditors: MonacoEditorProvider; + + @inject(CommandRegistry) protected readonly commands: CommandRegistry; + + @inject(SelectionService) protected readonly selectionService: SelectionService; protected prefix(command: string): string { return MonacoCommandRegistry.MONACO_COMMAND_PREFIX + command; @@ -66,7 +68,7 @@ export class MonacoCommandRegistry { // tslint:disable-next-line:no-any protected execute(monacoHandler: MonacoEditorCommandHandler, ...args: any[]): any { - const editor = MonacoEditor.getCurrent(this.editorManager); + const editor = this.monacoEditors.current; if (editor) { editor.focus(); return Promise.resolve(monacoHandler.execute(editor, ...args)); @@ -76,7 +78,7 @@ export class MonacoCommandRegistry { // tslint:disable-next-line:no-any protected isEnabled(monacoHandler: MonacoEditorCommandHandler, ...args: any[]): boolean { - const editor = MonacoEditor.getCurrent(this.editorManager); + const editor = this.monacoEditors.current; return !!editor && (!monacoHandler.isEnabled || monacoHandler.isEnabled(editor, ...args)); } diff --git a/packages/monaco/src/browser/monaco-editor-provider.ts b/packages/monaco/src/browser/monaco-editor-provider.ts index cc31a882f77e9..957f7cac62d63 100644 --- a/packages/monaco/src/browser/monaco-editor-provider.ts +++ b/packages/monaco/src/browser/monaco-editor-provider.ts @@ -19,7 +19,7 @@ import URI from '@theia/core/lib/common/uri'; import { EditorPreferenceChange, EditorPreferences, TextEditor, DiffNavigator } from '@theia/editor/lib/browser'; import { DiffUris } from '@theia/core/lib/browser/diff-uris'; import { inject, injectable } from 'inversify'; -import { DisposableCollection, deepClone } from '@theia/core/lib/common'; +import { DisposableCollection, deepClone, Disposable, } from '@theia/core/lib/common'; import { MonacoToProtocolConverter, ProtocolToMonacoConverter, TextDocumentSaveReason } from 'monaco-languageclient'; import { MonacoCommandServiceFactory } from './monaco-command-service'; import { MonacoContextMenuService } from './monaco-context-menu'; @@ -48,6 +48,16 @@ export class MonacoEditorProvider { private isWindowsBackend: boolean = false; + protected _current: MonacoEditor | undefined; + /** + * Returns the last focused MonacoEditor. + * It takes into account inline editors as well. + * If you are interested only in standalone editors then use `MonacoEditor.getCurrent(EditorManager)` + */ + get current(): MonacoEditor | undefined { + return this._current; + } + constructor( @inject(MonacoEditorService) protected readonly codeEditorService: MonacoEditorService, @inject(MonacoTextModelService) protected readonly textModelService: MonacoTextModelService, @@ -119,6 +129,18 @@ export class MonacoEditorProvider { commandService.setDelegate(standaloneCommandService); this.installQuickOpenService(editor); this.installReferencesController(editor); + + toDispose.push(editor.onFocusChanged(focused => { + if (focused) { + this._current = editor; + } + })); + toDispose.push(Disposable.create(() => { + if (this._current === editor) { + this._current = undefined; + } + })); + return editor; }