@@ -6,50 +6,44 @@ import * as child_process from 'child_process';
66import * as path from 'path' ;
77import * as vscode from 'vscode' ;
88import { createDeferred , Deferred } from '../common/helpers' ;
9- import { IClientAdapter } from './IClientAdapter' ;
109import { PythonSettings } from '../common/configSettings' ;
1110import { EventEmitter } from 'events' ;
1211import { formatErrorForLogging } from '../common/utils' ;
13- import { TextDocument , Position , CancellationToken } from 'vscode' ;
14- import { CompletionItem , Definition , Hover , ReferenceContext , Location , SignatureHelp , SymbolInformation } from 'vscode' ;
15- import { Commands } from "./commands" ;
16- import { CompletionParser } from './parsers/CompletionParser' ;
12+ import { CancellationToken } from 'vscode' ;
13+ import { CompletionItem , Definition , Hover , Location , SignatureHelp , SymbolInformation } from 'vscode' ;
14+ import { RequestCommands } from "./commands" ;
1715import { DefinitionParser } from './parsers/DefinitionParser' ;
1816import { HoverParser } from './parsers/HoverParser' ;
1917import { LocationParser } from './parsers/LocationParser' ;
2018import { SignatureHelpParser } from './parsers/SignatureHelpParser' ;
2119import { SymbolInformationParser } from './parsers/SymbolInformationParser' ;
2220
23- export class ClientAdapter extends EventEmitter implements IClientAdapter {
21+ export enum Command {
22+ Completions ,
23+ Definition ,
24+ Hover ,
25+ References ,
26+ Signature ,
27+ DocumentSymbols
28+ }
29+
30+ const commandMapping = new Map < Command , Buffer > ( ) ;
31+ commandMapping . set ( Command . Completions , RequestCommands . Completions ) ;
32+ commandMapping . set ( Command . Definition , RequestCommands . Definitions ) ;
33+ commandMapping . set ( Command . Hover , RequestCommands . Hover ) ;
34+ commandMapping . set ( Command . References , RequestCommands . Usages ) ;
35+ commandMapping . set ( Command . Signature , RequestCommands . Arguments ) ;
36+ commandMapping . set ( Command . DocumentSymbols , RequestCommands . Names ) ;
37+
38+ export class ClientAdapter extends EventEmitter {
2439 constructor ( private outputChannel : vscode . OutputChannel , private rootDir : string ) {
2540 super ( ) ;
2641 }
27-
28- public getCompletions ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < CompletionItem [ ] > {
29- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
30- . then ( CompletionParser . parse ) ;
31- }
32- public getDefinition ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < Definition > {
33- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
34- . then ( DefinitionParser . parse ) ;
35- }
36- public getHoverDefinition ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < Hover > {
37- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
38- . then ( HoverParser . parse ) ;
39- }
40- public getReferences ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < Location [ ] > {
41- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
42- . then ( DefinitionParser . parse ) ;
42+ public getResult < T > ( responseParser : ( data : Object ) => T , command : Command , token : CancellationToken , fileName : string , columnIndex ?: number , lineIndex ?: number , source ?: string ) : Promise < T > {
43+ const cmd = commandMapping . get ( command ) ;
44+ return this . socketClient . getResult < any > ( cmd , token , fileName , columnIndex , lineIndex , source )
45+ . then ( responseParser ) ;
4346 }
44- public getSignature ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < SignatureHelp > {
45- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
46- . then ( SignatureHelpParser . parse ) ;
47- }
48- public getDocumentSymbols ( token : CancellationToken , fileName : string , columnIndex : number , lineIndex : number , source : string ) : Promise < SymbolInformation [ ] > {
49- return this . socketClient . getResult < any > ( Commands . Completions , token , fileName , columnIndex , lineIndex , source )
50- . then ( SymbolInformationParser . parse ) ;
51- }
52-
5347 private process : child_process . ChildProcess ;
5448 private socketServer : SocketServer ;
5549 private socketClient : SocketClient ;
@@ -87,7 +81,6 @@ export class ClientAdapter extends EventEmitter implements IClientAdapter {
8781 this . startDef = createDeferred < any > ( ) ;
8882 const pyFile = path . join ( __dirname , '..' , '..' , '..' , '..' , 'pythonFiles' , 'completionServer.py' ) ;
8983 const newEnv = { } ;
90- // const newEnv = {'DEBUG_DJAYAMANNE_IPYTHON':'1'};
9184 Object . assign ( newEnv , envVariables ) ;
9285 Object . assign ( newEnv , process . env ) ;
9386
@@ -100,15 +93,12 @@ export class ClientAdapter extends EventEmitter implements IClientAdapter {
10093
10194 let processStarted = false ;
10295 let handshakeDone = false ;
103- let isInTestRun = newEnv [ 'PYTHON_DONJAYAMANNE_TEST' ] === "1" ;
104- const testDef = createDeferred < any > ( ) ;
105- const promiseToResolve = isInTestRun ? testDef . resolve . bind ( testDef ) : def . resolve . bind ( def ) ;
10696
10797 this . process . stdout . on ( 'data' , ( data : string ) => {
10898 if ( ! processStarted && data . split ( / \r ? \n / g) . some ( line => line === 'Started' ) ) {
10999 processStarted = true ;
110100 if ( processStarted && handshakeDone ) {
111- promiseToResolve ( ) ;
101+ def . resolve ( ) ;
112102 }
113103 return ;
114104 }
@@ -121,28 +111,10 @@ export class ClientAdapter extends EventEmitter implements IClientAdapter {
121111 this . socketClient . on ( 'handshake' , ( ) => {
122112 handshakeDone = true ;
123113 if ( processStarted && handshakeDone ) {
124- promiseToResolve ( ) ;
114+ def . resolve ( ) ;
125115 }
126116 } ) ;
127117
128- // If we're testing, then test the ping and the pong
129- // TODO: Better way to test sockets
130- if ( isInTestRun ) {
131- testDef . promise . then ( ( ) => {
132- // Ok everything has started, now test ping
133- const msg1 = 'Hello world from Type Script - Функция проверки ИНН и КПП - 长城!1' ;
134- const msg2 = 'Hello world from Type Script - Функция проверки ИНН и КПП - 长城!2' ;
135- Promise . all < string , string > ( [ this . socketClient . ping ( msg1 ) , this . socketClient . ping ( msg2 ) ] ) . then ( msgs => {
136- if ( msgs . indexOf ( msg1 ) === - 1 || msgs . indexOf ( msg2 ) === - 1 ) {
137- def . reject ( 'msg1 or msg2 not returned' ) ;
138- }
139- else {
140- def . resolve ( ) ;
141- }
142- } ) . catch ( reason => def . reject ( reason ) ) ;
143- } ) ;
144- }
145-
146118 return def . promise ;
147119 } ) . then ( ( ) => {
148120 this . startDef . resolve ( ) ;
@@ -164,52 +136,8 @@ export class ClientAdapter extends EventEmitter implements IClientAdapter {
164136 this . outputChannel . appendLine ( 'Error received: ' + error ) ;
165137 } ) ;
166138 this . socketClient . on ( 'commanderror' , ( commandError : { command : string , id : string , trace : string } ) => {
167- this . outputChannel . appendLine ( `Unhandled command Error from Jupyter . '${ JSON . stringify ( commandError ) } '` ) ;
139+ this . outputChannel . appendLine ( `Unhandled command Error from Autocompletion Library . '${ JSON . stringify ( commandError ) } '` ) ;
168140 } ) ;
169141 return this . socketServer . Start ( ) ;
170142 }
171-
172- public getAllKernelSpecs ( ) : Promise < { [ key : string ] : Kernelspec } > {
173- return this . start ( ) . then ( ( ) => this . socketClient . listKernelSpecs ( ) ) ;
174- }
175- private lastStartedKernelUUID : string ;
176- public startKernel ( kernelSpec : KernelspecMetadata ) : Promise < [ string , any , string ] > {
177- return this . start ( ) . then ( ( ) => this . getAllKernelSpecs ( ) ) . then ( specks => {
178- // ok given the specks, find the name of the kernelspec
179- const kernelSpecName = Object . keys ( specks ) . find ( kernelSpecName => {
180- const spec = specks [ kernelSpecName ] ;
181- return spec . spec . display_name === kernelSpec . display_name ;
182- } ) ;
183- return this . socketClient . startKernel ( kernelSpecName ) . then ( info => {
184- this . lastStartedKernelUUID = info [ 0 ] ;
185- return info ;
186- } ) ;
187- } ) ;
188- }
189- public shutdownkernel ( kernelUUID : string ) : Promise < any > {
190- return this . start ( ) . then ( ( ) => this . socketClient . sendKernelCommand ( kernelUUID , KernelCommand . shutdown ) ) ;
191- }
192- public interruptKernel ( kernelUUID : string ) : Promise < any > {
193- return this . start ( ) . then ( ( ) => this . socketClient . sendKernelCommand ( kernelUUID , KernelCommand . interrupt ) ) ;
194- }
195- public restartKernel ( kernelUUID : string ) : Promise < any > {
196- return this . start ( ) . then ( ( ) => this . socketClient . sendKernelCommand ( kernelUUID , KernelCommand . restart ) ) ;
197- }
198- public runCode ( code : string ) : Rx . IObservable < ParsedIOMessage > {
199- const subject = new Rx . Subject < ParsedIOMessage > ( ) ;
200- this . start ( ) . then ( ( ) => {
201- const runnerObservable = this . socketClient . runCode ( code ) ;
202- runnerObservable . subscribe ( data => {
203- subject . onNext ( data ) ;
204- } , reason => {
205- subject . onError ( reason ) ;
206- } , ( ) => {
207- subject . onCompleted ( ) ;
208- } ) ;
209- } ) . catch ( reason => {
210- subject . onError ( reason ) ;
211- } ) ;
212-
213- return subject ;
214- }
215143}
0 commit comments