forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
137 lines (124 loc) · 4.94 KB
/
Copy pathmain.ts
File metadata and controls
137 lines (124 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
import * as child_process from 'child_process';
import * as path from 'path';
import * as vscode from 'vscode';
import { SocketClient } from './socketClient';
import { SocketServer } from '../common/comms/socketServer';
import { createDeferred, Deferred } from '../common/helpers';
import { PythonSettings } from '../common/configSettings';
import { EventEmitter } from 'events';
import { CancellationToken } from 'vscode';
import { RequestCommands } from "./commands";
export enum Command {
Completions,
Definition,
Hover,
References,
Signature,
DocumentSymbols
}
const commandMapping = new Map<Command, Buffer>();
commandMapping.set(Command.Completions, RequestCommands.Completions);
commandMapping.set(Command.Definition, RequestCommands.Definitions);
commandMapping.set(Command.Hover, RequestCommands.Hover);
commandMapping.set(Command.References, RequestCommands.Usages);
commandMapping.set(Command.Signature, RequestCommands.Arguments);
commandMapping.set(Command.DocumentSymbols, RequestCommands.Names);
export class ClientAdapter extends EventEmitter {
constructor(private outputChannel: vscode.OutputChannel, private rootDir: string) {
super();
}
public getResult<T>(responseParser: (data: Object) => T, command: Command, token: CancellationToken, fileName: string, columnIndex?: number, lineIndex?: number, source?: string): Promise<T> {
const cmd = commandMapping.get(command);
return this.socketClient.getResult<any>(cmd, token, fileName, columnIndex, lineIndex, source)
.then(responseParser);
}
private process: child_process.ChildProcess;
private socketServer: SocketServer;
private socketClient: SocketClient;
private startDef: Deferred<any>;
public dispose() {
try {
if (this.process) {
this.process.stdin.write('\n');
}
}
catch (ex) {
}
try {
this.socketClient.dispose();
}
catch (ex) {
}
try {
this.socketServer.Stop();
}
catch (ex) {
}
this.socketClient = null;
this.process = null;
this.socketServer = null;
this.startDef = null;
}
public start(envVariables?: { [key: string]: string }): Promise<any> {
if (this.startDef) {
return this.startDef.promise;
}
this.startDef = createDeferred<any>();
const pyFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'completionServer.py');
const newEnv = {};
Object.assign(newEnv, envVariables);
Object.assign(newEnv, process.env);
this.startSocketServer().then(port => {
const def = createDeferred<any>();
const options = { env: newEnv, cwd: this.rootDir };
const rootDirUri = this.rootDir ? vscode.Uri.file(this.rootDir) : undefined;
this.process = child_process.spawn(PythonSettings.getInstance(rootDirUri).pythonPath, [pyFile, port.toString()], options);
this.process.stdout.setEncoding('utf8');
this.process.stderr.setEncoding('utf8');
let processStarted = false;
let handshakeDone = false;
this.process.stdout.on('data', (data: string) => {
if (!processStarted && data.split(/\r?\n/g).some(line => line === 'Started')) {
processStarted = true;
if (processStarted && handshakeDone) {
def.resolve();
}
return;
}
this.outputChannel.append(data);
});
this.process.stderr.on('data', (data: string) => {
this.outputChannel.append(data);
});
this.socketClient.on('handshake', () => {
handshakeDone = true;
if (processStarted && handshakeDone) {
def.resolve();
}
});
return def.promise;
}).then(() => {
this.startDef.resolve();
}).catch(reason => {
this.startDef.reject(reason);
});
return this.startDef.promise;
}
private startSocketServer(): Promise<number> {
this.socketServer = new SocketServer();
this.socketClient = new SocketClient(this.socketServer, this.outputChannel);
this.socketClient.on('status', status => {
this.emit('status', status);
});
this.socketClient.on('error', error => {
this.emit('error', error);
console.error(error);
this.outputChannel.appendLine('Error received: ' + error);
});
this.socketClient.on('commanderror', (commandError: { command: string, id: string, trace: string }) => {
this.outputChannel.appendLine(`Unhandled command Error from Autocompletion Library. '${JSON.stringify(commandError)}'`);
});
return this.socketServer.Start();
}
}