Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix command execution for inline editors #6328

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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