diff --git a/src/vs/workbench/api/electron-browser/mainThreadCommands.ts b/src/vs/workbench/api/electron-browser/mainThreadCommands.ts index 4351231a41b0c721ebac95e29e2eda3833de2a7a..050e27b59d03c83b5f3a22cb7763c86ac42ef9eb 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadCommands.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadCommands.ts @@ -9,6 +9,7 @@ import { IDisposable } from 'vs/base/common/lifecycle'; import { TPromise } from 'vs/base/common/winjs.base'; import { ExtHostContext, MainThreadCommandsShape, ExtHostCommandsShape, MainContext, IExtHostContext } from '../node/extHost.protocol'; import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers'; +import { revive } from 'vs/base/common/marshalling'; @extHostNamedCustomer(MainContext.MainThreadCommands) export class MainThreadCommands implements MainThreadCommandsShape { @@ -70,6 +71,9 @@ export class MainThreadCommands implements MainThreadCommandsShape { } $executeCommand(id: string, args: any[]): Thenable { + for (let i = 0; i < args.length; i++) { + args[i] = revive(args[i], 0); + } return this._commandService.executeCommand(id, ...args); } diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 0b7b069eab50d67f55b7dc5933e537a53bbb6129..ed498be9850d5b64fc681d5ec43b19ca83dd6acb 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -731,7 +731,7 @@ export interface ExtHostWindowShape { // --- proxy identifiers export const MainContext = { - MainThreadCommands: >createMainId('MainThreadCommands', ProxyType.CustomMarshaller), + MainThreadCommands: >createMainId('MainThreadCommands'), MainThreadConfiguration: createMainId('MainThreadConfiguration', ProxyType.CustomMarshaller), MainThreadDebugService: createMainId('MainThreadDebugService', ProxyType.CustomMarshaller), MainThreadDecorations: createMainId('MainThreadDecorations'), @@ -761,7 +761,7 @@ export const MainContext = { }; export const ExtHostContext = { - ExtHostCommands: createExtId('ExtHostCommands', ProxyType.CustomMarshaller), + ExtHostCommands: createExtId('ExtHostCommands'), ExtHostConfiguration: createExtId('ExtHostConfiguration', ProxyType.CustomMarshaller), ExtHostDiagnostics: createExtId('ExtHostDiagnostics'), ExtHostDebugService: createExtId('ExtHostDebugService', ProxyType.CustomMarshaller), diff --git a/src/vs/workbench/api/node/extHostCommands.ts b/src/vs/workbench/api/node/extHostCommands.ts index 1167a321ffcc73b77e5ea5866f01665b86531151..97ee3eb8232086d281a167f7c3657175321fd991 100644 --- a/src/vs/workbench/api/node/extHostCommands.ts +++ b/src/vs/workbench/api/node/extHostCommands.ts @@ -15,6 +15,7 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays'; import * as modes from 'vs/editor/common/modes'; import * as vscode from 'vscode'; import { ILogService } from 'vs/platform/log/common/log'; +import { revive } from 'vs/base/common/marshalling'; interface CommandHandler { callback: Function; @@ -28,18 +29,21 @@ export interface ArgumentProcessor { export class ExtHostCommands implements ExtHostCommandsShape { - private _commands = new Map(); - private _proxy: MainThreadCommandsShape; - private _converter: CommandsConverter; - private _argumentProcessors: ArgumentProcessor[] = []; + private readonly _commands = new Map(); + private readonly _proxy: MainThreadCommandsShape; + private readonly _converter: CommandsConverter; + private readonly _logService: ILogService; + private readonly _argumentProcessors: ArgumentProcessor[]; constructor( mainContext: IMainContext, heapService: ExtHostHeapService, - private logService: ILogService + logService: ILogService ) { this._proxy = mainContext.getProxy(MainContext.MainThreadCommands); this._converter = new CommandsConverter(this, heapService); + this._logService = logService; + this._argumentProcessors = [{ processArgument(a) { return revive(a, 0); } }]; } get converter(): CommandsConverter { @@ -51,7 +55,7 @@ export class ExtHostCommands implements ExtHostCommandsShape { } registerCommand(id: string, callback: (...args: any[]) => T | Thenable, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable { - this.logService.trace('ExtHostCommands#registerCommand', id); + this._logService.trace('ExtHostCommands#registerCommand', id); if (!id.trim().length) { throw new Error('invalid id'); @@ -72,12 +76,12 @@ export class ExtHostCommands implements ExtHostCommandsShape { } executeCommand(id: string, ...args: any[]): Thenable { - this.logService.trace('ExtHostCommands#executeCommand', id); + this._logService.trace('ExtHostCommands#executeCommand', id); if (this._commands.has(id)) { // we stay inside the extension host and support // to pass any kind of parameters around - return this.$executeContributedCommand(id, ...args); + return this._executeContributedCommand(id, args); } else { // automagically convert some argument types @@ -99,17 +103,10 @@ export class ExtHostCommands implements ExtHostCommandsShape { return this._proxy.$executeCommand(id, args); } - } - $executeContributedCommand(id: string, ...args: any[]): Thenable { - let command = this._commands.get(id); - if (!command) { - return Promise.reject(new Error(`Contributed command '${id}' does not exist.`)); - } - - let { callback, thisArg, description } = command; - + private _executeContributedCommand(id: string, args: any[]): Thenable { + let { callback, thisArg, description } = this._commands.get(id); if (description) { for (let i = 0; i < description.args.length; i++) { try { @@ -120,24 +117,26 @@ export class ExtHostCommands implements ExtHostCommandsShape { } } - args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg)); - try { let result = callback.apply(thisArg, args); return Promise.resolve(result); } catch (err) { - // console.log(err); - // try { - // console.log(toErrorMessage(err)); - // } catch (err) { - // // - // } + this._logService.error(err, id); return Promise.reject(new Error(`Running the contributed command:'${id}' failed.`)); } } + $executeContributedCommand(id: string, ...args: any[]): Thenable { + if (!this._commands.has(id)) { + return Promise.reject(new Error(`Contributed command '${id}' does not exist.`)); + } else { + args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg)); + return this._executeContributedCommand(id, args); + } + } + getCommands(filterUnderscoreCommands: boolean = false): Thenable { - this.logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands); + this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands); return this._proxy.$getCommands().then(result => { if (filterUnderscoreCommands) {