Skip to content

Commit 6a1ed78

Browse files
Code Completion Improvements
I want to make the editor supportive of multiple languages. Putting the keywords in a separate class and going to add more languages. Code to handle the different languages added to UI.java. I’m not finished yet, need to do some clean up/documenting. This works though.
1 parent 25aa5b2 commit 6a1ed78

File tree

3 files changed

+184
-82
lines changed

3 files changed

+184
-82
lines changed

src/simplejavatexteditor/JavaAutoComplete.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,12 @@
3636
public class JavaAutoComplete
3737
implements DocumentListener {
3838

39-
private static String[] keywords = {"abstract", "assert", "boolean",
40-
"break", "byte", "case", "catch", "char", "class", "const",
41-
"continue", "default", "do", "double", "else", "extends", "false",
42-
"final", "finally", "float", "for", "goto", "if", "implements",
43-
"import", "instanceof", "int", "System", "out", "print()", "println()",
44-
"new", "null", "package", "private", "protected", "public", "interface",
45-
"long", "native", "return", "short", "static", "strictfp", "super", "switch",
46-
"synchronized", "this", "throw", "throws", "transient", "true",
47-
"try", "void", "volatile", "while", "String"};
48-
49-
private static String[] bracketChars = {"{", "("};
50-
private static String[] bCompletions = {"}", ")"};
51-
private ArrayList<String> words = new ArrayList<>();
5239
private ArrayList<String> brackets = new ArrayList<>();
5340
private ArrayList<String> bracketCompletions = new ArrayList<>();
5441

42+
private ArrayList<String> words = new ArrayList<>();
43+
44+
SupportedKeywords kw;
5545
//Keep track of when code completion
5646
//has been activated
5747
private enum Mode {
@@ -68,6 +58,16 @@ private enum Mode {
6858
private String content;
6959

7060
public JavaAutoComplete(UI ui) {
61+
this.ui = ui;
62+
textArea = ui.getEditor();
63+
}
64+
65+
public JavaAutoComplete(UI ui, ArrayList<String> al) {
66+
words = al;
67+
kw = new SupportedKeywords();
68+
brackets = kw.getbrackets();
69+
bracketCompletions = kw.getbracketCompletions();
70+
7171
//Access the editor
7272
this.ui = ui;
7373
textArea = ui.getEditor();
@@ -78,19 +78,16 @@ public JavaAutoComplete(UI ui) {
7878
im.put(KeyStroke.getKeyStroke("ENTER "), COMMIT_ACTION);
7979
am.put(COMMIT_ACTION, new CommitAction());
8080

81-
//Set up the keywords
82-
for (String keyList : keywords) {
83-
words.add(keyList);
84-
}
85-
for (String bracket : bracketChars) {
86-
brackets.add(bracket);
87-
}
88-
for (String comp : bCompletions) {
89-
bracketCompletions.add(comp);
90-
}
91-
Collections.sort(words, null);
81+
Collections.sort(words);
9282
}
9383

84+
/**
85+
* A character has been typed into the document.
86+
* This method performs the primary
87+
* check to find a keyword completion.
88+
*
89+
* @param e
90+
*/
9491
@Override
9592
public void insertUpdate(DocumentEvent e) {
9693
pos = e.getOffset();
@@ -209,7 +206,8 @@ public void run() {
209206
}
210207

211208
/**
212-
* Enter key is pressed in response to an auto complete suggestion. Respond
209+
* Enter key is pressed in response to an
210+
* auto complete suggestion. Respond
213211
* appropriately.
214212
*/
215213
private class CommitAction
@@ -284,4 +282,7 @@ public void removeUpdate(DocumentEvent e) {
284282
public void changedUpdate(DocumentEvent e) {
285283
}
286284

287-
}
285+
public void removeListener() {
286+
textArea.getDocument().removeDocumentListener(this);
287+
}
288+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package simplejavatexteditor;
2+
3+
import java.util.ArrayList;
4+
5+
public class SupportedKeywords {
6+
private String[] java = {"abstract", "assert", "boolean",
7+
"break", "byte", "case", "catch", "char", "class", "const",
8+
"continue", "default", "do", "double", "else", "extends", "false",
9+
"final", "finally", "float", "for", "goto", "if", "implements",
10+
"import", "instanceof", "int", "System", "out", "print()", "println()",
11+
"new", "null", "package", "private", "protected", "public", "interface",
12+
"long", "native", "return", "short", "static", "strictfp", "super", "switch",
13+
"synchronized", "this", "throw", "throws", "transient", "true",
14+
"try", "void", "volatile", "while", "String"};
15+
16+
private String[] cpp = { "auto", "const", "double", "float", "int", "short",
17+
"struct", "unsigned", "break", "continue", "else", "for", "long", "signed",
18+
"switch", "void", "case", "default", "enum", "goto", "register", "sizeof",
19+
"typedef", "volatile", "char", "do", "extern", "if", "return", "static",
20+
"union", "while", "asm", "dynamic_cast", "namespace", "reinterpret_cast", "try",
21+
"bool", "explicit", "new", "static_cast", "typeid", "catch", "false", "operator",
22+
"template", "typename", "class", "friend", "private", "this", "using", "const_cast", "inline",
23+
"public", "throw", "virtual", "delete", "mutable", "protected", "true", "wchar_t", };
24+
25+
private String[] brackets = { "{", "(" };
26+
private String[] bCompletions = { "}", ")" };
27+
public String[] getJavaKeywords() {
28+
return java;
29+
}
30+
public String[] getCppKeywords() {
31+
return cpp;
32+
}
33+
public ArrayList<String> getbracketCompletions() {
34+
ArrayList<String> alist = new ArrayList<>();
35+
for(String completion : bCompletions) {
36+
alist.add(completion);
37+
}
38+
return alist;
39+
}
40+
public ArrayList<String> getbrackets() {
41+
ArrayList<String> alist = new ArrayList<>();
42+
for(String completion : brackets) {
43+
alist.add(completion);
44+
}
45+
return alist;
46+
}
47+
public ArrayList<String> setKeywords(String[] arr) {
48+
ArrayList<String> al = new ArrayList<>();
49+
for(String words : arr) {
50+
al.add(words);
51+
}
52+
return al;
53+
}
54+
}

0 commit comments

Comments
 (0)