forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigSettings.ts
More file actions
366 lines (342 loc) · 13.8 KB
/
Copy pathconfigSettings.ts
File metadata and controls
366 lines (342 loc) · 13.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
'use strict';
import * as vscode from 'vscode';
import { SystemVariables } from './systemVariables';
import { EventEmitter } from 'events';
import * as path from 'path';
import * as child_process from 'child_process';
export const IS_WINDOWS = /^win/.test(process.platform);
export interface IPythonSettings {
pythonPath: string;
jediPath: string;
devOptions: string[];
linting: ILintingSettings;
formatting: IFormattingSettings;
unitTest: IUnitTestSettings;
autoComplete: IAutoCompeteSettings;
terminal: ITerminalSettings;
jupyter: JupyterSettings;
sortImports: ISortImportSettings;
workspaceSymbols: IWorkspaceSymbolSettings;
}
export interface ISortImportSettings {
args: string[];
}
export interface IUnitTestSettings {
promptToConfigure: boolean;
debugPort: number;
nosetestsEnabled: boolean;
nosetestPath: string;
nosetestArgs: string[];
pyTestEnabled: boolean;
pyTestPath: string;
pyTestArgs: string[];
unittestEnabled: boolean;
unittestArgs: string[];
outputWindow: string;
}
export interface IPylintCategorySeverity {
convention: vscode.DiagnosticSeverity;
refactor: vscode.DiagnosticSeverity;
warning: vscode.DiagnosticSeverity;
error: vscode.DiagnosticSeverity;
fatal: vscode.DiagnosticSeverity;
}
export interface ILintingSettings {
enabled: boolean;
ignorePatterns: string[];
prospectorEnabled: boolean;
prospectorArgs: string[];
pylintEnabled: boolean;
pylintArgs: string[];
pep8Enabled: boolean;
pep8Args: string[];
pylamaEnabled: boolean;
pylamaArgs: string[];
flake8Enabled: boolean;
flake8Args: string[];
pydocstyleEnabled: boolean;
pydocstyleArgs: string[];
lintOnTextChange: boolean;
lintOnSave: boolean;
maxNumberOfProblems: number;
pylintCategorySeverity: IPylintCategorySeverity;
prospectorPath: string;
pylintPath: string;
pep8Path: string;
pylamaPath: string;
flake8Path: string;
pydocstylePath: string;
outputWindow: string;
mypyEnabled: boolean;
mypyArgs: string[];
mypyPath: string;
}
export interface IFormattingSettings {
provider: string;
autopep8Path: string;
autopep8Args: string[];
yapfPath: string;
yapfArgs: string[];
formatOnSave: boolean;
outputWindow: string;
}
export interface IAutoCompeteSettings {
addBrackets: boolean;
extraPaths: string[];
}
export interface IWorkspaceSymbolSettings {
enabled: boolean;
tagFilePath: string;
rebuildOnStart: boolean;
rebuildOnFileSave: boolean;
ctagsPath: string;
exclusionPatterns: string[];
}
export interface ITerminalSettings {
executeInFileDir: boolean;
launchArgs: string[];
}
export interface JupyterSettings {
appendResults: boolean;
defaultKernel: string;
startupCode: string[];
}
const IS_TEST_EXECUTION = process.env['PYTHON_DONJAYAMANNE_TEST'] === '1';
export class PythonSettings extends EventEmitter implements IPythonSettings {
private static pythonSettings: PythonSettings = new PythonSettings();
private disposables: vscode.Disposable[] = [];
constructor() {
super();
if (PythonSettings.pythonSettings) {
throw new Error('Singleton class, Use getInstance method');
}
this.disposables.push(vscode.workspace.onDidChangeConfiguration(() => {
this.initializeSettings();
}));
this.initializeSettings();
}
public static getInstance(): PythonSettings {
return PythonSettings.pythonSettings;
}
private initializeSettings() {
const systemVariables: SystemVariables = new SystemVariables();
const workspaceRoot = (IS_TEST_EXECUTION || typeof vscode.workspace.rootPath !== 'string') ? __dirname : vscode.workspace.rootPath;
let pythonSettings = vscode.workspace.getConfiguration('python');
this.pythonPath = systemVariables.resolveAny(pythonSettings.get<string>('pythonPath'));
this.pythonPath = getAbsolutePath(this.pythonPath, IS_TEST_EXECUTION ? __dirname : workspaceRoot);
this.jediPath = systemVariables.resolveAny(pythonSettings.get<string>('jediPath'));
if (typeof this.jediPath === 'string' && this.jediPath.length > 0) {
this.jediPath = getAbsolutePath(this.jediPath, IS_TEST_EXECUTION ? __dirname : workspaceRoot);
}
else {
this.jediPath = '';
}
this.devOptions = systemVariables.resolveAny(pythonSettings.get<any[]>('devOptions'));
this.devOptions = Array.isArray(this.devOptions) ? this.devOptions : [];
let lintingSettings = systemVariables.resolveAny(pythonSettings.get<ILintingSettings>('linting'));
if (this.linting) {
Object.assign<ILintingSettings, ILintingSettings>(this.linting, lintingSettings);
}
else {
this.linting = lintingSettings;
}
let sortImportSettings = systemVariables.resolveAny(pythonSettings.get<ISortImportSettings>('sortImports'));
if (this.sortImports) {
Object.assign<ISortImportSettings, ISortImportSettings>(this.sortImports, sortImportSettings);
}
else {
this.sortImports = sortImportSettings;
}
// Support for travis
this.sortImports = this.sortImports ? this.sortImports : { args: [] };
// Support for travis
this.linting = this.linting ? this.linting : {
enabled: false,
ignorePatterns: [],
flake8Args: [], flake8Enabled: false, flake8Path: 'flake',
lintOnSave: false, lintOnTextChange: false, maxNumberOfProblems: 100,
mypyArgs: [], mypyEnabled: false, mypyPath: 'mypy',
outputWindow: 'python', pep8Args: [], pep8Enabled: false, pep8Path: 'pep8',
pylamaArgs: [], pylamaEnabled: false, pylamaPath: 'pylama',
prospectorArgs: [], prospectorEnabled: false, prospectorPath: 'prospector',
pydocstyleArgs: [], pydocstyleEnabled: false, pydocstylePath: 'pydocstyle',
pylintArgs: [], pylintEnabled: false, pylintPath: 'pylint',
pylintCategorySeverity: {
convention: vscode.DiagnosticSeverity.Hint,
error: vscode.DiagnosticSeverity.Error,
fatal: vscode.DiagnosticSeverity.Error,
refactor: vscode.DiagnosticSeverity.Hint,
warning: vscode.DiagnosticSeverity.Warning
}
};
this.linting.pylintPath = getAbsolutePath(this.linting.pylintPath, workspaceRoot);
this.linting.flake8Path = getAbsolutePath(this.linting.flake8Path, workspaceRoot);
this.linting.pep8Path = getAbsolutePath(this.linting.pep8Path, workspaceRoot);
this.linting.pylamaPath = getAbsolutePath(this.linting.pylamaPath, workspaceRoot);
this.linting.prospectorPath = getAbsolutePath(this.linting.prospectorPath, workspaceRoot);
this.linting.pydocstylePath = getAbsolutePath(this.linting.pydocstylePath, workspaceRoot);
let formattingSettings = systemVariables.resolveAny(pythonSettings.get<IFormattingSettings>('formatting'));
if (this.formatting) {
Object.assign<IFormattingSettings, IFormattingSettings>(this.formatting, formattingSettings);
}
else {
this.formatting = formattingSettings;
}
// Support for travis
this.formatting = this.formatting ? this.formatting : {
autopep8Args: [], autopep8Path: 'autopep8',
outputWindow: 'python',
provider: 'autopep8',
yapfArgs: [], yapfPath: 'yapf',
formatOnSave: false
};
this.formatting.autopep8Path = getAbsolutePath(this.formatting.autopep8Path, workspaceRoot);
this.formatting.yapfPath = getAbsolutePath(this.formatting.yapfPath, workspaceRoot);
let autoCompleteSettings = systemVariables.resolveAny(pythonSettings.get<IAutoCompeteSettings>('autoComplete'));
if (this.autoComplete) {
Object.assign<IAutoCompeteSettings, IAutoCompeteSettings>(this.autoComplete, autoCompleteSettings);
}
else {
this.autoComplete = autoCompleteSettings;
}
// Support for travis
this.autoComplete = this.autoComplete ? this.autoComplete : {
extraPaths: [],
addBrackets: false
};
let workspaceSymbolsSettings = systemVariables.resolveAny(pythonSettings.get<IWorkspaceSymbolSettings>('workspaceSymbols'));
if (this.workspaceSymbols) {
Object.assign<IWorkspaceSymbolSettings, IWorkspaceSymbolSettings>(this.workspaceSymbols, workspaceSymbolsSettings);
}
else {
this.workspaceSymbols = workspaceSymbolsSettings;
}
// Support for travis
this.workspaceSymbols = this.workspaceSymbols ? this.workspaceSymbols : {
ctagsPath: 'ctags',
enabled: true,
exclusionPatterns: [],
rebuildOnFileSave: true,
rebuildOnStart: true,
tagFilePath: path.join(workspaceRoot, "tags")
};
let unitTestSettings = systemVariables.resolveAny(pythonSettings.get<IUnitTestSettings>('unitTest'));
if (this.unitTest) {
Object.assign<IUnitTestSettings, IUnitTestSettings>(this.unitTest, unitTestSettings);
}
else {
this.unitTest = unitTestSettings;
if (IS_TEST_EXECUTION && !this.unitTest) {
this.unitTest = { nosetestArgs: [], pyTestArgs: [], unittestArgs: [] } as IUnitTestSettings;
}
}
// Support for travis
this.unitTest = this.unitTest ? this.unitTest : {
promptToConfigure: true,
debugPort: 3000,
nosetestArgs: [], nosetestPath: 'nosetest', nosetestsEnabled: false,
outputWindow: 'python',
pyTestArgs: [], pyTestEnabled: false, pyTestPath: 'pytest',
unittestArgs: [], unittestEnabled: false
};
this.unitTest.pyTestPath = getAbsolutePath(this.unitTest.pyTestPath, workspaceRoot);
this.unitTest.nosetestPath = getAbsolutePath(this.unitTest.nosetestPath, workspaceRoot);
// Resolve any variables found in the test arguments
this.unitTest.nosetestArgs = this.unitTest.nosetestArgs.map(arg => systemVariables.resolveAny(arg));
this.unitTest.pyTestArgs = this.unitTest.pyTestArgs.map(arg => systemVariables.resolveAny(arg));
this.unitTest.unittestArgs = this.unitTest.unittestArgs.map(arg => systemVariables.resolveAny(arg));
let terminalSettings = systemVariables.resolveAny(pythonSettings.get<ITerminalSettings>('terminal'));
if (this.terminal) {
Object.assign<ITerminalSettings, ITerminalSettings>(this.terminal, terminalSettings);
}
else {
this.terminal = terminalSettings;
if (IS_TEST_EXECUTION && !this.terminal) {
this.terminal = {} as ITerminalSettings;
}
}
// Support for travis
this.terminal = this.terminal ? this.terminal : {
executeInFileDir: true,
launchArgs: []
};
this.jupyter = pythonSettings.get<JupyterSettings>('jupyter');
// Support for travis
this.jupyter = this.jupyter ? this.jupyter : {
appendResults: true, defaultKernel: '', startupCode: []
};
this.emit('change');
}
private _pythonPath: string;
public get pythonPath(): string {
return this._pythonPath;
}
public set pythonPath(value: string) {
if (this._pythonPath === value) {
return;
}
// Add support for specifying just the directory where the python executable will be located
// E.g. virtual directory name
try {
this._pythonPath = getPythonExecutable(value);
}
catch (ex) {
this._pythonPath = value;
}
}
public jediPath: string;
public devOptions: string[];
public linting: ILintingSettings;
public formatting: IFormattingSettings;
public autoComplete: IAutoCompeteSettings;
public unitTest: IUnitTestSettings;
public terminal: ITerminalSettings;
public jupyter: JupyterSettings;
public sortImports: ISortImportSettings;
public workspaceSymbols: IWorkspaceSymbolSettings;
}
function getAbsolutePath(pathToCheck: string, rootDir: string): string {
if (IS_TEST_EXECUTION && !pathToCheck) { return rootDir; }
if (pathToCheck.indexOf(path.sep) === -1) {
return pathToCheck;
}
return path.isAbsolute(pathToCheck) ? pathToCheck : path.resolve(rootDir, pathToCheck);
}
function getPythonExecutable(pythonPath: string): string {
// If only 'python'
if (pythonPath === 'python' ||
pythonPath.indexOf(path.sep) === -1 ||
path.basename(pythonPath) === path.dirname(pythonPath)) {
return pythonPath;
}
if (isValidPythonPath(pythonPath)) {
return pythonPath;
}
// Suffix with 'python' for linux and 'osx', and 'python.exe' for 'windows'
if (IS_WINDOWS) {
if (isValidPythonPath(path.join(pythonPath, 'python.exe'))) {
return path.join(pythonPath, 'python.exe');
}
if (isValidPythonPath(path.join(pythonPath, 'scripts', 'python.exe'))) {
return path.join(pythonPath, 'scripts', 'python.exe');
}
}
else {
if (isValidPythonPath(path.join(pythonPath, 'python'))) {
return path.join(pythonPath, 'python');
}
if (isValidPythonPath(path.join(pythonPath, 'bin', 'python'))) {
return path.join(pythonPath, 'bin', 'python');
}
}
return pythonPath;
}
function isValidPythonPath(pythonPath): boolean {
try {
let output = child_process.execFileSync(pythonPath, ['-c', 'print(1234)'], { encoding: 'utf8' });
return output.startsWith('1234');
}
catch (ex) {
return false;
}
}