4747
4848import org .python .indexer .types .NModuleType ;
4949import org .scijava .code .api .ClassIndex ;
50- import org .scijava .code .api .Completion ;
51- import org .scijava .code .api .Completion .TextEdit ;
52- import org .scijava .code .api .ParameterChoices ;
50+ import org .eclipse .lsp4j .CompletionItem ;
51+ import org .eclipse .lsp4j .CompletionItemKind ;
52+ import org .eclipse .lsp4j .MarkupContent ;
53+ import org .eclipse .lsp4j .MarkupKind ;
54+ import org .eclipse .lsp4j .TextEdit ;
55+ import org .scijava .code .lsp .Callable ;
5356
5457/**
5558 * The Jython completion engine: given the code before the caret it analyzes the
56- * Jython AST (via {@link JythonScriptParser}) and produces toolkit-agnostic
57- * {@link Completion}s plus, when relevant, a {@link ParameterChoices} for
58- * parameter assistance. Class-name and import completions (including auto-import
59- * {@link TextEdit}s) are produced via {@link ClassIndex}.
59+ * Jython AST (via {@link JythonScriptParser}) and produces completions (LSP
60+ * {@link CompletionItem}s; methods and constructors as {@link Callable}s),
61+ * plus the scope's variables by type, for arguments. Class-name and import
62+ * completions (including auto-import {@link TextEdit}s) are produced via
63+ * {@link ClassIndex}.
6064 *
6165 * @author Albert Cardona
6266 * @author Curtis Rueden
@@ -91,22 +95,23 @@ public class JythonAutoCompletions {
9195 private static final List <String > jython_jar_modules = findJythonModules ();
9296
9397 /**
94- * Result of a completion query: the suggestions plus optional choices and
95- * argument type resolution.
98+ * Result of a completion query: the suggestions, plus the analyzed scope's
99+ * variables by type, and argument type resolution.
96100 */
97101 public static final class Result {
98102
99- public final List <Completion > completions ;
100- public final ParameterChoices parameterChoices ;
103+ public final List <CompletionItem > completions ;
104+ /** The variables of a type (e.g. {@code double}) in scope; or null. */
105+ public final Function <String , List <String >> variablesOfType ;
101106 /** The type (name) of an expression in the analyzed scope, or null. */
102107 public final Function <String , String > types ;
103108
104- Result (final List <Completion > completions ,
105- final ParameterChoices parameterChoices ,
109+ Result (final List <CompletionItem > completions ,
110+ final Function < String , List < String >> variablesOfType ,
106111 final Function <String , String > types )
107112 {
108113 this .completions = completions ;
109- this .parameterChoices = parameterChoices ;
114+ this .variablesOfType = variablesOfType ;
110115 this .types = types ;
111116 }
112117 }
@@ -132,7 +137,7 @@ public Result completionsFor(final String codeWithoutLastLine,
132137 final String lastLine , final String alreadyEnteredText ,
133138 final Map <String , DotAutocompletions > predefined )
134139 {
135- final List <Completion > completions = new ArrayList <>();
140+ final List <CompletionItem > completions = new ArrayList <>();
136141 final int crop = lastLine .length () - alreadyEnteredText .length ();
137142
138143 // 1) AST-based completions (names, members, constructors, invocations).
@@ -144,7 +149,7 @@ public Result completionsFor(final String codeWithoutLastLine,
144149 completions );
145150
146151 if (scope == null ) return new Result (dedupe (completions ), null , null );
147- return new Result (dedupe (completions ), scopeChoices (scope ),
152+ return new Result (dedupe (completions ), type -> variablesOfType (scope , type ),
148153 expression -> JythonScriptParser .typeOf (expression , scope ));
149154 }
150155
@@ -154,7 +159,7 @@ public Result completionsFor(final String codeWithoutLastLine,
154159 private Scope astCompletions (String codeWithoutLastLine ,
155160 final String lastLine , final int crop ,
156161 final Map <String , DotAutocompletions > predefined ,
157- final List <Completion > completions )
162+ final List <CompletionItem > completions )
158163 {
159164 // Precondition: can't expand when empty or ending with any of "[]{},; ".
160165 if (lastLine .isEmpty ()) return null ;
@@ -220,8 +225,8 @@ private Scope astCompletions(String codeWithoutLastLine,
220225 .length ());
221226 final Map <String , String > names = scope .findStartsWith2 (seed );
222227 for (final Map .Entry <String , String > e : names .entrySet ()) {
223- completions .add (Completion . builder ((head + e .getKey ()).substring (crop ))
224- . kind ( Completion . Kind . VARIABLE ). build ( ));
228+ completions .add (item ((head + e .getKey ()).substring (crop ),
229+ CompletionItemKind . Variable , null ));
225230 final String classname = e .getValue ();
226231 if (null != classname ) {
227232 // A class: also offer its constructors.
@@ -241,8 +246,8 @@ private Scope astCompletions(String codeWithoutLastLine,
241246 }
242247 for (final String keyword : KEYWORDS ) {
243248 if (keyword .startsWith (seed ) && !keyword .equals (seed )) {
244- completions .add (Completion . builder ((head + keyword ).substring (crop ))
245- . kind ( Completion . Kind . KEYWORD ). build ( ));
249+ completions .add (item ((head + keyword ).substring (crop ),
250+ CompletionItemKind . Keyword , null ));
246251 }
247252 }
248253 return scope ;
@@ -299,7 +304,7 @@ private Scope astCompletions(String codeWithoutLastLine,
299304 final String pre = fullPre .substring (0 , fullPre .lastIndexOf (seed ));
300305 final String lowerCaseSeed = seed .toLowerCase ();
301306
302- final List <Completion > list = da .get ().stream ().filter (s -> s
307+ final List <CompletionItem > list = da .get ().stream ().filter (s -> s
303308 .getReplacementText ().toLowerCase ().contains (lowerCaseSeed )).map (
304309 s -> makeDotCompletion (pre , lowerCaseSeed , s )).collect (Collectors
305310 .toList ());
@@ -312,12 +317,12 @@ private Scope astCompletions(String codeWithoutLastLine,
312317 }
313318
314319 private void moduleNameCompletions (final String first , final String pkgName ,
315- final List <Completion > completions )
320+ final List <CompletionItem > completions )
316321 {
317322 jython_jar_modules .stream ().filter (s -> s .startsWith (pkgName )).forEach (s -> //
318- completions .add (Completion . builder (first + " " + s + (first .equals ("from" )
319- ? " import " : "" )). description ( "Python standard library module" )
320- . kind ( Completion . Kind . IMPORT ). build ( )));
323+ completions .add (documented ( item (first + " " + s + (first .equals ("from" )
324+ ? " import " : "" ), CompletionItemKind . Module , null ),
325+ "Python standard library module" )));
321326 final String pkgNameFile = pkgName .replace ('.' , '/' );
322327 for (final String dir : Scope .indexer .getLoadPath ()) {
323328 try {
@@ -327,9 +332,9 @@ private void moduleNameCompletions(final String first, final String pkgName,
327332 .map (s -> (s .endsWith ("__init__.py" ) //
328333 ? s .substring (dir .length (), s .length () - 12 ) //
329334 : s .substring (dir .length (), s .length () - 3 )).replace ('/' , '.' )) //
330- .forEach (s -> completions .add (Completion . builder (first + " " + s +
331- (first .equals ("from" ) ? " import " : "" )). description (
332- "Custom python module" ). kind ( Completion . Kind . IMPORT ). build ( )));
335+ .forEach (s -> completions .add (documented ( item (first + " " + s +
336+ (first .equals ("from" ) ? " import " : "" ), CompletionItemKind . Module ,
337+ null ), "Custom python module" )));
333338 }
334339 catch (final Exception e ) {
335340 JythonDev .print ("Failed to read jython module file." , e );
@@ -338,13 +343,13 @@ private void moduleNameCompletions(final String first, final String pkgName,
338343 }
339344
340345 private void moduleMemberCompletions (final String pkgName ,
341- final String member , final List <Completion > completions )
346+ final String member , final List <CompletionItem > completions )
342347 {
343348 final NModuleType mod = Scope .loadPythonModule (pkgName );
344349 if (null != mod && !mod .getTable ().keySet ().isEmpty ()) {
345350 mod .getTable ().keySet ().stream ().filter (s -> s .startsWith (member )).forEach (
346- s -> completions .add (Completion . builder ("from " + pkgName + " import " +
347- s ). kind ( Completion . Kind . IMPORT ). build ( )));
351+ s -> completions .add (item ("from " + pkgName + " import " + s ,
352+ CompletionItemKind . Module , null )));
348353 return ;
349354 }
350355 if (null != mod ) {
@@ -358,10 +363,9 @@ private void moduleMemberCompletions(final String pkgName,
358363 if (filename .startsWith (member ) && (new File (fdir .getAbsolutePath () +
359364 "/" + filename ).isDirectory () || filename .endsWith (".py" )))
360365 {
361- completions .add (Completion .builder ("from " + pkgName + " import " +
362- (filename .endsWith (".py" ) ? filename .substring (0 , filename
363- .length () - 3 ) : filename )).kind (Completion .Kind .IMPORT )
364- .build ());
366+ completions .add (item ("from " + pkgName + " import " + (filename
367+ .endsWith (".py" ) ? filename .substring (0 , filename .length () - 3 )
368+ : filename ), CompletionItemKind .Module , null ));
365369 }
366370 }
367371 }
@@ -372,22 +376,22 @@ private void moduleMemberCompletions(final String pkgName,
372376 // -- Import / class-name discovery (with auto-import edits) --
373377
374378 private void importCompletions (final String text , final String fullCode ,
375- final List <Completion > completions )
379+ final List <CompletionItem > completions )
376380 {
377381 if (!ClassIndex .isCacheReady ()) return ; // don't block
378382
379383 final Matcher m1 = fromImport .matcher (text );
380384 if (m1 .find ()) {
381385 ClassIndex .findClassNamesContaining (m1 .group (3 )).map (
382386 formatter ::singleToImportStatement ).forEach (s -> completions .add (
383- Completion . builder ( s ). kind ( Completion . Kind . IMPORT ). build ( )));
387+ item ( s , CompletionItemKind . Module , null )));
384388 return ;
385389 }
386390 final Matcher m1f = fastImport .matcher (text );
387391 if (m1f .find ()) {
388392 ClassIndex .findClassNamesForPackage (m1f .group (2 )).map (
389393 formatter ::singleToImportStatement ).forEach (s -> completions .add (
390- Completion . builder ( s ). kind ( Completion . Kind . IMPORT ). build ( )));
394+ item ( s , CompletionItemKind . Module , null )));
391395 return ;
392396 }
393397 final Matcher m2 = importStatement .matcher (text );
@@ -407,8 +411,7 @@ private void importCompletions(final String text, final String fullCode,
407411 else stream = ClassIndex .findClassNamesForPackage (packageName );
408412 final String pre = m2 .group (1 ) + precomma ;
409413 stream .map (s -> s .substring (Math .max (0 , s .lastIndexOf ('.' ) + 1 ))).forEach (
410- s -> completions .add (Completion .builder (pre + s ).kind (
411- Completion .Kind .IMPORT ).build ()));
414+ s -> completions .add (item (pre + s , CompletionItemKind .Module , null )));
412415 return ;
413416 }
414417 final Matcher m3 = simpleClassName .matcher (text );
@@ -426,10 +429,11 @@ private void importCompletions(final String text, final String fullCode,
426429 // Side effect on accept: insert the import near the top of the file.
427430 final TextEdit edit = JythonImports .autoImportEdit (fullCode , className ,
428431 importStmt );
429- final Completion .Builder b = Completion .builder (pre + simpleName ).kind (
430- Completion .Kind .CLASS ).summary (importStmt );
431- if (edit != null ) b .additionalEdits (Collections .singletonList (edit ));
432- completions .add (b .build ());
432+ final CompletionItem c = item (pre + simpleName , CompletionItemKind .Class ,
433+ importStmt );
434+ if (edit != null ) c .setAdditionalTextEdits (Collections .singletonList (
435+ edit ));
436+ completions .add (c );
433437 }
434438 }
435439 }
@@ -440,40 +444,62 @@ private void importCompletions(final String text, final String fullCode,
440444 * Removes duplicate completions (same text and parameters), keeping the first
441445 * occurrence, since the AST and class-name analyses can suggest the same thing.
442446 */
443- private static List <Completion > dedupe (final List <Completion > completions ) {
447+ private static List <CompletionItem > dedupe (
448+ final List <CompletionItem > completions )
449+ {
444450 final java .util .Set <String > seen = new java .util .HashSet <>();
445- final List <Completion > result = new ArrayList <>(completions .size ());
446- for (final Completion c : completions ) {
447- if (seen .add (c .insertionText () + "\u0000 " + c .parameters ())) result .add (c );
451+ final List <CompletionItem > result = new ArrayList <>(completions .size ());
452+ for (final CompletionItem c : completions ) {
453+ final String params = c .getLabelDetails () == null ? null : c
454+ .getLabelDetails ().getDetail ();
455+ if (seen .add (c .getLabel () + "\u0000 " + params )) result .add (c );
448456 }
449457 return result ;
450458 }
451459
452- /** Builds a neutral completion from a {@link CompletionText} and prefix. */
453- private static Completion makeDotCompletion (final String pre ,
460+ /** A completion of the given text. */
461+ private static CompletionItem item (final String text ,
462+ final CompletionItemKind kind , final String detail )
463+ {
464+ final CompletionItem item = new CompletionItem (text );
465+ item .setKind (kind );
466+ item .setInsertText (text );
467+ item .setDetail (detail );
468+ return item ;
469+ }
470+
471+ private static CompletionItem documented (final CompletionItem item ,
472+ final String doc )
473+ {
474+ item .setDocumentation (new MarkupContent (MarkupKind .MARKDOWN , doc ));
475+ return item ;
476+ }
477+
478+ /** Builds a completion from a {@link CompletionText} and prefix. */
479+ private static CompletionItem makeDotCompletion (final String pre ,
454480 final String seed , final CompletionText ct )
455481 {
456482 final List <Parameter > ps = ct .getMethodArgs ();
457483 if (null != ps && null != ct .getReturnType ()) {
458484 String text = ct .getReplacementText ();
459485 if (text .endsWith ("()" )) text = text .substring (0 , text .length () - 2 );
460- final List <Completion . Parameter > params = new ArrayList <>();
486+ final List <Callable . Param > params = new ArrayList <>();
461487 for (final Parameter p : ps ) {
462- params .add (new Completion . Parameter (p .getName (), p .getType ()
488+ params .add (new Callable . Param (p .getName (), p .getType ()
463489 .getCanonicalName ()));
464490 }
465- return Completion .builder (pre + text ).kind (Completion .Kind .METHOD )
466- .parameters (params ).returnType (ct .getReturnType ()).summary (ct
467- .getSummary ()).build ();
491+ return new Callable (pre + text , params , ct .getReturnType ()).detail (ct
492+ .getSummary ()).toItem ();
468493 }
469- return Completion . builder (pre + ct .getReplacementText ()). summary ( ct
470- .getSummary ()). build () ;
494+ return item (pre + ct .getReplacementText (), CompletionItemKind . Field , ct
495+ .getSummary ());
471496 }
472497
473- /** A {@link ParameterChoices} that suggests in-scope variables by type. */
474- private static ParameterChoices scopeChoices (final Scope scope ) {
475- return parameter -> {
476- final String type = parameter .type ();
498+ /** The variables of the given type in scope, innermost first. */
499+ private static List <String > variablesOfType (final Scope scope ,
500+ final String type )
501+ {
502+ {
477503 if (type == null ) return Collections .emptyList ();
478504 Class <?> clazz ;
479505 switch (type ) {
@@ -494,28 +520,20 @@ private static ParameterChoices scopeChoices(final Scope scope) {
494520 }
495521 }
496522 final Class <?> c = clazz ;
497- final List <String > vars = scope .findVarsByType (type , c ).collect (Collectors
498- .toList ());
499- final List <Completion > out = new ArrayList <>(vars .size ());
500- for (int i = 0 ; i < vars .size (); i ++) {
501- // Innermost-scope variables come first; rank them higher.
502- out .add (Completion .builder (vars .get (i )).kind (Completion .Kind .VARIABLE )
503- .relevance (vars .size () - i ).build ());
504- }
505- return out ;
506- };
523+ return scope .findVarsByType (type , c ).collect (Collectors .toList ());
524+ }
507525 }
508526
509- private void sortCompletions (final List <Completion > completions ,
527+ private void sortCompletions (final List <CompletionItem > completions ,
510528 final String pre )
511529 {
512- completions .sort (new Comparator <Completion >() {
530+ completions .sort (new Comparator <CompletionItem >() {
513531
514532 @ Override
515- public int compare (final Completion o1 , final Completion o2 ) {
516- final int p1 = o1 .insertionText ().startsWith (pre ) ? 0 : Integer .MAX_VALUE ;
517- final int p2 = o2 .insertionText ().startsWith (pre ) ? 0 : Integer .MAX_VALUE ;
518- if (p1 == p2 ) return o1 .insertionText ().compareTo (o2 .insertionText ());
533+ public int compare (final CompletionItem o1 , final CompletionItem o2 ) {
534+ final int p1 = o1 .getLabel ().startsWith (pre ) ? 0 : Integer .MAX_VALUE ;
535+ final int p2 = o2 .getLabel ().startsWith (pre ) ? 0 : Integer .MAX_VALUE ;
536+ if (p1 == p2 ) return o1 .getLabel ().compareTo (o2 .getLabel ());
519537 return p1 - p2 ;
520538 }
521539 });
0 commit comments