Repository navigation
Expand file tree
/
Copy pathPythonLspConfiguration.java
More file actions
266 lines (244 loc) · 11.4 KB
/
Copy pathPythonLspConfiguration.java
File metadata and controls
266 lines (244 loc) · 11.4 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
package org.netbeans.modules.python;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ChangeListener;
import org.eclipse.lsp4j.DidChangeConfigurationParams;
import org.netbeans.api.project.Project;
import org.netbeans.modules.lsp.client.LSPBindings;
import org.netbeans.modules.lsp.client.LanguageServerProviderAccessor;
import org.netbeans.modules.lsp.client.spi.LanguageServerProvider.LanguageServerDescription;
import org.netbeans.modules.python.project.PythonProject;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
/**
* Sends pylsp its settings together with the interpreter of the project it
* serves, so that Jedi completes the packages installed in that interpreter
* instead of those of the plugin's own pylsp environment (#144).
*/
public final class PythonLspConfiguration {
private static final Logger LOG = Logger.getLogger(PythonLspConfiguration.class.getName());
private static final RequestProcessor WORKER = new RequestProcessor(PythonLspConfiguration.class.getName(), 1);
/** Running Python servers and the project each was started for (null for files outside projects). */
private static final Map<LSPBindings, Project> SERVERS = new WeakHashMap<>();
/** Started servers that NetBeans has not initialized yet. */
private static final Map<LanguageServerDescription, PendingConfiguration> PENDING = new ConcurrentHashMap<>();
private static final Gson GSON = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.BIG_DECIMAL).create();
private static final Type SETTINGS_TYPE = new TypeToken<Map<String, Object>>() {
}.getType();
/** Held here because NetBeans keeps its change listeners only weakly. */
private static final ChangeListener BINDINGS_LISTENER = event -> configurePendingServers();
static {
// NetBeans fires this after it has initialized a server and published its bindings.
LSPBindings.addChangeListener(BINDINGS_LISTENER);
}
private PythonLspConfiguration() {
}
/** Called by {@link PythonLspClient} for every start and restart of a server. */
static void serverStarted(Project project, LanguageServerDescription description) {
PendingConfiguration pending = new PendingConfiguration(project);
PENDING.put(description, pending);
Process process = LanguageServerProviderAccessor.getINSTANCE().getProcess(description);
if (process != null) {
// Forget servers that exit, also those NetBeans never initialized.
process.onExit().thenRun(() -> {
PENDING.remove(description, pending);
LSPBindings bindings = pending.bindings;
if (bindings != null) {
synchronized (SERVERS) {
SERVERS.remove(bindings);
}
}
});
}
}
static void configurePendingServers() {
PENDING.forEach((description, pending) -> {
LSPBindings bindings = LanguageServerProviderAccessor.getINSTANCE().getBindings(description);
if (bindings != null && PENDING.remove(description, pending)) {
synchronized (SERVERS) {
pending.bindings = bindings;
Process process = LanguageServerProviderAccessor.getINSTANCE().getProcess(description);
if (process == null || process.isAlive()) {
SERVERS.put(bindings, pending.project);
}
}
WORKER.post(() -> {
try {
configure(bindings, pending.project, readSettings());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
});
}
});
}
/** Waits until the configuration posted so far has been sent; for tests. */
static void waitForPendingUpdates() {
WORKER.post(() -> { }).waitFinished();
}
/** Sends the current interpreter to the running servers of a project, after its interpreter changed. */
public static void configureProject(Project project) {
WORKER.post(() -> {
try {
Map<String, Object> settings = readSettings();
running().forEach((bindings, server) -> {
if (server != null && server.getProjectDirectory().equals(project.getProjectDirectory())) {
configure(bindings, server, settings);
}
});
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
});
}
/** Sends changed global settings to all Python servers, each with its own project's interpreter. */
public static void configureRunningServers(Map<String, Object> settings) {
WORKER.post(() -> running().forEach((bindings, project) -> configure(bindings, project, settings)));
}
private static Map<LSPBindings, Project> running() {
synchronized (SERVERS) {
return new HashMap<>(SERVERS);
}
}
static void configure(LSPBindings bindings, Project project, Map<String, Object> settings) {
DidChangeConfigurationParams params = configuration(settings, interpreter(project, settings));
try {
bindings.getWorkspaceService().didChangeConfiguration(params);
} catch (RuntimeException ex) {
// A server that has exited fails here; NetBeans starts and configures a new one when needed.
LOG.log(Level.FINE, "Cannot configure a Python language server", ex);
synchronized (SERVERS) {
SERVERS.remove(bindings);
}
}
}
/**
* The interpreter Jedi should use: the one chosen for the project, else a
* {@code plugins.jedi.environment} set in the settings (returns null to keep
* it), else the default Python platform. Returns null for an interpreter
* that does not exist, because pylsp then fails every Jedi request.
*/
static String interpreter(Project project, Map<String, Object> settings) {
try {
String interpreter = projectInterpreter(project);
if (interpreter == null) {
if (configuredEnvironment(settings) != null) {
return null;
}
interpreter = PythonUtility.getPlatformPythonExe();
}
if (interpreter == null || interpreter.isBlank()) {
return null;
}
if (!new File(interpreter).isFile()) {
LOG.log(Level.INFO, "Python interpreter {0} not found; completion uses the language server''s own"
+ " environment", interpreter);
return null;
}
return interpreter;
} catch (IOException | RuntimeException ex) {
// For example a damaged platforms.json: keep pylsp's own environment.
Exceptions.printStackTrace(ex);
return null;
}
}
/** The interpreter selected for the project, or null if none was selected. */
static String projectInterpreter(Project project) throws IOException {
if (project == null) {
return null;
}
FileObject configuration = project.getProjectDirectory().getFileObject("nbproject/project.properties");
if (configuration != null) {
Properties properties = new Properties();
try (InputStream input = configuration.getInputStream()) {
properties.load(input);
}
String interpreter = properties.getProperty("nbproject.python_path");
if (interpreter != null && !interpreter.isBlank()) {
return interpreter.strip();
}
}
if (project instanceof PythonProject && PythonUtility.isPoetry((PythonProject) project)) {
String lspPython = PythonUtility.getLspPythonExe();
String poetryPython = PythonUtility.getPoetryPythonPath(lspPython, project.getProjectDirectory());
if (!poetryPython.equals(lspPython)) {
return poetryPython;
}
}
return null;
}
/**
* The pylsp settings to send: the global settings with
* {@code plugins.jedi.environment} set to the given interpreter, if any.
* The global settings are not modified.
*/
static DidChangeConfigurationParams configuration(Map<String, Object> globalSettings, String interpreter) {
Map<String, Object> settings = new HashMap<>(globalSettings);
settings.remove("auto_pop_completion");
Map<String, Object> plugins = copyMap(settings.get("plugins"));
Map<String, Object> jedi = copyMap(plugins.get("jedi"));
if (interpreter != null) {
jedi.put("environment", interpreter);
}
// The default settings have "env_vars": {}. Once an environment is set,
// Jedi starts its interpreter with exactly these variables, so an empty
// map would leave it without PATH, HOME or (on Windows) SYSTEMROOT.
// Without the key pylsp passes on its own environment.
if (jedi.get("env_vars") instanceof Map && ((Map<?, ?>) jedi.get("env_vars")).isEmpty()) {
jedi.remove("env_vars");
}
if (!jedi.isEmpty()) {
plugins.put("jedi", jedi);
settings.put("plugins", plugins);
}
return new DidChangeConfigurationParams(Map.of("pylsp", settings));
}
private static Object configuredEnvironment(Map<String, Object> settings) {
Object environment = copyMap(copyMap(settings.get("plugins")).get("jedi")).get("environment");
return environment instanceof String && !((String) environment).isBlank() ? environment : null;
}
@SuppressWarnings("unchecked")
private static Map<String, Object> copyMap(Object value) {
return value instanceof Map ? new HashMap<>((Map<String, Object>) value) : new HashMap<>();
}
static Map<String, Object> readSettings() throws IOException {
String json;
if (PythonUtility.SETTINGS.isFile()) {
json = Files.readString(PythonUtility.SETTINGS.toPath());
} else {
try (InputStream input = PythonLspConfiguration.class.getResourceAsStream("settings.json")) {
if (input == null) {
throw new IOException("Missing Python language server settings");
}
json = new String(input.readAllBytes(), StandardCharsets.UTF_8);
}
}
Map<String, Object> settings = GSON.fromJson(json, SETTINGS_TYPE);
return settings == null ? Map.of() : settings;
}
private static final class PendingConfiguration {
final Project project;
/** Set once NetBeans has initialized the server; guarded by SERVERS. */
LSPBindings bindings;
PendingConfiguration(Project project) {
this.project = project;
}
}
}