Skip to content

Commit b57ca4a

Browse files
gselzerclaude
andcommitted
code-api: signature help, lazy docs, result updates
* Add CodeCompleter#signatureHelp and SignatureHelp: the signatures of the call around the caret, rated against the arguments typed so far (match, conversion, mismatch, unknown), in the completer's order. * Add Signatures: shared rating rules and call/argument helpers for completers whose types are Java classes. * Remove TypeResolver, which signature help supersedes. * Add Completion.Builder#lazyDescription: descriptions computed only when first requested. * Add CompletionResult#withUpdate/#update: results announcing a better result to follow, so completers need not block the UI. * Add CodeCompleter#prepare: a hook to warm up for a script. * Add CompletionRequest#path: the edited script's file path. * Skip #@script directives when parsing script parameters, which otherwise warned on every keystroke. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b3c50ef commit b57ca4a

11 files changed

Lines changed: 763 additions & 72 deletions

File tree

‎code-api/src/main/java/org/scijava/code/api/CodeCompleter.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,34 @@ public interface CodeCompleter {
6060
* @return the suggestions, never {@code null} (use {@link CompletionResult#EMPTY})
6161
*/
6262
CompletionResult complete(CompletionRequest request);
63+
64+
/**
65+
* Gives the completer a chance to get ready for completing in the given
66+
* script, e.g. when it is opened in an editor, so that the first completion
67+
* is fast. Implementations must return quickly, doing any slow work in the
68+
* background. The default does nothing.
69+
*
70+
* @param request the script, as for {@link #complete}
71+
*/
72+
default void prepare(final CompletionRequest request) {
73+
// NB: Nothing to prepare.
74+
}
75+
76+
/**
77+
* Gets the signatures of the call around the request's caret (e.g. all
78+
* overloads of {@code max} in {@code Math.max(1.5, |)}), rated against the
79+
* arguments typed so far, and ordered as the completer sees fit (e.g. best
80+
* fit first). The default offers none.
81+
* <p>
82+
* Like {@link #complete}, this is called as the user types, so it should
83+
* answer quickly, announcing slower answers via
84+
* {@link SignatureHelp#withUpdate}.
85+
* </p>
86+
*
87+
* @param request the source text and caret, as for {@link #complete}
88+
* @return the signatures, never {@code null} (use {@link SignatureHelp#NONE})
89+
*/
90+
default SignatureHelp signatureHelp(final CompletionRequest request) {
91+
return SignatureHelp.NONE;
92+
}
6393
}

‎code-api/src/main/java/org/scijava/code/api/Completion.java‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.ArrayList;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.function.Supplier;
3536

3637
/**
3738
* A single code completion suggestion.
@@ -133,7 +134,8 @@ public String toString() {
133134
private final String insertionText;
134135
private final String displayText;
135136
private final String summary;
136-
private final String description;
137+
private volatile String description;
138+
private volatile Supplier<String> lazyDescription;
137139
private final Kind kind;
138140
private final double relevance;
139141
private final List<Parameter> parameters;
@@ -147,6 +149,7 @@ private Completion(final Builder b) {
147149
this.displayText = b.displayText == null ? b.insertionText : b.displayText;
148150
this.summary = b.summary;
149151
this.description = b.description;
152+
this.lazyDescription = b.description == null ? b.lazyDescription : null;
150153
this.kind = b.kind;
151154
this.relevance = b.relevance;
152155
this.parameters = b.parameters == null ? Collections.emptyList()
@@ -173,8 +176,24 @@ public String summary() {
173176
return summary;
174177
}
175178

176-
/** A longer description, which may be HTML, or {@code null}. */
179+
/**
180+
* A longer description, which may be HTML, or {@code null}.
181+
* <p>
182+
* It may be computed only now, on first request (see
183+
* {@link Builder#lazyDescription}), so front ends should ask for it only
184+
* when they are about to show it, e.g. when the completion is selected.
185+
* </p>
186+
*/
177187
public String description() {
188+
final Supplier<String> lazy = lazyDescription;
189+
if (lazy != null) {
190+
synchronized (this) {
191+
if (lazyDescription != null) {
192+
description = lazyDescription.get();
193+
lazyDescription = null;
194+
}
195+
}
196+
}
178197
return description;
179198
}
180199

@@ -242,6 +261,7 @@ public static final class Builder {
242261
private String displayText;
243262
private String summary;
244263
private String description;
264+
private Supplier<String> lazyDescription;
245265
private Kind kind = Kind.OTHER;
246266
private double relevance = 0;
247267
private List<Parameter> parameters;
@@ -269,6 +289,16 @@ public Builder description(final String v) {
269289
return this;
270290
}
271291

292+
/**
293+
* Sets a description to compute only when first requested, e.g. because
294+
* it is expensive to obtain, and few completions are ever looked at.
295+
* The supplier is called at most once; it may return {@code null}.
296+
*/
297+
public Builder lazyDescription(final Supplier<String> v) {
298+
this.lazyDescription = v;
299+
return this;
300+
}
301+
272302
public Builder kind(final Kind v) {
273303
this.kind = v;
274304
return this;

‎code-api/src/main/java/org/scijava/code/api/CompletionRequest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,22 @@ public final class CompletionRequest {
5555
private final ScriptLanguage language;
5656
private final ScriptEngine engine;
5757
private final ScriptContext context;
58+
private final String path;
5859

5960
public CompletionRequest(final String text, final int offset,
6061
final ScriptLanguage language, final ScriptEngine engine,
6162
final ScriptContext context)
63+
{
64+
this(text, offset, language, engine, context, null);
65+
}
66+
67+
/**
68+
* As {@link #CompletionRequest(String, int, ScriptLanguage, ScriptEngine,
69+
* ScriptContext)}, for a script saved at the given path.
70+
*/
71+
public CompletionRequest(final String text, final int offset,
72+
final ScriptLanguage language, final ScriptEngine engine,
73+
final ScriptContext context, final String path)
6274
{
6375
if (offset < 0 || offset > text.length()) {
6476
throw new IndexOutOfBoundsException("offset " + offset + " out of [0, " +
@@ -69,6 +81,7 @@ public CompletionRequest(final String text, final int offset,
6981
this.language = language;
7082
this.engine = engine;
7183
this.context = context;
84+
this.path = path;
7285
}
7386

7487
/** Creates a request over the whole text with the caret at its end. */
@@ -104,6 +117,15 @@ public ScriptContext context() {
104117
return context;
105118
}
106119

120+
/**
121+
* The file system path of the script being edited, or {@code null} if it is
122+
* unsaved or unknown (e.g. in an interactive interpreter). Completers may
123+
* use it to resolve files relative to the script.
124+
*/
125+
public String path() {
126+
return path;
127+
}
128+
107129
/** The source text from the start of the document up to the caret. */
108130
public String textToCaret() {
109131
return text.substring(0, offset);

‎code-api/src/main/java/org/scijava/code/api/CompletionResult.java‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.util.Collections;
3333
import java.util.List;
34+
import java.util.concurrent.CompletableFuture;
3435

3536
/**
3637
* The result of a {@link CodeCompleter} query: the list of suggestions plus the
@@ -54,7 +55,7 @@ public final class CompletionResult {
5455
private final List<Completion> completions;
5556
private final int replaceStart;
5657
private final ParameterChoices parameterChoices;
57-
private final TypeResolver typeResolver;
58+
private final CompletableFuture<CompletionResult> update;
5859

5960
public CompletionResult(final List<Completion> completions,
6061
final int replaceStart)
@@ -68,14 +69,39 @@ public CompletionResult(final List<Completion> completions,
6869
this(completions, replaceStart, parameterChoices, null);
6970
}
7071

71-
public CompletionResult(final List<Completion> completions,
72+
private CompletionResult(final List<Completion> completions,
7273
final int replaceStart, final ParameterChoices parameterChoices,
73-
final TypeResolver typeResolver)
74+
final CompletableFuture<CompletionResult> update)
7475
{
7576
this.completions = Collections.unmodifiableList(completions);
7677
this.replaceStart = replaceStart;
7778
this.parameterChoices = parameterChoices;
78-
this.typeResolver = typeResolver;
79+
this.update = update;
80+
}
81+
82+
/**
83+
* Gets a copy of this result which announces a better result for the same
84+
* request, to follow: e.g. this result is what a completer knows at once,
85+
* and the update adds what a slow analysis finds.
86+
*
87+
* @param future Completes with the better result (or with null, if there
88+
* is none after all).
89+
*/
90+
public CompletionResult withUpdate(
91+
final CompletableFuture<CompletionResult> future)
92+
{
93+
return new CompletionResult(completions, replaceStart, parameterChoices,
94+
future);
95+
}
96+
97+
/**
98+
* A better result for the same request, still to come, or {@code null} if
99+
* this result is final. Front ends that can refresh their display should do
100+
* so when it completes (with a non-null result), provided the document and
101+
* caret have not changed since the request; others can ignore it.
102+
*/
103+
public CompletableFuture<CompletionResult> update() {
104+
return update;
79105
}
80106

81107
/** The suggested completions, ordered by descending relevance. */
@@ -96,14 +122,6 @@ public ParameterChoices parameterChoices() {
96122
return parameterChoices;
97123
}
98124

99-
/**
100-
* A resolver for the types of expressions in this result's scope (e.g. the
101-
* arguments of a callable completion), or {@code null} if unsupported.
102-
*/
103-
public TypeResolver typeResolver() {
104-
return typeResolver;
105-
}
106-
107125
/** True iff there are no suggestions. */
108126
public boolean isEmpty() {
109127
return completions.isEmpty();

‎code-api/src/main/java/org/scijava/code/api/DefaultCodeCompletionService.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ private static String parameterLines(final String text) {
101101
final StringBuilder sb = new StringBuilder();
102102
boolean header = true;
103103
for (final String line : text.split("\r?\n")) {
104-
if (line.startsWith("#@")) sb.append(line).append("\n");
104+
if (line.startsWith("#@")) {
105+
// NB: #@script(...) is a directive, which ScriptDirectiveScriptProcessor
106+
// handles when the script runs; ParameterScriptProcessor would warn.
107+
if (!line.matches("^#@script(?!\\w).*")) sb.append(line).append("\n");
108+
}
105109
else if (header) {
106110
if (line.matches("^[^\\w]*[^\\w#]@.*")) {
107111
sb.append(line).append("\n");

0 commit comments

Comments
 (0)