forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
137 lines (127 loc) · 5.64 KB
/
service.ts
File metadata and controls
137 lines (127 loc) · 5.64 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { inject, injectable } from 'inversify';
import { ConfigurationTarget, Uri, WorkspaceConfiguration } from 'vscode';
import { IInterpreterAutoSelectionService } from '../../interpreter/autoSelection/types';
import { IServiceContainer } from '../../ioc/types';
import { IWorkspaceService } from '../application/types';
import { PythonSettings } from '../configSettings';
import { isUnitTestExecution } from '../constants';
import { DeprecatePythonPath } from '../experiments/groups';
import {
IConfigurationService,
IDefaultLanguageServer,
IExperimentService,
IInterpreterPathService,
IPythonSettings,
} from '../types';
@injectable()
export class ConfigurationService implements IConfigurationService {
private readonly workspaceService: IWorkspaceService;
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
}
public getSettings(resource?: Uri): IPythonSettings {
const InterpreterAutoSelectionService = this.serviceContainer.get<IInterpreterAutoSelectionService>(
IInterpreterAutoSelectionService,
);
const interpreterPathService = this.serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
const experiments = this.serviceContainer.get<IExperimentService>(IExperimentService);
const defaultLS = this.serviceContainer.tryGet<IDefaultLanguageServer>(IDefaultLanguageServer);
return PythonSettings.getInstance(
resource,
InterpreterAutoSelectionService,
this.workspaceService,
experiments,
interpreterPathService,
defaultLS,
);
}
public async updateSectionSetting(
section: string,
setting: string,
value?: unknown,
resource?: Uri,
configTarget?: ConfigurationTarget,
): Promise<void> {
const experiments = this.serviceContainer.get<IExperimentService>(IExperimentService);
const interpreterPathService = this.serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
const inExperiment = experiments.inExperimentSync(DeprecatePythonPath.experiment);
const defaultSetting = {
uri: resource,
target: configTarget || ConfigurationTarget.WorkspaceFolder,
};
let settingsInfo = defaultSetting;
if (section === 'python' && configTarget !== ConfigurationTarget.Global) {
settingsInfo = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService);
}
configTarget = configTarget || settingsInfo.target;
const configSection = this.workspaceService.getConfiguration(section, settingsInfo.uri);
const currentValue =
inExperiment && section === 'python' && setting === 'pythonPath'
? interpreterPathService.inspect(settingsInfo.uri)
: configSection.inspect(setting);
if (
currentValue !== undefined &&
((configTarget === ConfigurationTarget.Global && currentValue.globalValue === value) ||
(configTarget === ConfigurationTarget.Workspace && currentValue.workspaceValue === value) ||
(configTarget === ConfigurationTarget.WorkspaceFolder && currentValue.workspaceFolderValue === value))
) {
return;
}
if (section === 'python' && setting === 'pythonPath') {
if (inExperiment) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await interpreterPathService.update(settingsInfo.uri, configTarget, value as any);
}
} else {
await configSection.update(setting, value, configTarget);
await this.verifySetting(configSection, configTarget, setting, value);
}
}
public async updateSetting(
setting: string,
value?: unknown,
resource?: Uri,
configTarget?: ConfigurationTarget,
): Promise<void> {
return this.updateSectionSetting('python', setting, value, resource, configTarget);
}
// eslint-disable-next-line class-methods-use-this
public isTestExecution(): boolean {
return process.env.VSC_PYTHON_CI_TEST === '1';
}
private async verifySetting(
configSection: WorkspaceConfiguration,
target: ConfigurationTarget,
settingName: string,
value?: unknown,
): Promise<void> {
if (this.isTestExecution() && !isUnitTestExecution()) {
let retries = 0;
do {
const setting = configSection.inspect(settingName);
if (!setting && value === undefined) {
break; // Both are unset
}
if (setting && value !== undefined) {
// Both specified
let actual;
if (target === ConfigurationTarget.Global) {
actual = setting.globalValue;
} else if (target === ConfigurationTarget.Workspace) {
actual = setting.workspaceValue;
} else {
actual = setting.workspaceFolderValue;
}
if (actual === value) {
break;
}
}
// Wait for settings to get refreshed.
await new Promise((resolve) => setTimeout(resolve, 250));
retries += 1;
} while (retries < 20);
}
}
}