3636import java .util .List ;
3737import java .util .Map ;
3838import java .util .Set ;
39+ import java .util .concurrent .CompletableFuture ;
3940
4041import javax .script .Bindings ;
4142import javax .script .ScriptContext ;
4243
4344import org .eclipse .lsp4j .CompletionItem ;
4445import org .eclipse .lsp4j .CompletionItemKind ;
4546import org .eclipse .lsp4j .CompletionList ;
47+ import org .eclipse .lsp4j .CompletionParams ;
48+ import org .eclipse .lsp4j .DidChangeTextDocumentParams ;
49+ import org .eclipse .lsp4j .DidCloseTextDocumentParams ;
50+ import org .eclipse .lsp4j .DidOpenTextDocumentParams ;
51+ import org .eclipse .lsp4j .Hover ;
52+ import org .eclipse .lsp4j .HoverParams ;
4653import org .eclipse .lsp4j .SignatureHelp ;
54+ import org .eclipse .lsp4j .SignatureHelpParams ;
55+ import org .eclipse .lsp4j .jsonrpc .messages .Either ;
4756import org .python .core .PyJavaType ;
4857import org .python .core .PyModule ;
4958import org .python .core .PyObject ;
6271 * a live interpreter, its variables. In a call's arguments, with nothing
6372 * typed, the variables of the parameter's type come first. Signature help
6473 * rates the overloads against the arguments typed.
74+ * <p>
75+ * If a Python language server is offered (see {@link PythonCompanion}), its
76+ * completions follow Jython's own; and its signatures and hover serve where
77+ * Jython has none.
78+ * </p>
6579 *
6680 * @author Albert Cardona
6781 * @author Curtis Rueden
@@ -80,17 +94,96 @@ public class JythonLanguageServer extends ScriptLanguageServer {
8094 private final LanguageServerService servers ;
8195 private final JythonAutoCompletions engine = new JythonAutoCompletions ();
8296
97+ /** A Python language server beside Jython's analysis, or null if none. */
98+ private final PythonCompanion python ;
99+
83100 /**
84101 * @param servers Reads the scripts' parameters.
85102 * @param environment Where the scripts run (e.g. a live interpreter).
86103 */
87104 public JythonLanguageServer (final LanguageServerService servers ,
88105 final Environment environment )
106+ {
107+ this (servers , environment , null );
108+ }
109+
110+ /**
111+ * @param servers Reads the scripts' parameters.
112+ * @param environment Where the scripts run (e.g. a live interpreter).
113+ * @param python A Python language server beside Jython's analysis, or null.
114+ */
115+ JythonLanguageServer (final LanguageServerService servers ,
116+ final Environment environment , final PythonCompanion python )
89117 {
90118 super (environment );
91119 this .servers = servers ;
120+ this .python = python ;
92121 }
93122
123+ // -- LanguageServer, with the Python server beside --
124+
125+ @ Override
126+ public void didOpen (final DidOpenTextDocumentParams params ) {
127+ super .didOpen (params );
128+ if (python != null ) python .didOpen (params );
129+ }
130+
131+ @ Override
132+ public void didChange (final DidChangeTextDocumentParams params ) {
133+ super .didChange (params );
134+ if (python != null ) python .didChange (params );
135+ }
136+
137+ @ Override
138+ public void didClose (final DidCloseTextDocumentParams params ) {
139+ super .didClose (params );
140+ if (python != null ) python .didClose (params );
141+ }
142+
143+ @ Override
144+ public CompletableFuture <Either <List <CompletionItem >, CompletionList >>
145+ completion (final CompletionParams params )
146+ {
147+ final CompletableFuture <Either <List <CompletionItem >, CompletionList >> own =
148+ super .completion (params );
149+ if (python == null ) return own ;
150+ return own .thenCompose (answer -> python .completion (params , answer
151+ .getRight ()));
152+ }
153+
154+ @ Override
155+ public CompletableFuture <CompletionItem > resolveCompletionItem (
156+ final CompletionItem item )
157+ {
158+ if (python == null ) return super .resolveCompletionItem (item );
159+ return python .resolveCompletionItem (item ,
160+ super ::resolveCompletionItem );
161+ }
162+
163+ @ Override
164+ public CompletableFuture <SignatureHelp > signatureHelp (
165+ final SignatureHelpParams params )
166+ {
167+ final CompletableFuture <SignatureHelp > own = super .signatureHelp (params );
168+ if (python == null ) return own ;
169+ return own .thenCompose (help -> python .signatureHelp (params , help ));
170+ }
171+
172+ @ Override
173+ public CompletableFuture <Hover > hover (final HoverParams params ) {
174+ final CompletableFuture <Hover > own = super .hover (params );
175+ if (python == null ) return own ;
176+ return own .thenCompose (h -> python .hover (params , h ));
177+ }
178+
179+ @ Override
180+ public CompletableFuture <Object > shutdown () {
181+ if (python != null ) python .shutdown ();
182+ return super .shutdown ();
183+ }
184+
185+ // -- ScriptLanguageServer --
186+
94187 @ Override
95188 protected CompletionList complete (final Document doc , final int caret ) {
96189 final String text = doc .text ();
0 commit comments