forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
182 lines (172 loc) · 7.63 KB
/
proxy.ts
File metadata and controls
182 lines (172 loc) · 7.63 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// tslint:disable:no-any no-empty member-ordering prefer-const prefer-template no-var-self
import { ChildProcess } from 'child_process';
import * as path from 'path';
import { Disposable, Position, Range, TextDocument, TextEditorOptions, Uri, window } from 'vscode';
import '../common/extensions';
import { createDeferred, Deferred } from '../common/helpers';
import { IPythonExecutionFactory } from '../common/process/types';
import { IPythonSettings } from '../common/types';
import { getWindowsLineEndingCount, IS_WINDOWS } from '../common/utils';
import { IServiceContainer } from '../ioc/types';
export class RefactorProxy extends Disposable {
private _process?: ChildProcess;
private _extensionDir: string;
private _previousOutData: string = '';
private _previousStdErrData: string = '';
private _startedSuccessfully: boolean = false;
private _commandResolve?: (value?: any | PromiseLike<any>) => void;
private _commandReject!: (reason?: any) => void;
private initialized!: Deferred<void>;
constructor(extensionDir: string, private pythonSettings: IPythonSettings, private workspaceRoot: string,
private serviceContainer: IServiceContainer) {
super(() => { });
this._extensionDir = extensionDir;
}
public dispose() {
try {
this._process!.kill();
} catch (ex) {
}
this._process = undefined;
}
private getOffsetAt(document: TextDocument, position: Position): number {
if (!IS_WINDOWS) {
return document.offsetAt(position);
}
// get line count
// Rope always uses LF, instead of CRLF on windows, funny isn't it
// So for each line, reduce one characer (for CR)
// But Not all Windows users use CRLF
const offset = document.offsetAt(position);
const winEols = getWindowsLineEndingCount(document, offset);
return offset - winEols;
}
public rename<T>(document: TextDocument, name: string, filePath: string, range: Range, options?: TextEditorOptions): Promise<T> {
if (!options) {
options = window.activeTextEditor!.options;
}
const command = {
lookup: 'rename',
file: filePath,
start: this.getOffsetAt(document, range.start).toString(),
id: '1',
name: name,
indent_size: options.tabSize
};
return this.sendCommand<T>(JSON.stringify(command));
}
public extractVariable<T>(document: TextDocument, name: string, filePath: string, range: Range, options?: TextEditorOptions): Promise<T> {
if (!options) {
options = window.activeTextEditor!.options;
}
const command = {
lookup: 'extract_variable',
file: filePath,
start: this.getOffsetAt(document, range.start).toString(),
end: this.getOffsetAt(document, range.end).toString(),
id: '1',
name: name,
indent_size: options.tabSize
};
return this.sendCommand<T>(JSON.stringify(command));
}
public extractMethod<T>(document: TextDocument, name: string, filePath: string, range: Range, options?: TextEditorOptions): Promise<T> {
if (!options) {
options = window.activeTextEditor!.options;
}
// Ensure last line is an empty line
if (!document.lineAt(document.lineCount - 1).isEmptyOrWhitespace && range.start.line === document.lineCount - 1) {
return Promise.reject<T>('Missing blank line at the end of document (PEP8).');
}
const command = {
lookup: 'extract_method',
file: filePath,
start: this.getOffsetAt(document, range.start).toString(),
end: this.getOffsetAt(document, range.end).toString(),
id: '1',
name: name,
indent_size: options.tabSize
};
return this.sendCommand<T>(JSON.stringify(command));
}
private sendCommand<T>(command: string, telemetryEvent?: string): Promise<T> {
return this.initialize(this.pythonSettings.pythonPath).then(() => {
// tslint:disable-next-line:promise-must-complete
return new Promise<T>((resolve, reject) => {
this._commandResolve = resolve;
this._commandReject = reject;
this._process!.stdin.write(command + '\n');
});
});
}
private async initialize(pythonPath: string): Promise<void> {
const pythonProc = await this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory).create({ resource: Uri.file(this.workspaceRoot) });
this.initialized = createDeferred<void>();
const args = ['refactor.py', this.workspaceRoot];
const cwd = path.join(this._extensionDir, 'pythonFiles');
const result = pythonProc.execObservable(args, { cwd });
this._process = result.proc;
result.out.subscribe(output => {
if (output.source === 'stdout') {
if (!this._startedSuccessfully && output.out.startsWith('STARTED')) {
this._startedSuccessfully = true;
return this.initialized.resolve();
}
this.onData(output.out);
} else {
this.handleStdError(output.out);
}
}, error => this.handleError(error));
return this.initialized.promise;
}
private handleStdError(data: string) {
// Possible there was an exception in parsing the data returned
// So append the data then parse it
let dataStr = this._previousStdErrData = this._previousStdErrData + data + '';
let errorResponse: { message: string; traceback: string; type: string }[];
try {
errorResponse = dataStr.split(/\r?\n/g).filter(line => line.length > 0).map(resp => JSON.parse(resp));
this._previousStdErrData = '';
} catch (ex) {
console.error(ex);
// Possible we've only received part of the data, hence don't clear previousData
return;
}
if (typeof errorResponse[0].message !== 'string' || errorResponse[0].message.length === 0) {
errorResponse[0].message = errorResponse[0].traceback.splitLines().pop()!;
}
let errorMessage = errorResponse[0].message + '\n' + errorResponse[0].traceback;
if (this._startedSuccessfully) {
this._commandReject(`Refactor failed. ${errorMessage}`);
} else {
if (typeof errorResponse[0].type === 'string' && errorResponse[0].type === 'ModuleNotFoundError') {
this.initialized.reject('Not installed');
return;
}
this.initialized.reject(`Refactor failed. ${errorMessage}`);
}
}
private handleError(error: Error) {
if (this._startedSuccessfully) {
return this._commandReject(error);
}
this.initialized.reject(error);
}
private onData(data: string) {
if (!this._commandResolve) { return; }
// Possible there was an exception in parsing the data returned
// So append the data then parse it
let dataStr = this._previousOutData = this._previousOutData + data + '';
let response: any;
try {
response = dataStr.split(/\r?\n/g).filter(line => line.length > 0).map(resp => JSON.parse(resp));
this._previousOutData = '';
} catch (ex) {
// Possible we've only received part of the data, hence don't clear previousData
return;
}
this.dispose();
this._commandResolve!(response[0]);
this._commandResolve = undefined;
}
}