3333import java .nio .file .FileVisitOption ;
3434import java .nio .file .Files ;
3535import java .util .ArrayList ;
36+ import java .util .Arrays ;
3637import java .util .Collections ;
3738import java .util .Comparator ;
3839import java .util .List ;
4445import java .util .stream .Collectors ;
4546
4647import org .python .indexer .types .NModuleType ;
47- import org .scijava .script . complete .ClassIndex ;
48- import org .scijava .script . complete .Completion ;
49- import org .scijava .script . complete .Completion .TextEdit ;
50- import org .scijava .script . complete .ParameterChoices ;
48+ import org .scijava .code . api .ClassIndex ;
49+ import org .scijava .code . api .Completion ;
50+ import org .scijava .code . api .Completion .TextEdit ;
51+ import org .scijava .code . api .ParameterChoices ;
5152
5253/**
5354 * The Jython completion engine: given the code before the caret it analyzes the
6263public class JythonAutoCompletions {
6364
6465 private static final Pattern
65- nameToken = Pattern .compile ("^(.*?[ \\ t,\\ [=\\ (]+)([a-zA-Z_][a-zA-Z0-9_]+ )$" ),
66+ nameToken = Pattern .compile ("^(| .*?[ \\ t,\\ [=\\ (+ \\ -*/%<>!: ]+)([a-zA-Z_][a-zA-Z0-9_]* )$" ),
6667 invocation = Pattern .compile ("^(.*?[ \\ t]+|)([a-zA-Z_][a-zA-Z0-9_]+\\ ()$" ),
6768 dotNameToken = Pattern .compile ("^(.*?[ \\ t]+|)([a-zA-Z0-9_\\ .\\ [\\ ](){}]+)\\ .([a-zA-Z0-9_]*)$" ),
6869 assign = Pattern .compile ("^([ \\ t]*)(([a-zA-Z_][a-zA-Z0-9_ \\ t,]*)[ \\ t]+=[ \\ t]+(.*))$" ),
@@ -76,6 +77,13 @@ public class JythonAutoCompletions {
7677 importStatement = Pattern .compile ("^((from[ \\ t]+([a-zA-Z0-9._]+)[ \\ t]+|[ \\ t]*)import[ \\ t]+)([a-zA-Z0-9_., \\ t]*)$" ),
7778 simpleClassName = Pattern .compile ("^(.*[ \\ t]+|)([A-Z_][a-zA-Z0-9_]+)$" );
7879
80+ /** Python 2 (Jython) keywords. */
81+ private static final List <String > KEYWORDS = Arrays .asList ("and" , "as" ,
82+ "assert" , "break" , "class" , "continue" , "def" , "del" , "elif" , "else" ,
83+ "except" , "exec" , "finally" , "for" , "from" , "global" , "if" , "import" , "in" ,
84+ "is" , "lambda" , "not" , "or" , "pass" , "print" , "raise" , "return" , "try" ,
85+ "while" , "with" , "yield" , "None" , "True" , "False" );
86+
7987 private final JythonImportFormat formatter = new JythonImportFormat ();
8088
8189 /** Python standard library module names, discovered from the jython jar. */
@@ -100,27 +108,42 @@ public static final class Result {
100108 * which follows {@code codeWithoutLastLine} in the script. {@code
101109 * alreadyEnteredText} is the text the editor will replace.
102110 */
103- public Result completionsFor (String codeWithoutLastLine , final String lastLine ,
104- final String alreadyEnteredText )
111+ public Result completionsFor (final String codeWithoutLastLine ,
112+ final String lastLine , final String alreadyEnteredText )
113+ {
114+ return completionsFor (codeWithoutLastLine , lastLine , alreadyEnteredText ,
115+ Collections .emptyMap ());
116+ }
117+
118+ /**
119+ * As {@link #completionsFor(String, String, String)}, for a script in which
120+ * the given variables are already defined before the code runs (e.g. script
121+ * parameters, or the bindings of a live interpreter).
122+ */
123+ public Result completionsFor (final String codeWithoutLastLine ,
124+ final String lastLine , final String alreadyEnteredText ,
125+ final Map <String , DotAutocompletions > predefined )
105126 {
106127 final List <Completion > completions = new ArrayList <>();
107128 final int crop = lastLine .length () - alreadyEnteredText .length ();
108129
109130 // 1) AST-based completions (names, members, constructors, invocations).
110131 final ParameterChoices choices = astCompletions (codeWithoutLastLine ,
111- lastLine , crop , completions );
132+ lastLine , crop , predefined , completions );
112133
113134 // 2) Import and class-name discovery, always appended.
114135 importCompletions (alreadyEnteredText , codeWithoutLastLine + lastLine ,
115136 completions );
116137
117- return new Result (completions , choices );
138+ return new Result (dedupe ( completions ) , choices );
118139 }
119140
120141 // -- AST-based completions --
121142
122143 private ParameterChoices astCompletions (String codeWithoutLastLine ,
123- final String lastLine , final int crop , final List <Completion > completions )
144+ final String lastLine , final int crop ,
145+ final Map <String , DotAutocompletions > predefined ,
146+ final List <Completion > completions )
124147 {
125148 // Precondition: can't expand when empty or ending with any of "[]{},; ".
126149 if (lastLine .isEmpty ()) return null ;
@@ -173,32 +196,43 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
173196 return null ;
174197 }
175198
176- // A plain name (variable, function, class) being typed.
199+ // A plain name (variable, function, class, keyword ) being typed.
177200 final Matcher m1 = nameToken .matcher (lastLine );
178201 if (m1 .find ()) {
179- final Scope scope = JythonScriptParser .parseAST (codeWithoutLastLine )
202+ final Scope scope = JythonScriptParser .parseAST (codeWithoutLastLine ,
203+ predefined )
180204 .getLast ();
205+ final String seed = m1 .group (2 );
181206 final String pre = crop > -1 && crop < m1 .group (1 ).length () ? m1 .group (1 )
182207 .substring (crop ) : "" ;
183- final Map <String , String > names = scope .findStartsWith2 (m1 .group (2 ));
208+ final String head = lastLine .substring (0 , lastLine .length () - seed
209+ .length ());
210+ final Map <String , String > names = scope .findStartsWith2 (seed );
184211 for (final Map .Entry <String , String > e : names .entrySet ()) {
212+ completions .add (Completion .builder ((head + e .getKey ()).substring (crop ))
213+ .kind (Completion .Kind .VARIABLE ).build ());
185214 final String classname = e .getValue ();
186215 if (null != classname ) {
216+ // A class: also offer its constructors.
187217 try {
188218 final Class <?> c = Class .forName (classname );
189219 for (final java .lang .reflect .Constructor <?> cons : c
190220 .getConstructors ())
191221 {
192- completions .add (makeDotCompletion (pre , m1 . group ( 2 ) ,
222+ completions .add (makeDotCompletion (pre , seed ,
193223 new CompletionText (e .getKey (), c , cons )));
194224 }
195225 }
196- catch (final ClassNotFoundException cnfe ) {
226+ catch (final ClassNotFoundException | LinkageError exc ) {
197227 JythonDev .printTrace ("Can't load class: " + classname );
198228 }
199229 }
200- completions .add (Completion .of ((lastLine + e .getKey ().substring (m1
201- .group (2 ).length ())).substring (crop )));
230+ }
231+ for (final String keyword : KEYWORDS ) {
232+ if (keyword .startsWith (seed ) && !keyword .equals (seed )) {
233+ completions .add (Completion .builder ((head + keyword ).substring (crop ))
234+ .kind (Completion .Kind .KEYWORD ).build ());
235+ }
202236 }
203237 return scopeChoices (scope );
204238 }
@@ -207,7 +241,8 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
207241 final Matcher m1c = invocation .matcher (lastLine );
208242 if (m1c .find ()) {
209243 final String name = m1c .group (2 ).substring (0 , m1c .group (2 ).length () - 1 );
210- final Scope scope = JythonScriptParser .parseAST (codeWithoutLastLine )
244+ final Scope scope = JythonScriptParser .parseAST (codeWithoutLastLine ,
245+ predefined )
211246 .getLast ();
212247 final DotAutocompletions da = scope .find (name , DotAutocompletions .EMPTY );
213248 if (da instanceof ConstructorAutocompletions ) {
@@ -246,7 +281,7 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
246281 lastLine .substring (0 , start ) + varName + " = " + lastLine .substring (
247282 start , lastLine .length () - 1 - seed .length ());
248283 }
249- final Scope scope = JythonScriptParser .parseAST (code );
284+ final Scope scope = JythonScriptParser .parseAST (code , predefined );
250285 final DotAutocompletions da = scope .getLast ().find (varName ,
251286 DotAutocompletions .EMPTY );
252287 final String fullPre = lastLine .substring (crop );
@@ -368,9 +403,12 @@ private void importCompletions(final String text, final String fullCode,
368403 final Matcher m3 = simpleClassName .matcher (text );
369404 if (m3 .find ()) {
370405 final String pre = m3 .group (1 );
371- for (final String className : ClassIndex .findSimpleClassNamesStartingWith (
372- m3 .group (2 )))
373- {
406+ final List <String > classNames = ClassIndex
407+ .findSimpleClassNamesStartingWith (m3 .group (2 ));
408+ // Closest matches first: shortest simple name, then alphabetical.
409+ classNames .sort (Comparator .comparingInt ((String cn ) -> cn .length () - cn
410+ .lastIndexOf ('.' )).thenComparing (Comparator .naturalOrder ()));
411+ for (final String className : classNames ) {
374412 final String simpleName = className .substring (className .lastIndexOf (
375413 '.' ) + 1 );
376414 final String importStmt = formatter .singleToImportStatement (className );
@@ -387,6 +425,19 @@ private void importCompletions(final String text, final String fullCode,
387425
388426 // -- Helpers --
389427
428+ /**
429+ * Removes duplicate completions (same text and parameters), keeping the first
430+ * occurrence, since the AST and class-name analyses can suggest the same thing.
431+ */
432+ private static List <Completion > dedupe (final List <Completion > completions ) {
433+ final java .util .Set <String > seen = new java .util .HashSet <>();
434+ final List <Completion > result = new ArrayList <>(completions .size ());
435+ for (final Completion c : completions ) {
436+ if (seen .add (c .insertionText () + "\u0000 " + c .parameters ())) result .add (c );
437+ }
438+ return result ;
439+ }
440+
390441 /** Builds a neutral completion from a {@link CompletionText} and prefix. */
391442 private static Completion makeDotCompletion (final String pre ,
392443 final String seed , final CompletionText ct )
0 commit comments