-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsettings.ts
More file actions
242 lines (218 loc) · 9.74 KB
/
settings.ts
File metadata and controls
242 lines (218 loc) · 9.74 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConfigurationChangeEvent, ConfigurationScope, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
import { traceLog, traceWarn } from './logging';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
import { getInterpreterFromSetting } from './utilities';
import { expandTilde } from './envFile';
/* eslint-disable @typescript-eslint/naming-convention */
const DEFAULT_SEVERITY: Record<string, string> = {
E: 'Error',
F: 'Error',
I: 'Information',
W: 'Warning',
};
export interface ISettings {
cwd: string;
enabled: boolean;
workspace: string;
args: string[];
severity: Record<string, string>;
path: string[];
ignorePatterns: string[];
interpreter: string[];
importStrategy: string;
showNotifications: string;
extraPaths: string[];
}
export function getExtensionSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings[]> {
return Promise.all(getWorkspaceFolders().map((w) => getWorkspaceSettings(namespace, w, includeInterpreter)));
}
function resolveVariables(
value: string[],
workspace?: WorkspaceFolder,
interpreter?: string[],
env?: NodeJS.ProcessEnv,
): string[] {
const substitutions = new Map<string, string>();
const home = process.env.HOME || process.env.USERPROFILE;
if (home) {
substitutions.set('${userHome}', home);
}
if (workspace) {
substitutions.set('${workspaceFolder}', workspace.uri.fsPath);
}
substitutions.set('${cwd}', process.cwd());
getWorkspaceFolders().forEach((w) => {
substitutions.set('${workspaceFolder:' + w.name + '}', w.uri.fsPath);
});
env = env || process.env;
if (env) {
for (const [key, value] of Object.entries(env)) {
if (value) {
substitutions.set('${env:' + key + '}', value);
}
}
}
const modifiedValue = [];
for (const v of value) {
if (interpreter && v === '${interpreter}') {
modifiedValue.push(...interpreter);
} else {
modifiedValue.push(v);
}
}
return modifiedValue.map((s) => {
for (const [key, value] of substitutions) {
s = s.replace(key, value);
}
return s;
});
}
function getCwd(config: WorkspaceConfiguration, workspace: WorkspaceFolder): string {
const cwd = config.get<string>('cwd', workspace.uri.fsPath);
return resolveVariables([cwd], workspace)[0];
}
function getExtraPaths(namespace: string, workspace: WorkspaceFolder): string[] {
const config = getConfiguration(namespace, workspace);
const extraPaths = config.get<string[]>('extraPaths', []);
if (extraPaths.length > 0) {
return extraPaths;
}
// Fall back to python.analysis.extraPaths if the extension-specific setting is empty
const pythonConfig = getConfiguration('python', workspace.uri);
const legacyExtraPaths = pythonConfig.get<string[]>('analysis.extraPaths', []);
if (legacyExtraPaths.length > 0) {
traceLog('Using extraPaths from `python.analysis.extraPaths`.');
}
return legacyExtraPaths;
}
export async function getWorkspaceSettings(
namespace: string,
workspace: WorkspaceFolder,
includeInterpreter?: boolean,
): Promise<ISettings> {
const config = getConfiguration(namespace, workspace);
let interpreter: string[] = [];
if (includeInterpreter) {
interpreter = getInterpreterFromSetting(namespace, workspace) ?? [];
if (interpreter.length === 0) {
traceLog(`No interpreter found from setting ${namespace}.interpreter`);
traceLog(`Getting interpreter from ms-python.python extension for workspace ${workspace.uri.fsPath}`);
interpreter = (await getInterpreterDetails(workspace.uri)).path ?? [];
if (interpreter.length > 0) {
traceLog(
`Interpreter from ms-python.python extension for ${workspace.uri.fsPath}:`,
`${interpreter.join(' ')}`,
);
}
} else {
traceLog(`Interpreter from setting ${namespace}.interpreter: ${interpreter.join(' ')}`);
}
if (interpreter.length === 0) {
traceLog(`No interpreter found for ${workspace.uri.fsPath} in settings or from ms-python.python extension`);
}
}
const workspaceSetting = {
cwd: getCwd(config, workspace),
enabled: config.get<boolean>('enabled', true),
workspace: workspace.uri.toString(),
args: resolveVariables(config.get<string[]>('args', []), workspace),
severity: config.get<Record<string, string>>('severity', DEFAULT_SEVERITY),
path: resolveVariables(config.get<string[]>('path', []), workspace, interpreter),
ignorePatterns: resolveVariables(config.get<string[]>('ignorePatterns', []), workspace),
interpreter: resolveVariables(interpreter, workspace),
importStrategy: config.get<string>('importStrategy', 'useBundled'),
showNotifications: config.get<string>('showNotifications', 'onError'),
extraPaths: resolveVariables(getExtraPaths(namespace, workspace), workspace),
};
// Apply tilde expansion only to path-typed settings
workspaceSetting.path = workspaceSetting.path.map(expandTilde);
workspaceSetting.extraPaths = workspaceSetting.extraPaths.map(expandTilde);
if (workspaceSetting.cwd.startsWith('~')) {
workspaceSetting.cwd = expandTilde(workspaceSetting.cwd);
}
return workspaceSetting;
}
function getGlobalValue<T>(config: WorkspaceConfiguration, key: string, defaultValue: T): T {
const inspect = config.inspect<T>(key);
return inspect?.globalValue ?? inspect?.defaultValue ?? defaultValue;
}
export async function getGlobalSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings> {
const config = getConfiguration(namespace);
let interpreter: string[] = [];
if (includeInterpreter) {
interpreter = getGlobalValue<string[]>(config, 'interpreter', []);
if (interpreter === undefined || interpreter.length === 0) {
interpreter = (await getInterpreterDetails()).path ?? [];
}
}
const setting = {
cwd: getGlobalValue<string>(config, 'cwd', process.cwd()),
enabled: getGlobalValue<boolean>(config, 'enabled', true),
workspace: process.cwd(),
args: getGlobalValue<string[]>(config, 'args', []),
severity: getGlobalValue<Record<string, string>>(config, 'severity', DEFAULT_SEVERITY),
path: getGlobalValue<string[]>(config, 'path', []),
ignorePatterns: getGlobalValue<string[]>(config, 'ignorePatterns', []),
interpreter: interpreter ?? [],
importStrategy: getGlobalValue<string>(config, 'importStrategy', 'fromEnvironment'),
showNotifications: getGlobalValue<string>(config, 'showNotifications', 'onError'),
extraPaths: getGlobalValue<string[]>(config, 'extraPaths', []),
};
return setting;
}
export function checkIfConfigurationChanged(e: ConfigurationChangeEvent, namespace: string): boolean {
const settings = [
`${namespace}.args`,
`${namespace}.cwd`,
`${namespace}.enabled`,
`${namespace}.severity`,
`${namespace}.path`,
`${namespace}.interpreter`,
`${namespace}.importStrategy`,
`${namespace}.showNotifications`,
`${namespace}.ignorePatterns`,
`${namespace}.extraPaths`,
];
const changed = settings.map((s) => e.affectsConfiguration(s));
return changed.includes(true);
}
export function logLegacySettings(): void {
getWorkspaceFolders().forEach((workspace) => {
try {
const legacyConfig = getConfiguration('python', workspace.uri);
const legacyFlake8Enabled = legacyConfig.get<boolean>('linting.flake8Enabled', false);
if (legacyFlake8Enabled) {
traceWarn(`"python.linting.flake8Enabled" is deprecated. You can remove that setting.`);
traceWarn(
'The flake8 extension is always enabled. However, you can disable it per workspace using the extensions view.',
);
traceWarn('You can exclude files and folders using the `python.linting.ignorePatterns` setting.');
traceWarn(
`"python.linting.flake8Enabled" value for workspace ${workspace.uri.fsPath}: ${legacyFlake8Enabled}`,
);
}
const legacyCwd = legacyConfig.get<string>('linting.cwd');
if (legacyCwd) {
traceWarn(`"python.linting.cwd" is deprecated. Use "flake8.cwd" instead.`);
traceWarn(`"python.linting.cwd" value for workspace ${workspace.uri.fsPath}: ${legacyCwd}`);
}
const legacyArgs = legacyConfig.get<string[]>('linting.flake8Args', []);
if (legacyArgs.length > 0) {
traceWarn(`"python.linting.flake8Args" is deprecated. Use "flake8.args" instead.`);
traceWarn(`"python.linting.flake8Args" value for workspace ${workspace.uri.fsPath}:`);
traceWarn(`\n${JSON.stringify(legacyArgs, null, 4)}`);
}
const legacyPath = legacyConfig.get<string>('linting.flake8Path', '');
if (legacyPath.length > 0 && legacyPath !== 'flake8') {
traceWarn(`"python.linting.flake8Path" is deprecated. Use "flake8.path" instead.`);
traceWarn(`"python.linting.flake8Path" value for workspace ${workspace.uri.fsPath}:`);
traceWarn(`\n${JSON.stringify(legacyPath, null, 4)}`);
}
} catch (err) {
traceWarn(`Error while logging legacy settings: ${err}`);
}
});
}