@@ -12,7 +12,11 @@ import { ExtHostEditors } from 'vs/workbench/api/node/extHostEditors';
1212import * as extHostTypes from 'vs/workbench/api/node/extHostTypes' ;
1313import * as extHostTypeConverter from 'vs/workbench/api/node/extHostTypeConverters' ;
1414import { cloneAndChange } from 'vs/base/common/objects' ;
15- import { MainContext , MainThreadCommandsShape , ExtHostCommandsShape } from './extHost.protocol' ;
15+ import { MainContext , MainThreadCommandsShape , ExtHostCommandsShape , ObjectIdentifier } from './extHost.protocol' ;
16+ import { ExtHostHeapService } from 'vs/workbench/api/node/extHostHeapService' ;
17+ import { isFalsyOrEmpty } from 'vs/base/common/arrays' ;
18+ import * as modes from 'vs/editor/common/modes' ;
19+ import * as vscode from 'vscode' ;
1620
1721interface CommandHandler {
1822 callback : Function ;
@@ -25,14 +29,21 @@ export class ExtHostCommands extends ExtHostCommandsShape {
2529 private _commands : { [ n : string ] : CommandHandler } = Object . create ( null ) ;
2630 private _proxy : MainThreadCommandsShape ;
2731 private _extHostEditors : ExtHostEditors ;
32+ private _converter : CommandsConverter ;
2833
2934 constructor (
3035 threadService : IThreadService ,
31- extHostEditors : ExtHostEditors
36+ extHostEditors : ExtHostEditors ,
37+ heapService : ExtHostHeapService
3238 ) {
3339 super ( ) ;
3440 this . _extHostEditors = extHostEditors ;
3541 this . _proxy = threadService . get ( MainContext . MainThreadCommands ) ;
42+ this . _converter = new CommandsConverter ( this , heapService ) ;
43+ }
44+
45+ get converter ( ) : CommandsConverter {
46+ return this . _converter ;
3647 }
3748
3849 registerCommand ( id : string , callback : < T > ( ...args : any [ ] ) => T | Thenable < T > , thisArg ?: any , description ?: ICommandHandlerDescription ) : extHostTypes . Disposable {
@@ -137,3 +148,68 @@ export class ExtHostCommands extends ExtHostCommandsShape {
137148 return TPromise . as ( result ) ;
138149 }
139150}
151+
152+
153+ export class CommandsConverter {
154+
155+ private _commands : ExtHostCommands ;
156+ private _heap : ExtHostHeapService ;
157+
158+ // --- conversion between internal and api commands
159+ constructor ( commands : ExtHostCommands , heap : ExtHostHeapService ) {
160+
161+ this . _commands = commands ;
162+ this . _heap = heap ;
163+ this . _commands . registerCommand ( '_internal_command_delegation' , this . _executeConvertedCommand , this ) ;
164+ }
165+
166+ toInternal ( command : vscode . Command ) : modes . Command {
167+
168+ if ( ! command ) {
169+ return ;
170+ }
171+
172+ const result : modes . Command = {
173+ id : command . command ,
174+ title : command . title
175+ } ;
176+
177+ if ( ! isFalsyOrEmpty ( command . arguments ) ) {
178+ // we have a contributed command with arguments. that
179+ // means we don't want to send the arguments around
180+
181+ const id = this . _heap . keep ( command ) ;
182+ ObjectIdentifier . mixin ( result , id ) ;
183+
184+ result . id = '_internal_command_delegation' ;
185+ result . arguments = [ id ] ;
186+ }
187+
188+ return result ;
189+ }
190+
191+ fromInternal ( command : modes . Command ) : vscode . Command {
192+
193+ if ( ! command ) {
194+ return ;
195+ }
196+
197+ const id = ObjectIdentifier . of ( command ) ;
198+ if ( typeof id === 'number' ) {
199+ return this . _heap . get < vscode . Command > ( id ) ;
200+
201+ } else {
202+ return {
203+ command : command . id ,
204+ title : command . title ,
205+ arguments : command . arguments
206+ } ;
207+ }
208+ }
209+
210+ private _executeConvertedCommand ( [ id ] : number [ ] ) {
211+ const actualCmd = this . _heap . get < vscode . Command > ( id ) ;
212+ return this . _commands . executeCommand ( actualCmd . command , ...actualCmd . arguments ) ;
213+ }
214+
215+ }
0 commit comments