Skip to content

Commit d8fbc6f

Browse files
gselzerclaude
andcommitted
Provide signature help
Find the call around the caret, take the callee's overloads from the completion engine (methods before the "(", constructors after it), type the arguments with the engine's own inference, and rate the overloads with code-api's Signatures. This replaces the TypeResolver, which code-api no longer has. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ed91469 commit d8fbc6f

3 files changed

Lines changed: 113 additions & 22 deletions

File tree

‎src/main/java/org/scijava/plugins/scripting/jython/JythonAutoCompletions.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
import java.util.jar.JarFile;
4343
import java.util.regex.Matcher;
4444
import java.util.regex.Pattern;
45+
import java.util.function.Function;
4546
import java.util.stream.Collectors;
4647

4748
import org.python.indexer.types.NModuleType;
4849
import org.scijava.code.api.ClassIndex;
4950
import org.scijava.code.api.Completion;
5051
import org.scijava.code.api.Completion.TextEdit;
5152
import org.scijava.code.api.ParameterChoices;
52-
import org.scijava.code.api.TypeResolver;
5353

5454
/**
5555
* The Jython completion engine: given the code before the caret it analyzes the
@@ -98,15 +98,16 @@ public static final class Result {
9898

9999
public final List<Completion> completions;
100100
public final ParameterChoices parameterChoices;
101-
public final TypeResolver typeResolver;
101+
/** The type (name) of an expression in the analyzed scope, or null. */
102+
public final Function<String, String> types;
102103

103104
Result(final List<Completion> completions,
104105
final ParameterChoices parameterChoices,
105-
final TypeResolver typeResolver)
106+
final Function<String, String> types)
106107
{
107108
this.completions = completions;
108109
this.parameterChoices = parameterChoices;
109-
this.typeResolver = typeResolver;
110+
this.types = types;
110111
}
111112
}
112113

‎src/main/java/org/scijava/plugins/scripting/jython/JythonCodeCompleter.java‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
*/
2929
package org.scijava.plugins.scripting.jython;
3030

31+
import java.util.ArrayList;
3132
import java.util.Collections;
33+
import java.util.Comparator;
34+
import java.util.List;
3235
import java.util.LinkedHashMap;
3336
import java.util.Map;
37+
import java.util.stream.Collectors;
3438

3539
import javax.script.Bindings;
3640
import javax.script.ScriptContext;
@@ -48,7 +52,10 @@
4852
import org.scijava.code.api.CodeCompleterPlugin;
4953
import org.scijava.code.api.CodeCompletionService;
5054
import org.scijava.code.api.CompletionRequest;
55+
import org.scijava.code.api.Completion;
5156
import org.scijava.code.api.CompletionResult;
57+
import org.scijava.code.api.SignatureHelp;
58+
import org.scijava.code.api.Signatures;
5259

5360
/**
5461
* Toolkit-agnostic code completion for the Jython language, contributed by the
@@ -94,7 +101,73 @@ public CompletionResult complete(final CompletionRequest request) {
94101
codeWithoutLastLine, lastLine, alreadyEntered, predefinedVariables(
95102
request));
96103
return new CompletionResult(result.completions, replaceStart,
97-
result.parameterChoices, result.typeResolver);
104+
result.parameterChoices);
105+
}
106+
107+
@Override
108+
public SignatureHelp signatureHelp(final CompletionRequest request) {
109+
final String text = request.text();
110+
final int caret = request.offset();
111+
if (request.lineToCaret().trim().startsWith("#")) return SignatureHelp.NONE;
112+
final int open = Signatures.callStart(text, caret);
113+
if (open < 0) return SignatureHelp.NONE;
114+
final List<String> args = Signatures.arguments(text, open, caret);
115+
if (args == null) return SignatureHelp.NONE;
116+
117+
// The callee, e.g. "Math.max" or "ArrayList".
118+
final String callee = alreadyEnteredText(text.substring(text.lastIndexOf(
119+
'\n', open - 1) + 1, open)).trim();
120+
if (callee.isEmpty()) return SignatureHelp.NONE;
121+
122+
// Its signatures: what completion offers for it, as if the caret were
123+
// just before the "(" (methods: "Math.max"), or just after it
124+
// (constructors: offered by their class's simple name, "ArrayList").
125+
final String simpleName = callee.substring(callee.lastIndexOf('.') + 1);
126+
JythonAutoCompletions.Result result = null;
127+
List<Completion> callables = Collections.emptyList();
128+
for (final int at : new int[] { open, open + 1 }) {
129+
final String name = at == open ? callee : simpleName;
130+
result = completionsAt(request, at);
131+
callables = result.completions.stream().filter(c -> c.isCallable() &&
132+
stripParens(c.insertionText()).equals(name)).collect(Collectors
133+
.toList());
134+
if (!callables.isEmpty()) break;
135+
}
136+
if (callables.isEmpty()) return SignatureHelp.NONE;
137+
138+
// The types of the arguments before the caret's (which is being typed).
139+
final List<String> argTypes = new ArrayList<>();
140+
for (int i = 0; i < args.size(); i++) {
141+
final String arg = args.get(i).trim();
142+
argTypes.add(i == args.size() - 1 || arg.isEmpty() ? null : result.types
143+
.apply(arg));
144+
}
145+
// NB: Reflection lists methods in no particular order: sort them by
146+
// their parameter types, so that equally good fits come out the same.
147+
final List<Completion> sorted = new ArrayList<>(callables);
148+
sorted.sort(Comparator.comparing(c -> c.parameters().stream().map(
149+
p -> String.valueOf(p.type())).collect(Collectors.joining(","))));
150+
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
151+
return new SignatureHelp(Signatures.rateAll(sorted, argTypes, (a,
152+
p) -> Signatures.fit(a, p, loader)), args.size() - 1, open);
153+
}
154+
155+
/** Computes completions as if the caret were at the given offset. */
156+
private JythonAutoCompletions.Result completionsAt(
157+
final CompletionRequest request, final int offset)
158+
{
159+
final CompletionRequest there = new CompletionRequest(request.text(),
160+
offset, request.language(), request.engine(), request.context(), request
161+
.path());
162+
final String textToCaret = there.textToCaret();
163+
final String lastLine = there.lineToCaret();
164+
return engine.completionsFor(textToCaret.substring(0, textToCaret
165+
.length() - lastLine.length()), lastLine, alreadyEnteredText(lastLine),
166+
predefinedVariables(request));
167+
}
168+
169+
private static String stripParens(final String name) {
170+
return name.endsWith("()") ? name.substring(0, name.length() - 2) : name;
98171
}
99172

