This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathLanguageServer.Configuration.cs
249 lines (208 loc) · 11.4 KB
/
LanguageServer.Configuration.cs
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
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Analysis;
using Microsoft.Python.Analysis.Analyzer;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Collections;
using Microsoft.Python.Core.IO;
using Microsoft.Python.Core.OS;
using Microsoft.Python.LanguageServer.CodeActions;
using Microsoft.Python.LanguageServer.Protocol;
using Microsoft.Python.LanguageServer.SearchPaths;
using Newtonsoft.Json.Linq;
using StreamJsonRpc;
using DiagnosticSource = Microsoft.Python.Analysis.Diagnostics.DiagnosticSource;
namespace Microsoft.Python.LanguageServer.Implementation {
public sealed partial class LanguageServer {
[JsonRpcMethod("workspace/didChangeConfiguration")]
public async Task DidChangeConfiguration(JToken token, CancellationToken cancellationToken) {
using (await _prioritizer.ConfigurationPriorityAsync(cancellationToken)) {
Debug.Assert(_initialized);
var settings = new LanguageServerSettings();
// https://github.com/microsoft/python-language-server/issues/915
// If token or settings are missing, assume defaults.
var rootSection = token?["settings"];
var pythonSection = rootSection?["python"];
if (pythonSection == null) {
return;
}
var autoComplete = pythonSection["autoComplete"];
settings.completion.showAdvancedMembers = GetSetting(autoComplete, "showAdvancedMembers", true);
settings.completion.addBrackets = GetSetting(autoComplete, "addBrackets", false);
var analysis = pythonSection["analysis"];
settings.symbolsHierarchyDepthLimit = GetSetting(analysis, "symbolsHierarchyDepthLimit", 10);
settings.symbolsHierarchyMaxSymbols = GetSetting(analysis, "symbolsHierarchyMaxSymbols", 1000);
_logger.LogLevel = GetLogLevel(analysis).ToTraceEventType();
var userConfiguredPaths = GetUserConfiguredPaths(pythonSection);
HandleUserConfiguredPathsChanges(userConfiguredPaths);
HandlePathWatchChanges(GetSetting(analysis, "watchSearchPaths", false));
HandleDiagnosticsChanges(pythonSection, settings);
HandleCodeActionsChanges(pythonSection);
_server.DidChangeConfiguration(new DidChangeConfigurationParams { settings = settings }, cancellationToken);
}
}
private void HandleCodeActionsChanges(JToken pythonSection) {
var refactoring = new Dictionary<string, object>();
var quickFix = new Dictionary<string, object>();
var refactoringToken = pythonSection["refactoring"];
var quickFixToken = pythonSection["quickfix"];
// +1 is for last "." after prefix
AppendToMap(refactoringToken, refactoringToken?.Path.Length + 1 ?? 0, refactoring);
AppendToMap(quickFixToken, quickFixToken?.Path.Length + 1 ?? 0, quickFix);
var codeActionSettings = new CodeActionSettings(refactoring, quickFix);
_server.HandleCodeActionsChange(codeActionSettings);
void AppendToMap(JToken setting, int prefixLength, Dictionary<string, object> map) {
if (setting == null || !setting.HasValues) {
return;
}
foreach (var child in setting) {
if (child is JValue value) {
// there shouldn't be duplicates and prefix must exist.
var path = child.Path;
if (path.Length <= prefixLength) {
// nothing to add
continue;
}
// get rid of common "settings.python..." prefix
map[path.Substring(prefixLength)] = value.Value;
continue;
}
AppendToMap(child, prefixLength, map);
}
}
}
private void HandleDiagnosticsChanges(JToken pythonSection, LanguageServerSettings settings) {
var analysis = pythonSection["analysis"];
settings.diagnosticPublishDelay = GetSetting(analysis, "diagnosticPublishDelay", 200);
var ds = _services.GetService<IDiagnosticsService>();
ds.PublishingDelay = settings.diagnosticPublishDelay;
ds.DiagnosticsSeverityMap = new DiagnosticsSeverityMap(
GetSetting(analysis, "errors", Array.Empty<string>()),
GetSetting(analysis, "warnings", Array.Empty<string>()),
GetSetting(analysis, "information", Array.Empty<string>()),
GetSetting(analysis, "disabled", Array.Empty<string>()));
var linting = pythonSection["linting"];
HandleLintingOnOff(_services, GetSetting(linting, "enabled", true));
var memory = analysis["memory"];
var optionsProvider = _services.GetService<IAnalysisOptionsProvider>();
optionsProvider.Options.KeepLibraryAst = GetSetting(memory, "keepLibraryAst", false);
optionsProvider.Options.AnalysisCachingLevel = GetAnalysisCachingLevel(analysis);
_logger?.Log(TraceEventType.Information, Resources.AnalysisCacheLevel.FormatInvariant(optionsProvider.Options.AnalysisCachingLevel));
}
internal static void HandleLintingOnOff(IServiceContainer services, bool linterEnabled) {
var optionsProvider = services.GetService<IAnalysisOptionsProvider>();
var ds = services.GetService<IDiagnosticsService>();
var rdt = services.GetService<IRunningDocumentTable>();
var wasEnabled = optionsProvider.Options.LintingEnabled;
optionsProvider.Options.LintingEnabled = linterEnabled;
foreach (var m in rdt.GetDocuments().Where(m => m.ModuleType == ModuleType.User)) {
IReadOnlyList<DiagnosticsEntry> entries = Array.Empty<DiagnosticsEntry>();
if (!wasEnabled && linterEnabled) {
// Lint all user files in the RDT
var analyzer = services.GetService<IPythonAnalyzer>();
entries = analyzer.LintModule(m);
}
ds.Replace(m.Uri, entries, DiagnosticSource.Linter);
}
}
private void HandlePathWatchChanges(bool watchSearchPaths) => _server.HandleWatchPathsChange(watchSearchPaths);
private void HandleUserConfiguredPathsChanges(ImmutableArray<string> paths) => _server.HandleUserConfiguredPathsChange(paths);
/// <summary>
/// Gets the user's configured search paths, by python.analysis.searchPaths,
/// python.autoComplete.extraPaths, PYTHONPATH, or _initParam's searchPaths.
/// </summary>
/// <param name="pythonSection">The python section of the user config.</param>
/// <returns>An array of search paths.</returns>
private ImmutableArray<string> GetUserConfiguredPaths(JToken pythonSection) {
var paths = ImmutableArray<string>.Empty;
var set = false;
JToken autoComplete;
JToken analysis;
try {
autoComplete = pythonSection?["autoComplete"];
analysis = pythonSection?["analysis"];
} catch (Exception e) {
analysis = JToken.Parse("{ 'errors': [], 'warnings': [], 'information': [], 'disabled': [] }");
autoComplete = JToken.Parse("{ 'extraPaths': [] }");
}
// The values of these may not be null even if the value is "unset", depending on
// what the client uses as a default. Use null as a default anyway until the
// extension uses a null default (and/or extraPaths is dropped entirely).
var autoCompleteExtraPaths = GetSetting<IReadOnlyList<string>>(autoComplete, "extraPaths", null);
var analysisSearchPaths = GetSetting<IReadOnlyList<string>>(analysis, "searchPaths", null);
var analysisUsePYTHONPATH = GetSetting(analysis, "usePYTHONPATH", true);
var analayisAutoSearchPaths = GetSetting(analysis, "autoSearchPaths", true);
if (analysisSearchPaths != null) {
set = true;
paths = analysisSearchPaths.ToImmutableArray();
} else if (autoCompleteExtraPaths != null) {
set = true;
paths = autoCompleteExtraPaths.ToImmutableArray();
}
if (analysisUsePYTHONPATH) {
var pythonpath = Environment.GetEnvironmentVariable("PYTHONPATH");
if (pythonpath != null) {
var sep = _services.GetService<IOSPlatform>().IsWindows ? ';' : ':';
var pythonpathPaths = pythonpath.Split(sep, StringSplitOptions.RemoveEmptyEntries);
if (pythonpathPaths.Length > 0) {
paths = paths.AddRange(pythonpathPaths);
set = true;
}
}
}
if (analayisAutoSearchPaths) {
var fs = _services.GetService<IFileSystem>();
var auto = AutoSearchPathFinder.Find(fs, _server.Root);
paths = paths.AddRange(auto);
set = true;
}
if (set) {
return paths;
}
var initPaths = _initParams?.initializationOptions?.searchPaths;
if (initPaths != null) {
return initPaths.ToImmutableArray();
}
return ImmutableArray<string>.Empty;
}
private const string DefaultCachingLevel = "None";
private AnalysisCachingLevel GetAnalysisCachingLevel(JToken analysisKey) {
// TODO: Remove this one caching is working at any level again.
// https://github.com/microsoft/python-language-server/issues/1758
return AnalysisCachingLevel.None;
// var s = GetSetting(analysisKey, "cachingLevel", DefaultCachingLevel);
//
// if (string.IsNullOrWhiteSpace(s) || s.EqualsIgnoreCase("Default")) {
// s = DefaultCachingLevel;
// }
//
// if (s.EqualsIgnoreCase("System")) {
// return AnalysisCachingLevel.System;
// }
//
// return s.EqualsIgnoreCase("Library") ? AnalysisCachingLevel.Library : AnalysisCachingLevel.None;
}
}
}