diff --git a/java/src/processing/mode/java/JavaMode.java b/java/src/processing/mode/java/JavaMode.java index 195490a10..20b4ba140 100644 --- a/java/src/processing/mode/java/JavaMode.java +++ b/java/src/processing/mode/java/JavaMode.java @@ -22,8 +22,15 @@ package processing.mode.java; +import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; @@ -322,9 +329,15 @@ public class JavaMode extends Mode { static public final String prefAutoSavePrompt = "pdex.autoSave.promptDisplay"; static public final String prefDefaultAutoSave = "pdex.autoSave.autoSaveByDefault"; static public final String prefImportSuggestEnabled = "pdex.importSuggestEnabled"; + static public final String suggestionsFileName = "suggestions.txt"; static volatile public boolean enableTweak = false; + /** + * Stores the white list/black list of allowed/blacklisted imports. These are defined in + * suggestions.txt in java mode folder. + */ + static public final HashMap> suggestionsMap = new HashMap<>(); public void loadPreferences() { Messages.log("Load PDEX prefs"); @@ -341,6 +354,7 @@ public class JavaMode extends Mode { defaultAutoSaveEnabled = Preferences.getBoolean(prefDefaultAutoSave); ccTriggerEnabled = Preferences.getBoolean(prefCCTriggerEnabled); importSuggestEnabled = Preferences.getBoolean(prefImportSuggestEnabled); + loadSuggestionsMap(); } @@ -360,6 +374,46 @@ public class JavaMode extends Mode { Preferences.setBoolean(prefImportSuggestEnabled, importSuggestEnabled); } + public void loadSuggestionsMap() { + File suggestionsListFile = new File(getFolder() + File.separator + + suggestionsFileName); + if (!suggestionsListFile.exists()) { + Messages.loge("Suggestions file not found! " + + suggestionsListFile.getAbsolutePath()); + return; + } + + try { + BufferedReader br = new BufferedReader( + new FileReader(suggestionsListFile)); + while (true) { + String line = br.readLine(); + if (line == null) { + break; + } + line = line.trim(); + if (line.startsWith("#")) { + continue; + } else { + if (line.contains("=")) { + String key = line.split("=")[0]; + String val = line.split("=")[1]; + if (suggestionsMap.containsKey(key)) { + suggestionsMap.get(key).add(val); + } else { + HashSet al = new HashSet<>(); + al.add(val); + suggestionsMap.put(key, al); + } + } + } + } + } catch (IOException e) { + Messages.loge("IOException while reading suggestions file:" + + suggestionsListFile.getAbsolutePath()); + } + } + public void ensurePrefsExist() { //TODO: Need to do a better job of managing prefs. Think lists. diff --git a/java/src/processing/mode/java/pdex/ASTGenerator.java b/java/src/processing/mode/java/pdex/ASTGenerator.java index 5b396fc95..213b474c2 100644 --- a/java/src/processing/mode/java/pdex/ASTGenerator.java +++ b/java/src/processing/mode/java/pdex/ASTGenerator.java @@ -74,6 +74,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; +import org.apache.tools.ant.taskdefs.Java; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; @@ -953,13 +954,14 @@ public class ASTGenerator { Pattern.compile(word2 + "[a-zA-Z_0-9]*.class", Pattern.CASE_INSENSITIVE)); String[] resources = classPath.findResources("", regExpResourceFilter); + for (String matchedClass2 : resources) { matchedClass2 = matchedClass2.replace('/', '.'); //package name String matchedClass = matchedClass2.substring(0, matchedClass2.length() - 6); int d = matchedClass.lastIndexOf('.'); - if (ignorableImport(matchedClass2,matchedClass.substring(d + 1))) + if (ignorableImport(matchedClass,matchedClass.substring(d + 1))) { continue; - + } matchedClass = matchedClass.substring(d + 1); //class name candidates .add(new CompletionCandidate(matchedClass, "" @@ -3507,22 +3509,34 @@ public class ASTGenerator { } } - public static final String ignoredImports[] = { - "com.oracle.", "sun.", "sunw.", "com.sun.", "javax.", "sunw.", "org.ietf.", - "org.jcp.", "org.omg.", "org.w3c.", "org.xml.", "org.eclipse.", "com.ibm.", - "org.netbeans.", "org.jsoup.", "org.junit.", "org.apache.", "antlr." }; - public static final String allowedImports[] = {"java.lang.", "java.util.", "java.io.", - "java.math.", "processing.core.", "processing.data.", "processing.event.", "processing.opengl."}; - protected boolean ignorableImport(String impName, String className) { - //TODO: Trie man. + protected boolean ignorableImport(String impName, String fullClassName) { for (ImportStatement impS : errorCheckerService.getProgramImports()) { - if(impName.startsWith(impS.getPackageName())) + if (impName.toLowerCase().startsWith(impS.getPackageName().toLowerCase())) { return false; + } } - for (String impS : allowedImports) { - if(impName.startsWith(impS) && className.indexOf('.') == -1) + if (JavaMode.suggestionsMap == null + || JavaMode.suggestionsMap.keySet().size() == 0) { + log("SuggestionsMap is null or empty, won't be able to trim class names"); + return true; + } + final String processingInclude = "include.processing"; + final String processingExclude = "exclude.processing"; + final String jdkInclude = "include.jdk"; + + if (impName.startsWith("processing")) { + if (JavaMode.suggestionsMap.get(processingInclude).contains(impName)) { return false; + } else if (JavaMode.suggestionsMap.get(processingExclude) + .contains(impName)) { + return true; + } + } else if (impName.startsWith("java")) { + if (JavaMode.suggestionsMap.get(jdkInclude).contains(impName)) { + return false; + } } + return true; } diff --git a/java/src/processing/mode/java/pdex/ImportStatement.java b/java/src/processing/mode/java/pdex/ImportStatement.java index 993e0117d..bb86955f8 100644 --- a/java/src/processing/mode/java/pdex/ImportStatement.java +++ b/java/src/processing/mode/java/pdex/ImportStatement.java @@ -63,7 +63,9 @@ public class ImportStatement { if(ret.startsWith("import ")) ret = ret.substring(7); if(ret.endsWith(";")) - ret = ret.substring(0, ret.length() - 1); + ret = ret.substring(0, ret.length() - 1).trim(); + if(ret.endsWith(".*")) + ret = ret.substring(0, ret.length() - 2); return ret; } diff --git a/java/suggestions.txt b/java/suggestions.txt new file mode 100644 index 000000000..ff86167a2 --- /dev/null +++ b/java/suggestions.txt @@ -0,0 +1,48 @@ +#List of suggestions to include/exclude in code completion + +#.