100173
/**

‎src/test/java/org/scijava/plugins/scripting/jython/JythonCompletionTest.java‎

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import org.scijava.code.api.Completion;
4848
import org.scijava.code.api.CompletionRequest;
4949
import org.scijava.code.api.CompletionResult;
50-
import org.scijava.code.api.TypeResolver;
50+
import org.scijava.code.api.SignatureHelp;
5151

5252
/**
5353
* Tests that the Jython {@link JythonCodeCompleter} is discovered for the Jython
@@ -203,33 +203,50 @@ public void testInterpreterBindings() throws Exception {
203203
}
204204

205205
@Test
206-
public void testArgumentTypes() {
206+
public void testSignatureHelp() {
207+
// A Python float fits the floating-point overloads, which come first.
208+
final SignatureHelp max = help("from java.lang import Math\n" +
209+
"Math.max(1.5, ");
210+
assertEquals(1, max.activeParameter());
211+
assertEquals("[double:MATCH, float:MATCH, int:MISMATCH, long:MISMATCH]",
212+
describe(max));
213+
// A Python int fits the integral ones, and converts to the others.
214+
assertEquals("[int:MATCH, long:MATCH, double:CONVERSION, " +
215+
"float:CONVERSION]", describe(help("from java.lang import Math\n" +
216+
"Math.max(1, 2")));
217+
// Arguments' types come from the analysis, e.g. of variables.
218+
final SignatureHelp indexOf = help("s = 'abc'\nc = 'b'\n" +
219+
"s.indexOf(c, ");
220+
assertTrue(describe(indexOf), describe(indexOf).startsWith(
221+
"[java.lang.String:MATCH"));
222+
// Constructors.
223+
assertTrue(describe(help("from java.util import ArrayList\n" +
224+
"ArrayList(")).contains("java.util.Collection"));
225+
// None outside calls.
226+
assertTrue(help("x = 1").isEmpty());
227+
}
228+
229+
private static SignatureHelp help(final String code) {
207230
final Context ctx = new Context(ScriptService.class,
208231
CodeCompletionService.class);
209232
try {
210233
final ScriptLanguage jython = ctx.service(ScriptService.class)
211234
.getLanguageByName("Jython");
212-
final String code = "#@ String name\n" + //
213-
"from java.util import ArrayList\n" + //
214-
"x = 1.5\n" + //
215-
"lst = ArrayList()\n" + //
216-
"name.";
217-
final TypeResolver types = ctx.service(CodeCompletionService.class)
218-
.complete(new CompletionRequest(code, jython, null)).typeResolver();
219-
assertNotNull(types);
220-
assertEquals("long", types.typeOf("1"));
221-
assertEquals("double", types.typeOf("x"));
222-
assertEquals("java.lang.String", types.typeOf("name"));
223-
assertEquals("java.lang.String", types.typeOf("'hi'"));
224-
assertEquals("java.util.ArrayList", types.typeOf("lst"));
225-
assertEquals(null, types.typeOf("undefinedThing"));
226-
assertEquals(null, types.typeOf("1 +"));
235+
return ctx.service(CodeCompletionService.class).getCompleter(jython)
236+
.signatureHelp(new CompletionRequest(code, jython, null));
227237
}
228238
finally {
229239
ctx.dispose();
230240
}
231241
}
232242

243+
/** Each signature's first parameter type and fit, in order. */
244+
private static String describe(final SignatureHelp help) {
245+
return help.signatures().stream().map(s -> (s.callable().parameters()
246+
.isEmpty() ? "()" : s.callable().parameters().get(0).type()) + ":" + s
247+
.fit()).collect(Collectors.toList()).toString();
248+
}
249+
233250
// -- Helper methods --
234251

235252
private static List<String> complete(final CodeCompletionService service,

0 commit comments

Comments
 (0)