|
33 | 33 | import java.util.Comparator; |
34 | 34 | import java.util.HashSet; |
35 | 35 | import java.util.List; |
36 | | -import java.util.Map; |
37 | 36 | import java.util.Set; |
38 | | -import java.util.TreeSet; |
39 | 37 | import java.util.concurrent.CompletableFuture; |
40 | 38 | import java.util.function.Function; |
41 | | -import java.util.stream.Collectors; |
42 | 39 |
|
43 | 40 | import org.eclipse.lsp4j.CompletionItem; |
44 | 41 | import org.eclipse.lsp4j.CompletionList; |
|
57 | 54 | import org.eclipse.lsp4j.services.TextDocumentService; |
58 | 55 | import org.scijava.code.lsp.Environment; |
59 | 56 | import org.scijava.code.lsp.LanguageServerService; |
60 | | -import org.scijava.code.lsp.script.ScriptDocument; |
61 | | -import org.scijava.code.lsp.script.TransformingLanguageServer; |
62 | 57 |
|
63 | 58 | /** |
64 | 59 | * A Python language server (e.g. jedi-language-server, if the |
65 | 60 | * {@code appose-python} scripting plugin offers one) beside Jython's own |
66 | 61 | * analysis (see {@link JythonLanguageServer}): for Python modules, and for |
67 | | - * Java classes, from Python stubs of the Java packages a script imports and |
68 | | - * its parameters' types (see {@link JavaStubs}, written in the background). |
| 62 | + * Java classes, from Python stubs of the Java packages a script imports |
| 63 | + * (including those of its parameters' types, declared in its text; see |
| 64 | + * {@link JavaStubs}, written in the background). |
69 | 65 | * <p> |
70 | 66 | * The server analyzes scripts as CPython (Jython's standard library is Python |
71 | 67 | * 2's, but mostly the same): so its completions follow Jython's own, it is |
|
77 | 73 | */ |
78 | 74 | final class PythonCompanion { |
79 | 75 |
|
80 | | - /** The server, seeing scripts as plain Python. */ |
| 76 | + /** The server. */ |
81 | 77 | private final LanguageServer server; |
82 | 78 | private final JavaStubs stubs; |
83 | | - private final Function<String, Map<String, Class<?>>> parameters; |
84 | 79 |
|
85 | | - private PythonCompanion(final LanguageServer python, final JavaStubs stubs, |
86 | | - final Function<String, Map<String, Class<?>>> parameters) |
87 | | - { |
| 80 | + private PythonCompanion(final LanguageServer python, final JavaStubs stubs) { |
88 | 81 | this.stubs = stubs; |
89 | | - this.parameters = parameters; |
90 | 82 | // NB: No client: the server's problems are not asked for. |
91 | | - server = new TransformingLanguageServer(python, text -> ScriptDocument.of( |
92 | | - text, parameters.apply(text), JythonLanguageServer.DIALECT, null)); |
| 83 | + server = python; |
93 | 84 | server.initialize(new InitializeParams()).thenRun(() -> server |
94 | 85 | .initialized(new InitializedParams())); |
95 | 86 | } |
96 | 87 |
|
97 | 88 | /** |
98 | 89 | * Gets a Python server beside Jython's, or null if none is offered. |
99 | 90 | * |
100 | | - * @param servers Offers the server, and reads the scripts' parameters. |
| 91 | + * @param servers Offers the server. |
101 | 92 | * @param stubs The stubs of Java packages, for the server to search. |
102 | 93 | */ |
103 | 94 | static PythonCompanion launch(final LanguageServerService servers, |
104 | 95 | final JavaStubs stubs) |
105 | 96 | { |
106 | 97 | if (servers == null) return null; |
107 | 98 | final LanguageServer python = servers.launch("python", environment(stubs)); |
108 | | - return python == null ? null : new PythonCompanion(python, stubs, |
109 | | - servers::scriptParameters); |
| 99 | + return python == null ? null : new PythonCompanion(python, stubs); |
110 | 100 | } |
111 | 101 |
|
112 | 102 | /** Where the server runs: its own Python, searching the Java stubs. */ |
@@ -192,14 +182,9 @@ private TextDocumentService documents() { |
192 | 182 | return server.getTextDocumentService(); |
193 | 183 | } |
194 | 184 |
|
195 | | - /** Writes the stubs the script needs, in the background. */ |
| 185 | + /** Writes the stubs of the packages the script imports, in the background. */ |
196 | 186 | private void ensureStubs(final String text) { |
197 | | - final Set<String> packages = new TreeSet<>(JavaStubs.imports(text)); |
198 | | - final ScriptDocument doc = ScriptDocument.of(text, parameters.apply(text), |
199 | | - JythonLanguageServer.DIALECT, null); |
200 | | - packages.addAll(JythonDialect.packages(doc.parameters().values().stream() |
201 | | - .filter(t -> t != null).collect(Collectors.toList()))); |
202 | | - stubs.ensure(packages); |
| 187 | + stubs.ensure(JavaStubs.imports(text)); |
203 | 188 | } |
204 | 189 |
|
205 | 190 | /** |
|
0 commit comments