Skip to content

Commit dc31d1c

Browse files
committed
streamlined
1 parent 20a7287 commit dc31d1c

2 files changed

Lines changed: 42 additions & 120 deletions

File tree

src/client/jedi/main.ts

Lines changed: 28 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,44 @@ import * as child_process from 'child_process';
66
import * as path from 'path';
77
import * as vscode from 'vscode';
88
import { createDeferred, Deferred } from '../common/helpers';
9-
import { IClientAdapter } from './IClientAdapter';
109
import { PythonSettings } from '../common/configSettings';
1110
import { EventEmitter } from 'events';
1211
import { 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";
1715
import { DefinitionParser } from './parsers/DefinitionParser';
1816
import { HoverParser } from './parsers/HoverParser';
1917
import { LocationParser } from './parsers/LocationParser';
2018
import { SignatureHelpParser } from './parsers/SignatureHelpParser';
2119
import { 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
}

src/client/jedi/socketClient.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
"use strict";
22

33
import { SocketCallbackHandler } from "../common/comms/socketCallbackHandler";
4-
import { Commands, ResponseCommands } from "./commands";
4+
import { RequestCommands, ResponseCommands } from "./commands";
55
import { SocketServer } from '../common/comms/socketServer';
66
import { IdDispenser } from '../common/idDispenser';
77
import { createDeferred, Deferred } from '../common/helpers';
88
import { OutputChannel, CancellationToken } from 'vscode';
99

10-
/*
11-
public static Exit: Buffer = new Buffer("exit");
12-
public static Ping: Buffer = new Buffer("ping");
13-
public static Arguments = new Buffer("args");
14-
public static Completions = new Buffer("comp");
15-
public static Definitions = new Buffer("defs");
16-
public static Hover = new Buffer("hovr");
17-
public static Usages = new Buffer("usag");
18-
public static Names = new Buffer("name");
19-
*/
20-
21-
2210
export class SocketClient extends SocketCallbackHandler {
2311
constructor(socketServer: SocketServer, private outputChannel: OutputChannel) {
2412
super(socketServer);
2513
this.registerCommandHandler(ResponseCommands.Pong, this.onPong.bind(this));
2614
this.registerCommandHandler(ResponseCommands.Error, this.onError.bind(this));
15+
this.registerCommandHandler(ResponseCommands.TraceLog, this.onWriteToLog.bind(this));
2716

28-
this.registerCommandHandler(ResponseCommands.Arguments, this.onResponseReceived.bind(this));
17+
this.registerCommandHandler(ResponseCommands.Signature, this.onResponseReceived.bind(this));
2918
this.registerCommandHandler(ResponseCommands.Completions, this.onResponseReceived.bind(this));
3019
this.registerCommandHandler(ResponseCommands.Definitions, this.onResponseReceived.bind(this));
3120
this.registerCommandHandler(ResponseCommands.Hover, this.onResponseReceived.bind(this));
32-
this.registerCommandHandler(ResponseCommands.Names, this.onResponseReceived.bind(this));
33-
this.registerCommandHandler(ResponseCommands.Usages, this.onResponseReceived.bind(this));
21+
this.registerCommandHandler(ResponseCommands.DocumentSymbols, this.onResponseReceived.bind(this));
22+
this.registerCommandHandler(ResponseCommands.References, this.onResponseReceived.bind(this));
3423

3524
this.idDispenser = new IdDispenser();
3625
}
@@ -49,9 +38,6 @@ export class SocketClient extends SocketCallbackHandler {
4938
}
5039
private idDispenser: IdDispenser;
5140
private pid: number;
52-
private writeToDebugLog(message: string) {
53-
this.outputChannel.appendLine(message);
54-
}
5541
public dispose() {
5642
super.dispose();
5743
}
@@ -108,9 +94,17 @@ export class SocketClient extends SocketCallbackHandler {
10894

10995
def.resolve(jsonResponse);
11096
}
97+
private onWriteToLog() {
98+
const message = this.stream.readStringInTransaction();
99+
if (typeof message !== 'string') {
100+
return;
101+
}
102+
this.outputChannel.appendLine(message);
103+
}
104+
111105
public ping(message: string) {
112106
const [def, id] = this.createId<string>(null);
113-
this.SendRawCommand(Commands.Ping);
107+
this.SendRawCommand(RequestCommands.Ping);
114108
this.stream.WriteString(id);
115109
this.stream.WriteString(message);
116110
return def.promise;

0 commit comments

Comments
 (0)