提交 8f18e874 编写于 作者: J Johannes Rieken

ExtHostCommands, MainThreadCommands, #40169

上级 785aac7d
...@@ -9,6 +9,7 @@ import { IDisposable } from 'vs/base/common/lifecycle'; ...@@ -9,6 +9,7 @@ import { IDisposable } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base'; import { TPromise } from 'vs/base/common/winjs.base';
import { ExtHostContext, MainThreadCommandsShape, ExtHostCommandsShape, MainContext, IExtHostContext } from '../node/extHost.protocol'; import { ExtHostContext, MainThreadCommandsShape, ExtHostCommandsShape, MainContext, IExtHostContext } from '../node/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers'; import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import { revive } from 'vs/base/common/marshalling';
@extHostNamedCustomer(MainContext.MainThreadCommands) @extHostNamedCustomer(MainContext.MainThreadCommands)
export class MainThreadCommands implements MainThreadCommandsShape { export class MainThreadCommands implements MainThreadCommandsShape {
...@@ -70,6 +71,9 @@ export class MainThreadCommands implements MainThreadCommandsShape { ...@@ -70,6 +71,9 @@ export class MainThreadCommands implements MainThreadCommandsShape {
} }
$executeCommand<T>(id: string, args: any[]): Thenable<T> { $executeCommand<T>(id: string, args: any[]): Thenable<T> {
for (let i = 0; i < args.length; i++) {
args[i] = revive(args[i], 0);
}
return this._commandService.executeCommand<T>(id, ...args); return this._commandService.executeCommand<T>(id, ...args);
} }
......
...@@ -731,7 +731,7 @@ export interface ExtHostWindowShape { ...@@ -731,7 +731,7 @@ export interface ExtHostWindowShape {
// --- proxy identifiers // --- proxy identifiers
export const MainContext = { export const MainContext = {
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands', ProxyType.CustomMarshaller), MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands'),
MainThreadConfiguration: createMainId<MainThreadConfigurationShape>('MainThreadConfiguration', ProxyType.CustomMarshaller), MainThreadConfiguration: createMainId<MainThreadConfigurationShape>('MainThreadConfiguration', ProxyType.CustomMarshaller),
MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService', ProxyType.CustomMarshaller), MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService', ProxyType.CustomMarshaller),
MainThreadDecorations: createMainId<MainThreadDecorationsShape>('MainThreadDecorations'), MainThreadDecorations: createMainId<MainThreadDecorationsShape>('MainThreadDecorations'),
...@@ -761,7 +761,7 @@ export const MainContext = { ...@@ -761,7 +761,7 @@ export const MainContext = {
}; };
export const ExtHostContext = { export const ExtHostContext = {
ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands', ProxyType.CustomMarshaller), ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands'),
ExtHostConfiguration: createExtId<ExtHostConfigurationShape>('ExtHostConfiguration', ProxyType.CustomMarshaller), ExtHostConfiguration: createExtId<ExtHostConfigurationShape>('ExtHostConfiguration', ProxyType.CustomMarshaller),
ExtHostDiagnostics: createExtId<ExtHostDiagnosticsShape>('ExtHostDiagnostics'), ExtHostDiagnostics: createExtId<ExtHostDiagnosticsShape>('ExtHostDiagnostics'),
ExtHostDebugService: createExtId<ExtHostDebugServiceShape>('ExtHostDebugService', ProxyType.CustomMarshaller), ExtHostDebugService: createExtId<ExtHostDebugServiceShape>('ExtHostDebugService', ProxyType.CustomMarshaller),
......
...@@ -15,6 +15,7 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays'; ...@@ -15,6 +15,7 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays';
import * as modes from 'vs/editor/common/modes'; import * as modes from 'vs/editor/common/modes';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { ILogService } from 'vs/platform/log/common/log'; import { ILogService } from 'vs/platform/log/common/log';
import { revive } from 'vs/base/common/marshalling';
interface CommandHandler { interface CommandHandler {
callback: Function; callback: Function;
...@@ -28,18 +29,21 @@ export interface ArgumentProcessor { ...@@ -28,18 +29,21 @@ export interface ArgumentProcessor {
export class ExtHostCommands implements ExtHostCommandsShape { export class ExtHostCommands implements ExtHostCommandsShape {
private _commands = new Map<string, CommandHandler>(); private readonly _commands = new Map<string, CommandHandler>();
private _proxy: MainThreadCommandsShape; private readonly _proxy: MainThreadCommandsShape;
private _converter: CommandsConverter; private readonly _converter: CommandsConverter;
private _argumentProcessors: ArgumentProcessor[] = []; private readonly _logService: ILogService;
private readonly _argumentProcessors: ArgumentProcessor[];
constructor( constructor(
mainContext: IMainContext, mainContext: IMainContext,
heapService: ExtHostHeapService, heapService: ExtHostHeapService,
private logService: ILogService logService: ILogService
) { ) {
this._proxy = mainContext.getProxy(MainContext.MainThreadCommands); this._proxy = mainContext.getProxy(MainContext.MainThreadCommands);
this._converter = new CommandsConverter(this, heapService); this._converter = new CommandsConverter(this, heapService);
this._logService = logService;
this._argumentProcessors = [{ processArgument(a) { return revive(a, 0); } }];
} }
get converter(): CommandsConverter { get converter(): CommandsConverter {
...@@ -51,7 +55,7 @@ export class ExtHostCommands implements ExtHostCommandsShape { ...@@ -51,7 +55,7 @@ export class ExtHostCommands implements ExtHostCommandsShape {
} }
registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable { registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
this.logService.trace('ExtHostCommands#registerCommand', id); this._logService.trace('ExtHostCommands#registerCommand', id);
if (!id.trim().length) { if (!id.trim().length) {
throw new Error('invalid id'); throw new Error('invalid id');
...@@ -72,12 +76,12 @@ export class ExtHostCommands implements ExtHostCommandsShape { ...@@ -72,12 +76,12 @@ export class ExtHostCommands implements ExtHostCommandsShape {
} }
executeCommand<T>(id: string, ...args: any[]): Thenable<T> { executeCommand<T>(id: string, ...args: any[]): Thenable<T> {
this.logService.trace('ExtHostCommands#executeCommand', id); this._logService.trace('ExtHostCommands#executeCommand', id);
if (this._commands.has(id)) { if (this._commands.has(id)) {
// we stay inside the extension host and support // we stay inside the extension host and support
// to pass any kind of parameters around // to pass any kind of parameters around
return this.$executeContributedCommand<T>(id, ...args); return this._executeContributedCommand<T>(id, args);
} else { } else {
// automagically convert some argument types // automagically convert some argument types
...@@ -99,17 +103,10 @@ export class ExtHostCommands implements ExtHostCommandsShape { ...@@ -99,17 +103,10 @@ export class ExtHostCommands implements ExtHostCommandsShape {
return this._proxy.$executeCommand<T>(id, args); return this._proxy.$executeCommand<T>(id, args);
} }
} }
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> { private _executeContributedCommand<T>(id: string, args: any[]): Thenable<T> {
let command = this._commands.get(id); let { callback, thisArg, description } = this._commands.get(id);
if (!command) {
return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));
}
let { callback, thisArg, description } = command;
if (description) { if (description) {
for (let i = 0; i < description.args.length; i++) { for (let i = 0; i < description.args.length; i++) {
try { try {
...@@ -120,24 +117,26 @@ export class ExtHostCommands implements ExtHostCommandsShape { ...@@ -120,24 +117,26 @@ export class ExtHostCommands implements ExtHostCommandsShape {
} }
} }
args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg));
try { try {
let result = callback.apply(thisArg, args); let result = callback.apply(thisArg, args);
return Promise.resolve(result); return Promise.resolve(result);
} catch (err) { } catch (err) {
// console.log(err); this._logService.error(err, id);
// try {
// console.log(toErrorMessage(err));
// } catch (err) {
// //
// }
return Promise.reject(new Error(`Running the contributed command:'${id}' failed.`)); return Promise.reject(new Error(`Running the contributed command:'${id}' failed.`));
} }
} }
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
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<string[]> { getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
this.logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands); this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
return this._proxy.$getCommands().then(result => { return this._proxy.$getCommands().then(result => {
if (filterUnderscoreCommands) { if (filterUnderscoreCommands) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册