diff --git a/packages/core/src/common/command.ts b/packages/core/src/common/command.ts index 3ad6eb3fc2188..f6482490029d5 100644 --- a/packages/core/src/common/command.ts +++ b/packages/core/src/common/command.ts @@ -281,7 +281,7 @@ export class CommandRegistry implements CommandService { return result; } const argsMessage = args && args.length > 0 ? ` (args: ${JSON.stringify(args)})` : ''; - throw new Error(`The command '${commandId}' cannot be executed. There are no active handlers available for the command.${argsMessage}`); + throw Object.assign(new Error(`The command '${commandId}' cannot be executed. There are no active handlers available for the command.${argsMessage}`), { code: 'NO_ACTIVE_HANDLER' }); } protected async fireWillExecuteCommand(commandId: string): Promise { diff --git a/packages/monaco/src/browser/monaco-editor.ts b/packages/monaco/src/browser/monaco-editor.ts index 91a5934286d5c..11fd880a200b0 100644 --- a/packages/monaco/src/browser/monaco-editor.ts +++ b/packages/monaco/src/browser/monaco-editor.ts @@ -322,6 +322,11 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor { this.toDispose.dispose(); } + // tslint:disable-next-line:no-any + trigger(source: string, handlerId: string, payload: any): void { + this.editor.trigger(source, handlerId, payload); + } + getControl(): IStandaloneCodeEditor { return this.editor; } diff --git a/packages/monaco/src/typings/monaco/index.d.ts b/packages/monaco/src/typings/monaco/index.d.ts index c71fe10fa5f73..4e5f2b07213c6 100644 --- a/packages/monaco/src/typings/monaco/index.d.ts +++ b/packages/monaco/src/typings/monaco/index.d.ts @@ -47,6 +47,7 @@ declare module monaco.editor { export interface IStandaloneCodeEditor extends CommonCodeEditor { setDecorations(decorationTypeKey: string, ranges: IDecorationOptions[]): void; setDecorationsFast(decorationTypeKey: string, ranges: IRange[]): void; + trigger(source: string, handlerId: string, payload: any): void } // https://github.com/TypeFox/vscode/blob/monaco/0.18.0/src/vs/editor/browser/widget/codeEditorWidget.ts#L107 diff --git a/packages/plugin-ext-vscode/package.json b/packages/plugin-ext-vscode/package.json index 2677905d03010..17f5f8f96bf98 100644 --- a/packages/plugin-ext-vscode/package.json +++ b/packages/plugin-ext-vscode/package.json @@ -5,6 +5,7 @@ "dependencies": { "@theia/core": "^0.13.0", "@theia/editor": "^0.13.0", + "@theia/monaco": "^0.13.0", "@theia/plugin": "^0.13.0", "@theia/plugin-ext": "^0.13.0", "@theia/workspace": "^0.13.0", 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 865f3b1c41975..cffc578c1e39f 100644 --- a/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts +++ b/packages/plugin-ext-vscode/src/browser/plugin-vscode-commands-contribution.ts @@ -28,6 +28,7 @@ import { fromViewColumn, toDocumentSymbol } from '@theia/plugin-ext/lib/plugin/t import { ViewColumn } from '@theia/plugin-ext/lib/plugin/types-impl'; import { WorkspaceCommands } from '@theia/workspace/lib/browser'; import { DiffService } from '@theia/workspace/lib/browser/diff-service'; +import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; import { inject, injectable } from 'inversify'; import URI from 'vscode-uri'; @@ -137,6 +138,14 @@ export class PluginVscodeCommandsContribution implements CommandContribution { commands.registerCommand({ id: 'workbench.action.files.openFolder' }, { execute: () => commands.executeCommand(WorkspaceCommands.OPEN_FOLDER.id) }); + commands.registerCommand({ id: 'default:type' }, { + execute: args => { + const editor = MonacoEditor.getCurrent(this.editorManager); + if (editor) { + editor.trigger('keyboard', 'type', args); + } + } + }); commands.registerCommand({ id: 'workbench.action.files.save', }, { execute: (uri?: monaco.Uri) => { if (uri) { diff --git a/packages/plugin-ext/src/main/browser/command-registry-main.ts b/packages/plugin-ext/src/main/browser/command-registry-main.ts index 84c82f5a945d8..916f959c717d2 100644 --- a/packages/plugin-ext/src/main/browser/command-registry-main.ts +++ b/packages/plugin-ext/src/main/browser/command-registry-main.ts @@ -72,8 +72,20 @@ export class CommandRegistryMainImpl implements CommandRegistryMain, Disposable } // tslint:disable-next-line:no-any - $executeCommand(id: string, ...args: any[]): PromiseLike { - return this.delegate.executeCommand(id, ...args); + async $executeCommand(id: string, ...args: any[]): Promise { + if (!this.delegate.getCommand(id)) { + throw new Error(`Command with id '${id}' is not registered.`); + } + try { + return await this.delegate.executeCommand(id, ...args); + } catch (e) { + // Command handler may be not active at the moment so the error must be caught. See https://github.com/eclipse-theia/theia/pull/6687#discussion_r354810079 + if ('code' in e && e['code'] === 'NO_ACTIVE_HANDLER') { + return; + } else { + throw e; + } + } } $getKeyBinding(commandId: string): PromiseLike {