ExtHostCommands, MainThreadCommands, #40169

This commit is contained in:
Johannes Rieken
2017-12-14 15:46:03 +01:00
parent 785aac7d10
commit 8f18e87477
3 changed files with 31 additions and 28 deletions

View File

@@ -731,7 +731,7 @@ export interface ExtHostWindowShape {
// --- proxy identifiers
export const MainContext = {
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands', ProxyType.CustomMarshaller),
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands'),
MainThreadConfiguration: createMainId<MainThreadConfigurationShape>('MainThreadConfiguration', ProxyType.CustomMarshaller),
MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService', ProxyType.CustomMarshaller),
MainThreadDecorations: createMainId<MainThreadDecorationsShape>('MainThreadDecorations'),
@@ -761,7 +761,7 @@ export const MainContext = {
};
export const ExtHostContext = {
ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands', ProxyType.CustomMarshaller),
ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands'),
ExtHostConfiguration: createExtId<ExtHostConfigurationShape>('ExtHostConfiguration', ProxyType.CustomMarshaller),
ExtHostDiagnostics: createExtId<ExtHostDiagnosticsShape>('ExtHostDiagnostics'),
ExtHostDebugService: createExtId<ExtHostDebugServiceShape>('ExtHostDebugService', ProxyType.CustomMarshaller),

View File

@@ -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<string, CommandHandler>();
private _proxy: MainThreadCommandsShape;
private _converter: CommandsConverter;
private _argumentProcessors: ArgumentProcessor[] = [];
private readonly _commands = new Map<string, CommandHandler>();
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: <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) {
throw new Error('invalid id');
@@ -72,12 +76,12 @@ export class ExtHostCommands implements ExtHostCommandsShape {
}
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)) {
// we stay inside the extension host and support
// to pass any kind of parameters around
return this.$executeContributedCommand<T>(id, ...args);
return this._executeContributedCommand<T>(id, args);
} else {
// automagically convert some argument types
@@ -99,17 +103,10 @@ export class ExtHostCommands implements ExtHostCommandsShape {
return this._proxy.$executeCommand<T>(id, args);
}
}
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
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<T>(id: string, args: any[]): Thenable<T> {
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<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[]> {
this.logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
return this._proxy.$getCommands().then(result => {
if (filterUnderscoreCommands) {