Skip to content

Commit

Permalink
fix #6325: fix command execution for inline editors
Browse files Browse the repository at this point in the history
Since migrating to Monaco 0.18 we handle monaco keybindings to avoid some bugs, like inability to close the reference widget. It led to a regression for inline editors, since they are not tracked by editor managers.

Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Oct 4, 2019
1 parent 2186ccb commit ee4cbce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
18 changes: 10 additions & 8 deletions packages/monaco/src/browser/monaco-command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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));
}

Expand Down
24 changes: 23 additions & 1 deletion packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit ee4cbce

Please sign in to comment.