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
@@ -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.
@@ -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;
}
@@ -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;
}
+48
View File
@@ -0,0 +1,48 @@
#List of suggestions to include/exclude in code completion
#<include/exclude>.<label>=<fully qualified class name>
include.processing=processing.core.PApplet
include.processing=processing.core.PFont
include.processing=processing.core.PGraphics
include.processing=processing.core.PImage
include.processing=processing.core.PMatrix2D
include.processing=processing.core.PMatrix3D
include.processing=processing.core.PStyle
include.processing=processing.core.PVector
include.processing=processing.core.PShape
include.processing=processing.core.PGraphicsJava2D
include.processing=processing.core.PGraphics2D
include.processing=processing.core.PGraphics3D
include.processing=processing.data.FloatDict
include.processing=processing.data.FloatList
include.processing=processing.data.IntDict
include.processing=processing.data.IntList
include.processing=processing.data.JSONArray
include.processing=processing.data.JSONObject
include.processing=processing.data.StringDict
include.processing=processing.data.StringList
include.processing=processing.data.Table
include.processing=processing.data.XML
include.processing=processing.event.Event
include.processing=processing.event.KeyEvent
include.processing=processing.event.MouseEvent
include.processing=processing.event.TouchEvent
include.processing=processing.opengl.PShader
include.processing=processing.opengl.PGL
exclude.processing=processing.core.PGraphicsRetina2D
exclude.processing=processing.core.PShapeOBJ
exclude.processing=processing.core.PShapeSVG
exclude.processing=processing.data.Sort
exclude.processing=processing.opengl.FrameBuffer
exclude.processing=processing.opengl.LinePath
exclude.processing=processing.opengl.LinePath.PathIterator
exclude.processing=processing.opengl.LineStroker
exclude.processing=processing.opengl.PGraphicsOpenGL
include.jdk=java.util.ArrayList
include.jdk=java.io.BufferedReader
include.jdk=java.util.HashMap
include.jdk=java.io.PrintWriter
include.jdk=java.lang.String