Skip to content

Commit 5f4e417

Browse files
gselzerclaude
andcommitted
code-lsp: plain LSP answers only
Drop UpdatingCompletionList and RatedSignatureInformation, SciJava's additions to LSP: - Merged completions come once all servers have answered, as signature help and hover already did; clients that must not wait ask asynchronously. - In-process servers still order overloads best fit first (Fit moves to Signatures), but signatures no longer carry their fit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e12764 commit 5f4e417

10 files changed

Lines changed: 51 additions & 266 deletions

File tree

‎code-lsp/README.md‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ scripts.
2222
seeing the script as plain code (TransformingLanguageServer)
2323
```
2424

25-
It does five things, each in a package of its own (the first and the last
26-
share one).
25+
It does four things, each in a package of its own.
2726

2827
## 1. Brokering between scripts and servers (`org.scijava.code.lsp`)
2928

@@ -72,22 +71,14 @@ call as a completion snippet; `Signatures`, rating overloads against the
7271
arguments typed; and `ClassIndex`, the classes on the classpath, with their
7372
javadoc.
7473

75-
## 5. Additions to LSP (`org.scijava.code.lsp`)
76-
77-
For in-process use, where both sides are SciJava's (over JSON-RPC, they
78-
degrade to plain LSP): `UpdatingCompletionList`, completions answered at once
79-
with better ones to follow (e.g. a slower server's); and
80-
`RatedSignatureInformation`, a signature rated by how well the arguments
81-
typed so far fit it.
82-
8374
## Who uses what
8475

85-
| | 1 | 2 | 3 | 4 | 5 |
86-
|-------------------------|---|---|---|---|---|
87-
| script-editor | ✓ | | | | ✓ |
88-
| scripting-groovy | ✓ | | ✓ | ✓ | ✓ |
89-
| scripting-jython | ✓ | ✓ | ✓ | ✓ | ✓ |
90-
| scripting-appose-python | ✓ | ✓ | ✓ | | |
76+
| | 1 | 2 | 3 | 4 |
77+
|-------------------------|---|---|---|---|
78+
| script-editor | ✓ | | | |
79+
| scripting-groovy | ✓ | | ✓ | ✓ |
80+
| scripting-jython | ✓ | ✓ | ✓ | ✓ |
81+
| scripting-appose-python | ✓ | ✓ | ✓ | |
9182

9283
The specialized packages depend only on `org.scijava.code.lsp`, never on each
9384
other. The module exports all four; its implementation

‎code-lsp/src/main/java/org/scijava/code/lsp/RatedSignatureInformation.java‎

Lines changed: 0 additions & 81 deletions
This file was deleted.

‎code-lsp/src/main/java/org/scijava/code/lsp/ScriptSession.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,7 @@ else if (!newText.equals(text)) {
216216

217217
// -- Requests: about the given text (sent first, if new), at an offset --
218218

219-
/**
220-
* Completions at an offset. If some servers are slower, the list is an
221-
* {@link UpdatingCompletionList}, whose update brings theirs.
222-
*/
219+
/** Completions at an offset: all servers', once all have answered. */
223220
public synchronized CompletableFuture<CompletionList> completion(
224221
final String script, final int offset)
225222
{

‎code-lsp/src/main/java/org/scijava/code/lsp/UpdatingCompletionList.java‎

Lines changed: 0 additions & 69 deletions
This file was deleted.

‎code-lsp/src/main/java/org/scijava/code/lsp/impl/MergedLanguageServer.java‎

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Set;
4040
import java.util.concurrent.CompletableFuture;
4141
import java.util.concurrent.ConcurrentHashMap;
42-
import java.util.concurrent.TimeUnit;
4342
import java.util.function.Consumer;
4443
import java.util.function.Function;
4544

@@ -81,7 +80,6 @@
8180
import org.scijava.code.lsp.Environment;
8281
import org.scijava.code.lsp.LanguageServerPlugin;
8382
import org.scijava.code.lsp.Positions;
84-
import org.scijava.code.lsp.UpdatingCompletionList;
8583
import org.scijava.script.ScriptLanguage;
8684

8785

@@ -92,8 +90,7 @@
9290
* their answers, in the plugins' priority order.
9391
* <ul>
9492
* <li>Completions: all servers', in order, without duplicates (the same text
95-
* after inserting). Those not answered within a moment follow as an
96-
* {@link UpdatingCompletionList#update() update}.</li>
93+
* after inserting).</li>
9794
* <li>Signature help and hover: the first server's that has some.</li>
9895
* <li>Diagnostics: all servers', published together to this server's
9996
* client.</li>
@@ -110,9 +107,6 @@ public class MergedLanguageServer implements LanguageServer,
110107
TextDocumentService, WorkspaceService, LanguageClientAware
111108
{
112109

113-
/** How long to wait for all servers' completions, in milliseconds. */
114-
private static final long BUDGET = Long.getLong("scijava.lsp.budget", 25);
115-
116110
private final DefaultLanguageServerService service;
117111
private final ScriptLanguage language;
118112
private final List<LanguageServerPlugin> plugins;
@@ -284,33 +278,14 @@ public void didSave(final DidSaveTextDocumentParams params) {
284278
final String uri = doc.item.getUri();
285279
final String text = doc.item.getText();
286280
final int caret = Positions.offset(text, params.getPosition());
287-
final List<CompletableFuture<CompletionList>> first = new ArrayList<>();
288-
final List<CompletableFuture<CompletionList>> last = new ArrayList<>();
281+
final List<CompletableFuture<CompletionList>> answers = new ArrayList<>();
289282
for (final Member m : doc.members) {
290-
final CompletableFuture<CompletionList> f = ask(m, s -> s.completion(
291-
params)).thenApply(MergedLanguageServer::list);
292-
first.add(f);
293-
last.add(f.thenCompose(MergedLanguageServer::settled));
283+
answers.add(ask(m, s -> s.completion(params)).thenApply(
284+
MergedLanguageServer::list));
294285
}
295-
final CompletableFuture<Void> all = CompletableFuture.allOf(first.toArray(
296-
new CompletableFuture[0]));
297-
return all.completeOnTimeout(null, BUDGET, TimeUnit.MILLISECONDS)
298-
.thenApply(x -> {
299-
final boolean settled = last.stream().allMatch(
300-
CompletableFuture::isDone);
301-
final List<CompletionList> now = new ArrayList<>();
302-
for (int i = 0; i < first.size(); i++) {
303-
now.add(last.get(i).isDone() ? last.get(i).getNow(null) : first.get(
304-
i).getNow(null));
305-
}
306-
final CompletionList merged = merge(now, uri, text, caret);
307-
if (settled) return merged;
308-
final CompletableFuture<CompletionList> update = CompletableFuture
309-
.allOf(last.toArray(new CompletableFuture[0])).thenApply(
310-
y -> merge(joinAll(last), uri, text, caret));
311-
return (CompletionList) new UpdatingCompletionList(merged.getItems(),
312-
update);
313-
}).thenApply(Either::forRight);
286+
return CompletableFuture.allOf(answers.toArray(new CompletableFuture[0]))
287+
.thenApply(x -> Either.forRight(merge(joinAll(answers), uri, text,
288+
caret)));
314289
}
315290

316291
@Override
@@ -505,19 +480,6 @@ private static CompletionList list(
505480
.getLeft());
506481
}
507482

508-
/** The final completions of a server: its update's, if announced. */
509-
private static CompletableFuture<CompletionList> settled(
510-
final CompletionList list)
511-
{
512-
if (!(list instanceof UpdatingCompletionList)) {
513-
return CompletableFuture.completedFuture(list);
514-
}
515-
final CompletableFuture<CompletionList> update =
516-
((UpdatingCompletionList) list).update();
517-
if (update == null) return CompletableFuture.completedFuture(list);
518-
return update.handle((better, error) -> better != null ? better : list);
519-
}
520-
521483
private static List<CompletionList> joinAll(
522484
final List<CompletableFuture<CompletionList>> lists)
523485
{
@@ -543,8 +505,7 @@ private static CompletionList merge(final List<CompletionList> lists,
543505
for (int i = 0; i < lists.size(); i++) {
544506
final CompletionList list = lists.get(i);
545507
if (list == null || list.getItems() == null) continue;
546-
incomplete |= list.isIncomplete() &&
547-
!(list instanceof UpdatingCompletionList);
508+
incomplete |= list.isIncomplete();
548509
final List<CompletionItem> items = new ArrayList<>(list.getItems());
549510
items.sort(Comparator.comparing(c -> c.getSortText() != null ? c
550511
.getSortText() : c.getLabel()));

‎code-lsp/src/main/java/org/scijava/code/lsp/jvm/Callable.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import org.eclipse.lsp4j.InsertTextFormat;
4141
import org.eclipse.lsp4j.ParameterInformation;
4242
import org.eclipse.lsp4j.SignatureInformation;
43-
import org.scijava.code.lsp.RatedSignatureInformation.Fit;
44-
import org.scijava.code.lsp.RatedSignatureInformation;
4543

4644
/**
4745
* Something to call (a method, function or constructor), as in-process
@@ -191,14 +189,16 @@ public CompletionItem toItem() {
191189
return item;
192190
}
193191

194-
/** As a signature, rated. */
195-
public SignatureInformation toSignature(final Fit fit) {
192+
/** As a signature. */
193+
public SignatureInformation toSignature() {
196194
final List<ParameterInformation> infos = new ArrayList<>();
197195
for (final Param p : params) infos.add(new ParameterInformation(p
198196
.toString()));
199197
final String label = name + parameterList() + (returnType == null ? ""
200198
: " -> " + returnType);
201-
return new RatedSignatureInformation(label, infos, fit);
199+
final SignatureInformation s = new SignatureInformation(label);
200+
s.setParameters(infos);
201+
return s;
202202
}
203203

204204
/** E.g. {@code (double a, double b)}. */

‎code-lsp/src/main/java/org/scijava/code/lsp/jvm/Signatures.java‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.util.function.BiFunction;
4141

4242
import org.eclipse.lsp4j.SignatureInformation;
43-
import org.scijava.code.lsp.RatedSignatureInformation.Fit;
44-
import org.scijava.code.lsp.RatedSignatureInformation;
4543

4644
/**
4745
* Helpers for rating signatures against arguments, for language servers whose
@@ -52,6 +50,18 @@
5250
*/
5351
public final class Signatures {
5452

53+
/** How well arguments fit a signature. */
54+
public enum Fit {
55+
/** The arguments fit as they are. */
56+
MATCH,
57+
/** The arguments fit after a conversion (e.g. int to double). */
58+
CONVERSION,
59+
/** Not known (e.g. the arguments' types are unknown). */
60+
UNKNOWN,
61+
/** Some argument cannot fit. */
62+
MISMATCH
63+
}
64+
5565
private static final Map<String, Class<?>> PRIMITIVES = new HashMap<>();
5666
static {
5767
for (final Class<?> c : new Class<?>[] { boolean.class, byte.class,
@@ -130,20 +140,20 @@ public static Fit rate(final Callable callable, final List<String> argTypes,
130140
}
131141

132142
/**
133-
* Rates each callable against the arguments, and orders them for display:
134-
* matches first, then those needing conversions, unrated ones, and
135-
* mismatches last; otherwise keeping their order.
143+
* Rates each callable against the arguments, and orders their signatures
144+
* for display: matches first, then those needing conversions, unrated ones,
145+
* and mismatches last; otherwise keeping their order.
136146
*/
137147
public static List<SignatureInformation> rateAll(
138148
final List<Callable> callables, final List<String> argTypes,
139149
final BiFunction<String, String, Fit> fit)
140150
{
151+
final List<Callable> sorted = new ArrayList<>(callables);
152+
final Map<Callable, Integer> ranks = new HashMap<>();
153+
for (final Callable c : sorted) ranks.put(c, rank(rate(c, argTypes, fit)));
154+
sorted.sort(Comparator.comparingInt(ranks::get));
141155
final List<SignatureInformation> out = new ArrayList<>();
142-
for (final Callable c : callables) {
143-
out.add(c.toSignature(rate(c, argTypes, fit)));
144-
}
145-
out.sort(Comparator.comparingInt(s -> rank(RatedSignatureInformation
146-
.fitOf(s))));
156+
for (final Callable c : sorted) out.add(c.toSignature());
147157
return out;
148158
}
149159

0 commit comments

Comments
 (0)