1818import org .fife .ui .autocomplete .Completion ;
1919import org .fife .ui .autocomplete .DefaultCompletionProvider ;
2020import org .fife .ui .rsyntaxtextarea .RSyntaxTextArea ;
21- import org .scijava .ui .swing .script .ClassUtil ;
2221
23- public class AutocompletionProvider extends DefaultCompletionProvider {
22+ public class JythonAutocompletionProvider extends DefaultCompletionProvider {
2423
2524 private final RSyntaxTextArea text_area ;
25+ private final ImportFormat formatter ;
2626
27- public AutocompletionProvider (final RSyntaxTextArea text_area ) {
27+ public JythonAutocompletionProvider (final RSyntaxTextArea text_area , final ImportFormat formatter ) {
2828 this .text_area = text_area ;
29+ this .formatter = formatter ;
2930 new Thread (new Runnable () {
3031 @ Override
3132 public void run () {
@@ -58,7 +59,7 @@ public boolean isValidChar(final char c) {
5859
5960 private final List <Completion > asCompletionList (final Stream <String > stream , final String pre ) {
6061 return stream
61- .map ((s ) -> new BasicCompletion (AutocompletionProvider .this , pre + s ))
62+ .map ((s ) -> new BasicCompletion (JythonAutocompletionProvider .this , pre + s ))
6263 .collect (Collectors .toList ());
6364 }
6465
@@ -72,20 +73,11 @@ public List<Completion> getCompletionsImpl(final JTextComponent comp) {
7273 // E.g. "from ij" to expand to a package name and class like ij or ij.gui or ij.plugin
7374 final Matcher m1 = fromImport .matcher (text );
7475 if (m1 .find ())
75- return asCompletionList (ClassUtil .findClassNamesContaining (m1 .group (3 ))
76- .map (new Function <String , String >() {
77- @ Override
78- public final String apply (final String s ) {
79- final int idot = s .lastIndexOf ('.' );
80- return "from " + s .substring (0 , Math .max (0 , idot )) + " import " + s .substring (idot +1 );
81- }
82- }),
83- "" );
76+ return asCompletionList (ClassUtil .findClassNamesContaining (m1 .group (3 )).map (formatter ::singleToImportStatement ), "" );
8477
8578 final Matcher m1f = fastImport .matcher (text );
8679 if (m1f .find ())
87- return asCompletionList (ClassUtil .findClassNamesForPackage (m1f .group (2 )).map (s -> s .substring (m1f .group (2 ).length () + 1 )),
88- m1f .group (0 ) + "import " );
80+ return asCompletionList (ClassUtil .findClassNamesForPackage (m1f .group (2 )).map (formatter ::singleToImportStatement ), "" );
8981
9082 // E.g. "from ij.gui import Roi, Po" to expand to PolygonRoi, PointRoi for Jython
9183 // or e.g. "importClass(Package.ij" to expand to a fully qualified class name for Javascript
@@ -113,8 +105,16 @@ public final String apply(final String s) {
113105 }
114106
115107 final Matcher m3 = simpleClassName .matcher (text );
116- if (m3 .find ())
117- return asCompletionList (ClassUtil .findSimpleClassNamesStartingWith (m3 .group (2 )).stream (), m3 .group (1 ));
108+ if (m3 .find ()) {
109+ // Side effect: insert the import at the top of the file if necessary
110+ //return asCompletionList(ClassUtil.findSimpleClassNamesStartingWith(m3.group(2)).stream(), m3.group(1));
111+ return ClassUtil .findSimpleClassNamesStartingWith (m3 .group (2 )).stream ()
112+ .map (className -> new ImportCompletion (JythonAutocompletionProvider .this ,
113+ m3 .group (1 ) + className .substring (className .lastIndexOf ('.' ) + 1 ),
114+ className ,
115+ formatter .singleToImportStatement (className )))
116+ .collect (Collectors .toList ());
117+ }
118118
119119 final Matcher m4 = staticMethodOrField .matcher (text );
120120 if (m4 .find ()) {
0 commit comments