4949import org .scijava .code .api .Completion ;
5050import org .scijava .code .api .Completion .TextEdit ;
5151import 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 ;
0 commit comments