@@ -15,6 +15,7 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays';
1515import * as modes from 'vs/editor/common/modes' ;
1616import * as vscode from 'vscode' ;
1717import { ILogService } from 'vs/platform/log/common/log' ;
18+ import { revive } from 'vs/base/common/marshalling' ;
1819
1920interface CommandHandler {
2021 callback : Function ;
@@ -28,18 +29,21 @@ export interface ArgumentProcessor {
2829
2930export 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