Skip to content

Commit ed91469

Browse files
committed
WIP: AI-generated overloaded params
TODO: Need to understand
1 parent 5813d17 commit ed91469

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.scijava.code.api.Completion;
5050
import org.scijava.code.api.Completion.TextEdit;
5151
import org.scijava.code.api.ParameterChoices;
52+
import org.scijava.code.api.TypeResolver;
5253

5354
/**
5455
* The Jython completion engine: given the code before the caret it analyzes the
@@ -89,17 +90,23 @@ public class JythonAutoCompletions {
8990
/** Python standard library module names, discovered from the jython jar. */
9091
private static final List<String> jython_jar_modules = findJythonModules();
9192

92-
/** Result of a completion query: the suggestions plus optional choices. */
93+
/**
94+
* Result of a completion query: the suggestions plus optional choices and
95+
* argument type resolution.
96+
*/
9397
public static final class Result {
9498

9599
public final List<Completion> completions;
96100
public final ParameterChoices parameterChoices;
101+
public final TypeResolver typeResolver;
97102

98103
Result(final List<Completion> completions,
99-
final ParameterChoices parameterChoices)
104+
final ParameterChoices parameterChoices,
105+
final TypeResolver typeResolver)
100106
{
101107
this.completions = completions;
102108
this.parameterChoices = parameterChoices;
109+
this.typeResolver = typeResolver;
103110
}
104111
}
105112

@@ -128,19 +135,22 @@ public Result completionsFor(final String codeWithoutLastLine,
128135
final int crop = lastLine.length() - alreadyEnteredText.length();
129136

130137
// 1) AST-based completions (names, members, constructors, invocations).
131-
final ParameterChoices choices = astCompletions(codeWithoutLastLine,
132-
lastLine, crop, predefined, completions);
138+
final Scope scope = astCompletions(codeWithoutLastLine, lastLine, crop,
139+
predefined, completions);
133140

134141
// 2) Import and class-name discovery, always appended.
135142
importCompletions(alreadyEnteredText, codeWithoutLastLine + lastLine,
136143
completions);
137144

138-
return new Result(dedupe(completions), choices);
145+
if (scope == null) return new Result(dedupe(completions), null, null);
146+
return new Result(dedupe(completions), scopeChoices(scope),
147+
expression -> JythonScriptParser.typeOf(expression, scope));
139148
}
140149

141150
// -- AST-based completions --
142151

143-
private ParameterChoices astCompletions(String codeWithoutLastLine,
152+
/** Adds AST-based completions, returning the analyzed scope (or null). */
153+
private Scope astCompletions(String codeWithoutLastLine,
144154
final String lastLine, final int crop,
145155
final Map<String, DotAutocompletions> predefined,
146156
final List<Completion> completions)
@@ -234,7 +244,7 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
234244
.kind(Completion.Kind.KEYWORD).build());
235245
}
236246
}
237-
return scopeChoices(scope);
247+
return scope;
238248
}
239249

240250
// A function/constructor invocation: "name(".
@@ -249,7 +259,7 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
249259
for (final CompletionText ct : da.get()) {
250260
completions.add(makeDotCompletion("", name, ct));
251261
}
252-
return scopeChoices(scope);
262+
return scope;
253263
}
254264
// Otherwise fall through to dot-name handling below.
255265
}
@@ -294,7 +304,7 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
294304
.toList());
295305
sortCompletions(list, seed);
296306
completions.addAll(list);
297-
return scopeChoices(scope.getLast());
307+
return scope.getLast();
298308
}
299309

300310
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public CompletionResult complete(final CompletionRequest request) {
9494
codeWithoutLastLine, lastLine, alreadyEntered, predefinedVariables(
9595
request));
9696
return new CompletionResult(result.completions, replaceStart,
97-
result.parameterChoices);
97+
result.parameterChoices, result.typeResolver);
9898
}
9999

100100
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.python.antlr.ast.Call;
4545
import org.python.antlr.ast.ClassDef;
4646
import org.python.antlr.ast.Expr;
47+
import org.python.antlr.ast.Expression;
4748
import org.python.antlr.ast.For;
4849
import org.python.antlr.ast.FunctionDef;
4950
import org.python.antlr.ast.If;
@@ -360,6 +361,26 @@ static public void parseClassDef(final ClassDef c, final Scope parent) {
360361
* @param right
361362
* @param scope
362363
*/
364+
/**
365+
* Infers the type of a single expression, such as a function argument, in
366+
* the given scope.
367+
*
368+
* @param expression The source text of the expression.
369+
* @param scope The scope in which to resolve names.
370+
* @return A class name, a primitive type name (e.g. "long" for an integer
371+
* literal), or null if unknown.
372+
*/
373+
static public String typeOf(final String expression, final Scope scope) {
374+
try {
375+
final mod m = ParserFacade.parse(expression, CompileMode.eval, "<none>", new CompilerFlags());
376+
if (!(m instanceof Expression)) return null;
377+
return parseRight(((Expression) m).getInternalBody(), scope).getClassname();
378+
} catch (final Throwable t) {
379+
// Incomplete or invalid expression.
380+
return null;
381+
}
382+
}
383+
363384
static public DotAutocompletions parseRight(final PyObject right, final Scope scope) {
364385
if (right instanceof Name) {
365386
// e.g. the name of another variable:

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +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;
5051

5152
/**
5253
* Tests that the Jython {@link JythonCodeCompleter} is discovered for the Jython
@@ -201,6 +202,34 @@ public void testInterpreterBindings() throws Exception {
201202
}
202203
}
203204

205+
@Test
206+
public void testArgumentTypes() {
207+
final Context ctx = new Context(ScriptService.class,
208+
CodeCompletionService.class);
209+
try {
210+
final ScriptLanguage jython = ctx.service(ScriptService.class)
211+
.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 +"));
227+
}
228+
finally {
229+
ctx.dispose();
230+
}
231+
}
232+
204233
// -- Helper methods --
205234

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

0 commit comments

Comments
 (0)