implemented code completion defaults #2951

This commit is contained in:
Manindra Moharana
2015-08-14 00:07:56 -07:00
parent 25380f9a55
commit 68de54bbc6
4 changed files with 132 additions and 14 deletions

View File

@@ -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<String, HashSet<String>> 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<String> 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.

View File

@@ -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, "<html>"
@@ -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;
}

View File

@@ -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;
}