Skip to content

Commit 68de54b

Browse files
committed
implemented code completion defaults #2951
1 parent 25380f9 commit 68de54b

File tree

4 files changed

+132
-14
lines changed

4 files changed

+132
-14
lines changed

java/src/processing/mode/java/JavaMode.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@
2222

2323
package processing.mode.java;
2424

25+
import java.io.BufferedReader;
2526
import java.io.File;
27+
import java.io.FileNotFoundException;
28+
import java.io.FileReader;
2629
import java.io.IOException;
30+
import java.io.InputStreamReader;
31+
import java.util.ArrayList;
32+
import java.util.HashMap;
33+
import java.util.HashSet;
2734
import java.util.logging.FileHandler;
2835
import java.util.logging.Handler;
2936
import java.util.logging.Level;
@@ -322,9 +329,15 @@ void initLogger() {
322329
static public final String prefAutoSavePrompt = "pdex.autoSave.promptDisplay";
323330
static public final String prefDefaultAutoSave = "pdex.autoSave.autoSaveByDefault";
324331
static public final String prefImportSuggestEnabled = "pdex.importSuggestEnabled";
332+
static public final String suggestionsFileName = "suggestions.txt";
325333

326334
static volatile public boolean enableTweak = false;
327335

336+
/**
337+
* Stores the white list/black list of allowed/blacklisted imports. These are defined in
338+
* suggestions.txt in java mode folder.
339+
*/
340+
static public final HashMap<String, HashSet<String>> suggestionsMap = new HashMap<>();
328341

329342
public void loadPreferences() {
330343
Messages.log("Load PDEX prefs");
@@ -341,6 +354,7 @@ public void loadPreferences() {
341354
defaultAutoSaveEnabled = Preferences.getBoolean(prefDefaultAutoSave);
342355
ccTriggerEnabled = Preferences.getBoolean(prefCCTriggerEnabled);
343356
importSuggestEnabled = Preferences.getBoolean(prefImportSuggestEnabled);
357+
loadSuggestionsMap();
344358
}
345359

346360

@@ -360,6 +374,46 @@ public void savePreferences() {
360374
Preferences.setBoolean(prefImportSuggestEnabled, importSuggestEnabled);
361375
}
362376

377+
public void loadSuggestionsMap() {
378+
File suggestionsListFile = new File(getFolder() + File.separator
379+
+ suggestionsFileName);
380+
if (!suggestionsListFile.exists()) {
381+
Messages.loge("Suggestions file not found! "
382+
+ suggestionsListFile.getAbsolutePath());
383+
return;
384+
}
385+
386+
try {
387+
BufferedReader br = new BufferedReader(
388+
new FileReader(suggestionsListFile));
389+
while (true) {
390+
String line = br.readLine();
391+
if (line == null) {
392+
break;
393+
}
394+
line = line.trim();
395+
if (line.startsWith("#")) {
396+
continue;
397+
} else {
398+
if (line.contains("=")) {
399+
String key = line.split("=")[0];
400+
String val = line.split("=")[1];
401+
if (suggestionsMap.containsKey(key)) {
402+
suggestionsMap.get(key).add(val);
403+
} else {
404+
HashSet<String> al = new HashSet<>();
405+
al.add(val);
406+
suggestionsMap.put(key, al);
407+
}
408+
}
409+
}
410+
}
411+
} catch (IOException e) {
412+
Messages.loge("IOException while reading suggestions file:"
413+
+ suggestionsListFile.getAbsolutePath());
414+
}
415+
}
416+
363417

364418
public void ensurePrefsExist() {
365419
//TODO: Need to do a better job of managing prefs. Think lists.

java/src/processing/mode/java/pdex/ASTGenerator.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import javax.swing.tree.DefaultTreeModel;
7575
import javax.swing.tree.MutableTreeNode;
7676

77+
import org.apache.tools.ant.taskdefs.Java;
7778
import org.eclipse.jdt.core.JavaCore;
7879
import org.eclipse.jdt.core.dom.AST;
7980
import org.eclipse.jdt.core.dom.ASTNode;
@@ -953,13 +954,14 @@ public void preparePredictions(final String word, final int line,
953954
Pattern.compile(word2 + "[a-zA-Z_0-9]*.class",
954955
Pattern.CASE_INSENSITIVE));
955956
String[] resources = classPath.findResources("", regExpResourceFilter);
957+
956958
for (String matchedClass2 : resources) {
957959
matchedClass2 = matchedClass2.replace('/', '.'); //package name
958960
String matchedClass = matchedClass2.substring(0, matchedClass2.length() - 6);
959961
int d = matchedClass.lastIndexOf('.');
960-
if (ignorableImport(matchedClass2,matchedClass.substring(d + 1)))
962+
if (ignorableImport(matchedClass,matchedClass.substring(d + 1))) {
961963
continue;
962-
964+
}
963965
matchedClass = matchedClass.substring(d + 1); //class name
964966
candidates
965967
.add(new CompletionCandidate(matchedClass, "<html>"
@@ -3507,22 +3509,34 @@ public static void disposeWindow(JFrame... f) {
35073509
}
35083510
}
35093511

3510-
public static final String ignoredImports[] = {
3511-
"com.oracle.", "sun.", "sunw.", "com.sun.", "javax.", "sunw.", "org.ietf.",
3512-
"org.jcp.", "org.omg.", "org.w3c.", "org.xml.", "org.eclipse.", "com.ibm.",
3513-
"org.netbeans.", "org.jsoup.", "org.junit.", "org.apache.", "antlr." };
3514-
public static final String allowedImports[] = {"java.lang.", "java.util.", "java.io.",
3515-
"java.math.", "processing.core.", "processing.data.", "processing.event.", "processing.opengl."};
3516-
protected boolean ignorableImport(String impName, String className) {
3517-
//TODO: Trie man.
3512+
protected boolean ignorableImport(String impName, String fullClassName) {
35183513
for (ImportStatement impS : errorCheckerService.getProgramImports()) {
3519-
if(impName.startsWith(impS.getPackageName()))
3514+
if (impName.toLowerCase().startsWith(impS.getPackageName().toLowerCase())) {
35203515
return false;
3516+
}
3517+
}
3518+
if (JavaMode.suggestionsMap == null
3519+
|| JavaMode.suggestionsMap.keySet().size() == 0) {
3520+
log("SuggestionsMap is null or empty, won't be able to trim class names");
3521+
return true;
35213522
}
3522-
for (String impS : allowedImports) {
3523-
if(impName.startsWith(impS) && className.indexOf('.') == -1)
3523+
final String processingInclude = "include.processing";
3524+
final String processingExclude = "exclude.processing";
3525+
final String jdkInclude = "include.jdk";
3526+
3527+
if (impName.startsWith("processing")) {
3528+
if (JavaMode.suggestionsMap.get(processingInclude).contains(impName)) {
3529+
return false;
3530+
} else if (JavaMode.suggestionsMap.get(processingExclude)
3531+
.contains(impName)) {
3532+
return true;
3533+
}
3534+
} else if (impName.startsWith("java")) {
3535+
if (JavaMode.suggestionsMap.get(jdkInclude).contains(impName)) {
35243536
return false;
3537+
}
35253538
}
3539+
35263540
return true;
35273541
}
35283542

java/src/processing/mode/java/pdex/ImportStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public String getPackageName(){
6363
if(ret.startsWith("import "))
6464
ret = ret.substring(7);
6565
if(ret.endsWith(";"))
66-
ret = ret.substring(0, ret.length() - 1);
66+
ret = ret.substring(0, ret.length() - 1).trim();
67+
if(ret.endsWith(".*"))
68+
ret = ret.substring(0, ret.length() - 2);
6769
return ret;
6870
}
6971

java/suggestions.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#List of suggestions to include/exclude in code completion
2+
3+
#<include/exclude>.<label>=<fully qualified class name>
4+
5+
include.processing=processing.core.PApplet
6+
include.processing=processing.core.PFont
7+
include.processing=processing.core.PGraphics
8+
include.processing=processing.core.PImage
9+
include.processing=processing.core.PMatrix2D
10+
include.processing=processing.core.PMatrix3D
11+
include.processing=processing.core.PStyle
12+
include.processing=processing.core.PVector
13+
include.processing=processing.core.PShape
14+
include.processing=processing.core.PGraphicsJava2D
15+
include.processing=processing.core.PGraphics2D
16+
include.processing=processing.core.PGraphics3D
17+
include.processing=processing.data.FloatDict
18+
include.processing=processing.data.FloatList
19+
include.processing=processing.data.IntDict
20+
include.processing=processing.data.IntList
21+
include.processing=processing.data.JSONArray
22+
include.processing=processing.data.JSONObject
23+
include.processing=processing.data.StringDict
24+
include.processing=processing.data.StringList
25+
include.processing=processing.data.Table
26+
include.processing=processing.data.XML
27+
include.processing=processing.event.Event
28+
include.processing=processing.event.KeyEvent
29+
include.processing=processing.event.MouseEvent
30+
include.processing=processing.event.TouchEvent
31+
include.processing=processing.opengl.PShader
32+
include.processing=processing.opengl.PGL
33+
34+
exclude.processing=processing.core.PGraphicsRetina2D
35+
exclude.processing=processing.core.PShapeOBJ
36+
exclude.processing=processing.core.PShapeSVG
37+
exclude.processing=processing.data.Sort
38+
exclude.processing=processing.opengl.FrameBuffer
39+
exclude.processing=processing.opengl.LinePath
40+
exclude.processing=processing.opengl.LinePath.PathIterator
41+
exclude.processing=processing.opengl.LineStroker
42+
exclude.processing=processing.opengl.PGraphicsOpenGL
43+
44+
include.jdk=java.util.ArrayList
45+
include.jdk=java.io.BufferedReader
46+
include.jdk=java.util.HashMap
47+
include.jdk=java.io.PrintWriter
48+
include.jdk=java.lang.String

0 commit comments

Comments
 (0)