Skip to content

Commit 8f18e87

Browse files
committed
ExtHostCommands, MainThreadCommands, microsoft#40169
1 parent 785aac7 commit 8f18e87

3 files changed

Lines changed: 31 additions & 28 deletions

File tree

src/vs/workbench/api/electron-browser/mainThreadCommands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IDisposable } from 'vs/base/common/lifecycle';
99
import { TPromise } from 'vs/base/common/winjs.base';
1010
import { ExtHostContext, MainThreadCommandsShape, ExtHostCommandsShape, MainContext, IExtHostContext } from '../node/extHost.protocol';
1111
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
12+
import { revive } from 'vs/base/common/marshalling';
1213

1314
@extHostNamedCustomer(MainContext.MainThreadCommands)
1415
export class MainThreadCommands implements MainThreadCommandsShape {
@@ -70,6 +71,9 @@ export class MainThreadCommands implements MainThreadCommandsShape {
7071
}
7172

7273
$executeCommand<T>(id: string, args: any[]): Thenable<T> {
74+
for (let i = 0; i < args.length; i++) {
75+
args[i] = revive(args[i], 0);
76+
}
7377
return this._commandService.executeCommand<T>(id, ...args);
7478
}
7579

src/vs/workbench/api/node/extHost.protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ export interface ExtHostWindowShape {
731731
// --- proxy identifiers
732732

733733
export const MainContext = {
734-
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands', ProxyType.CustomMarshaller),
734+
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands'),
735735
MainThreadConfiguration: createMainId<MainThreadConfigurationShape>('MainThreadConfiguration', ProxyType.CustomMarshaller),
736736
MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService', ProxyType.CustomMarshaller),
737737
MainThreadDecorations: createMainId<MainThreadDecorationsShape>('MainThreadDecorations'),
@@ -761,7 +761,7 @@ export const MainContext = {
761761
};
762762

763763
export const ExtHostContext = {
764-
ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands', ProxyType.CustomMarshaller),
764+
ExtHostCommands: createExtId<ExtHostCommandsShape>('ExtHostCommands'),
765765
ExtHostConfiguration: createExtId<ExtHostConfigurationShape>('ExtHostConfiguration', ProxyType.CustomMarshaller),
766766
ExtHostDiagnostics: createExtId<ExtHostDiagnosticsShape>('ExtHostDiagnostics'),
767767
ExtHostDebugService: createExtId<ExtHostDebugServiceShape>('ExtHostDebugService', ProxyType.CustomMarshaller),

src/vs/workbench/api/node/extHostCommands.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays';
1515
import * as modes from 'vs/editor/common/modes';
1616
import * as vscode from 'vscode';
1717
import { ILogService } from 'vs/platform/log/common/log';
18+
import { revive } from 'vs/base/common/marshalling';
1819

1920
interface CommandHandler {
2021
callback: Function;
@@ -28,18 +29,21 @@ export interface ArgumentProcessor {
2829

2930
export class ExtHostCommands implements ExtHostCommandsShape {
3031

31-
private _commands = new Map<string, CommandHandler>();
32-
private _proxy: MainThreadCommandsShape;
33-
private _converter: CommandsConverter;
34-
private _argumentProcessors: ArgumentProcessor[] = [];
32+
private readonly _commands = new Map<string, CommandHandler>();
33+
private readonly _proxy: MainThreadCommandsShape;
34+
private readonly _converter: CommandsConverter;
35+
private readonly _logService: ILogService;
36+
private readonly _argumentProcessors: ArgumentProcessor[];
3537

3638
constructor(
3739
mainContext: IMainContext,
3840
heapService: ExtHostHeapService,
39-
private logService: ILogService
41+
logService: ILogService
4042
) {
4143
this._proxy = mainContext.getProxy(MainContext.MainThreadCommands);
4244
this._converter = new CommandsConverter(this, heapService);
45+
this._logService = logService;
46+
this._argumentProcessors = [{ processArgument(a) { return revive(a, 0); } }];
4347
}
4448

4549
get converter(): CommandsConverter {
@@ -51,7 +55,7 @@ export class ExtHostCommands implements ExtHostCommandsShape {
5155
}
5256

5357
registerCommand(id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, description?: ICommandHandlerDescription): extHostTypes.Disposable {
54-
this.logService.trace('ExtHostCommands#registerCommand', id);
58+
this._logService.trace('ExtHostCommands#registerCommand', id);
5559

5660
if (!id.trim().length) {
5761
throw new Error('invalid id');
@@ -72,12 +76,12 @@ export class ExtHostCommands implements ExtHostCommandsShape {
7276
}
7377

7478
executeCommand<T>(id: string, ...args: any[]): Thenable<T> {
75-
this.logService.trace('ExtHostCommands#executeCommand', id);
79+
this._logService.trace('ExtHostCommands#executeCommand', id);
7680

7781
if (this._commands.has(id)) {
7882
// we stay inside the extension host and support
7983
// to pass any kind of parameters around
80-
return this.$executeContributedCommand<T>(id, ...args);
84+
return this._executeContributedCommand<T>(id, args);
8185

8286
} else {
8387
// automagically convert some argument types
@@ -99,17 +103,10 @@ export class ExtHostCommands implements ExtHostCommandsShape {
99103

100104
return this._proxy.$executeCommand<T>(id, args);
101105
}
102-
103106
}
104107

105-
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
106-
let command = this._commands.get(id);
107-
if (!command) {
108-
return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));
109-
}
110-
111-
let { callback, thisArg, description } = command;
112-
108+
private _executeContributedCommand<T>(id: string, args: any[]): Thenable<T> {
109+
let { callback, thisArg, description } = this._commands.get(id);
113110
if (description) {
114111
for (let i = 0; i < description.args.length; i++) {
115112
try {
@@ -120,24 +117,26 @@ export class ExtHostCommands implements ExtHostCommandsShape {
120117
}
121118
}
122119

123-
args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg));
124-
125120
try {
126121
let result = callback.apply(thisArg, args);
127122
return Promise.resolve(result);
128123
} catch (err) {
129-
// console.log(err);
130-
// try {
131-
// console.log(toErrorMessage(err));
132-
// } catch (err) {
133-
// //
134-
// }
124+
this._logService.error(err, id);
135125
return Promise.reject(new Error(`Running the contributed command:'${id}' failed.`));
136126
}
137127
}
138128

129+
$executeContributedCommand<T>(id: string, ...args: any[]): Thenable<T> {
130+
if (!this._commands.has(id)) {
131+
return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));
132+
} else {
133+
args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r), arg));
134+
return this._executeContributedCommand(id, args);
135+
}
136+
}
137+
139138
getCommands(filterUnderscoreCommands: boolean = false): Thenable<string[]> {
140-
this.logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
139+
this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
141140

142141
return this._proxy.$getCommands().then(result => {
143142
if (filterUnderscoreCommands) {

0 commit comments

Comments
 (0)