Skip to content

Commit 3660b3f

Browse files
committed
TextEditor: restore addImport function
When given a "raw" class (no package), addImport will now iterate over all elements in the core ImageJ packages looking for potential matches to the requested class. All potential matches will be added as imports (e.g. net.imglib2.ImgPlus and net.imglib2.meta.ImgPlus will both be added). If there are no matches, the class will be imported without a package. Addresses http://fiji.sc/bugzilla/show_bug.cgi?id=817
1 parent 59c60eb commit 3660b3f

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

src/main/java/net/imagej/ui/swing/script/TextEditor.java

+66-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.io.Writer;
6060
import java.net.URL;
6161
import java.util.ArrayList;
62+
import java.util.Collection;
6263
import java.util.Collections;
6364
import java.util.Comparator;
6465
import java.util.Date;
@@ -107,6 +108,10 @@
107108
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
108109
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
109110
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
111+
import org.reflections.Reflections;
112+
import org.reflections.scanners.SubTypesScanner;
113+
import org.reflections.util.ClasspathHelper;
114+
import org.reflections.util.ConfigurationBuilder;
110115
import org.scijava.Context;
111116
import org.scijava.command.CommandService;
112117
import org.scijava.event.ContextDisposingEvent;
@@ -168,6 +173,7 @@ public class TextEditor extends JFrame implements ActionListener,
168173
}
169174

170175
private static AbstractTokenMakerFactory tokenMakerFactory = null;
176+
private static Reflections reflections = null;
171177

172178
private JTabbedPane tabbed;
173179
private JMenuItem newFile, open, save, saveas, compileAndRun, compile,
@@ -2064,9 +2070,37 @@ public String getSelectedTextOrAsk(final String label) {
20642070
public String getSelectedClassNameOrAsk() {
20652071
String className = getSelectedTextOrAsk("Class name");
20662072
if (className != null) className = className.trim();
2073+
20672074
return className;
20682075
}
20692076

2077+
/**
2078+
* Returns the static Reflections instance, constructing it
2079+
* if it doesn't already exist. This is to limit the number of
2080+
* classpath scans.
2081+
*
2082+
* @return static {@link Reflections} instance
2083+
*/
2084+
private static Reflections getReflections() {
2085+
if (reflections == null) {
2086+
synchronized(TextEditor.class) {
2087+
if (reflections == null) {
2088+
final Collection<URL> packages = new HashSet<>();
2089+
packages.addAll(ClasspathHelper.forPackage("net.imagej"));
2090+
packages.addAll(ClasspathHelper.forPackage("org.scijava"));
2091+
packages.addAll(ClasspathHelper.forPackage("net.imglib2"));
2092+
packages.addAll(ClasspathHelper.forPackage("io.scif"));
2093+
packages.addAll(ClasspathHelper.forPackage("sc.fiji"));
2094+
packages.addAll(ClasspathHelper.forPackage("ij"));
2095+
reflections = new Reflections(new ConfigurationBuilder().setUrls(
2096+
packages).setScanners(new SubTypesScanner(false)));
2097+
}
2098+
}
2099+
}
2100+
2101+
return reflections;
2102+
}
2103+
20702104
private static void append(final JTextArea textArea, final String text) {
20712105
final int length = textArea.getDocument().getLength();
20722106
textArea.insert(text, length);
@@ -2209,7 +2243,38 @@ private void updateGitDirectory() {
22092243

22102244
public void addImport(final String className) {
22112245
if (className != null) {
2212-
new TokenFunctions(getTextArea()).addImport(className.trim());
2246+
2247+
boolean addRaw = true;
2248+
2249+
// If there are NO package separators then this is a raw class name. Try
2250+
// and find matching packages
2251+
2252+
// NB: decided to only look for complete class names without packages.
2253+
// Matching "endsWith(className) can produce a plethora of false positives
2254+
// which we want to limit, because the current implementation imports
2255+
// all matches.
2256+
// Some alternatives to consider (including combinations):
2257+
// - match *className
2258+
// - match *className*
2259+
// - match .className*
2260+
// - if >1 match show list to the user to choose a "winner" to import
2261+
if (!className.contains(".")) {
2262+
final String packagedClass = "." + className;
2263+
final Reflections refl = TextEditor.getReflections();
2264+
final StringBuilder sb = new StringBuilder();
2265+
2266+
for (final String type : refl.getAllTypes()) {
2267+
// look for "blah.className"
2268+
if (type.endsWith(packagedClass)) {
2269+
addRaw = false;
2270+
new TokenFunctions(getTextArea()).addImport(type.trim());
2271+
}
2272+
}
2273+
}
2274+
2275+
// If there was a package separator or no matching packages, import the raw
2276+
// class.
2277+
if (addRaw) new TokenFunctions(getTextArea()).addImport(className.trim());
22132278
}
22142279
}
22152280

0 commit comments

Comments
 (0)