Skip to content

Commit b9996ac

Browse files
committed
FEATURE: Autocomplete text
1 parent c3675a8 commit b9996ac

File tree

6 files changed

+173
-76
lines changed

6 files changed

+173
-76
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<classpathentry kind="src" path="app/test"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
66
<classpathentry kind="lib" path="app/lib/apple.jar"/>
7+
<classpathentry kind="lib" path="app/lib/autocomplete-3.0.0.jar"/>
78
<classpathentry kind="lib" path="app/lib/ecj.jar"/>
89
<classpathentry kind="lib" path="app/test-lib/junit-4.11.jar"/>
910
<classpathentry kind="lib" path="app/test-lib/fest-assert-1.2.jar"/>

app/lib/autocomplete-3.0.0.jar

148 KB
Binary file not shown.

app/src/processing/app/EditorTab.java

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public EditorTab(Editor editor, SketchFile file, String contents)
111111
applyPreferences();
112112
add(scrollPane, BorderLayout.CENTER);
113113
editor.base.addEditorFontResizeMouseWheelListener(textarea);
114+
textarea.autoCompleteText();
114115
}
115116

116117
private RSyntaxDocument createDocument(String contents) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package processing.app.syntax;
2+
3+
public class KeywordModel {
4+
5+
String keyword, type;
6+
7+
public KeywordModel(String keyword) {
8+
this.keyword = keyword;
9+
}
10+
11+
public void setType(String type) {
12+
this.type = type;
13+
}
14+
15+
public String getKeyword() {
16+
return keyword;
17+
}
18+
19+
public String getType() {
20+
return type;
21+
}
22+
23+
}

app/src/processing/app/syntax/PdeKeywords.java

+51-26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
package processing.app.syntax;
2626

2727
import org.apache.commons.compress.utils.IOUtils;
28+
import org.fife.ui.autocomplete.BasicCompletion;
29+
import org.fife.ui.autocomplete.Completion;
30+
import org.fife.ui.autocomplete.DefaultCompletionProvider;
2831
import org.fife.ui.rsyntaxtextarea.TokenMap;
2932
import org.fife.ui.rsyntaxtextarea.TokenTypes;
3033
import processing.app.Base;
@@ -36,11 +39,14 @@
3639
import java.io.File;
3740
import java.io.FileInputStream;
3841
import java.io.InputStreamReader;
42+
import java.util.ArrayList;
43+
import java.util.Collection;
3944
import java.util.HashMap;
45+
import java.util.LinkedList;
46+
import java.util.List;
4047
import java.util.Map;
4148
import java.util.regex.Pattern;
4249

43-
4450
public class PdeKeywords {
4551

4652
private static final Map<String, Integer> KNOWN_TOKEN_TYPES = new HashMap<>();
@@ -65,6 +71,10 @@ public class PdeKeywords {
6571
// lookup table that maps keywords to their html reference pages
6672
private final Map<String, String> keywordToReference;
6773

74+
// Save the keywords in an ArrayList to use the AutoCompletion feature
75+
private ArrayList<KeywordModel> keywords = new ArrayList<KeywordModel>();
76+
private short position;
77+
6878
public PdeKeywords() {
6979
this.keywordTokenType = new TokenMap();
7080
this.keywordOldToken = new HashMap<>();
@@ -75,65 +85,76 @@ public PdeKeywords() {
7585
/**
7686
* Handles loading of keywords file.
7787
* <p/>
78-
* Uses getKeywords() method because that's part of the
79-
* TokenMarker classes.
88+
* Uses getKeywords() method because that's part of the TokenMarker classes.
8089
* <p/>
81-
* It is recommended that a # sign be used for comments
82-
* inside keywords.txt.
90+
* It is recommended that a # sign be used for comments inside keywords.txt.
8391
*/
8492
public void reload() {
8593
try {
86-
parseKeywordsTxt(new File(BaseNoGui.getContentFile("lib"), "keywords.txt"));
94+
parseKeywordsTxt(new File(BaseNoGui.getContentFile("lib"),
95+
"keywords.txt"));
8796
TargetPlatform tp = BaseNoGui.getTargetPlatform();
8897
if (tp != null) {
8998
File platformKeywords = new File(tp.getFolder(), "keywords.txt");
90-
if (platformKeywords.exists()) parseKeywordsTxt(platformKeywords);
99+
if (platformKeywords.exists())
100+
parseKeywordsTxt(platformKeywords);
91101
}
92-
for (UserLibrary lib : BaseNoGui.librariesIndexer.getInstalledLibraries()) {
102+
for (UserLibrary lib : BaseNoGui.librariesIndexer
103+
.getInstalledLibraries()) {
93104
File keywords = new File(lib.getInstalledFolder(), "keywords.txt");
94105
if (keywords.exists()) {
95106
parseKeywordsTxt(keywords);
96107
}
97108
}
98109
} catch (Exception e) {
99-
Base.showError("Problem loading keywords", "Could not load keywords.txt,\nplease re-install Arduino.", e);
110+
e.printStackTrace();
111+
112+
Base.showError("Problem loading keywords",
113+
"Could not load keywords.txt,\nplease re-install Arduino.",
114+
e);
100115
System.exit(1);
101116
}
102117
}
103118

104119
private void parseKeywordsTxt(File input) throws Exception {
105120
BufferedReader reader = null;
106121
try {
107-
reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
122+
reader = new BufferedReader(
123+
new InputStreamReader(new FileInputStream(input)));
108124

109125
String line;
110126
while ((line = reader.readLine()) != null) {
111-
//System.out.println("line is " + line);
127+
// System.out.println("line is " + line);
112128
// in case there's any garbage on the line
113129
line = line.trim();
114130
if (line.length() == 0 || line.startsWith("#")) {
115131
continue;
116132
}
117-
118133
String pieces[] = line.split("\t");
119134

120-
String keyword = pieces[0].trim();
121-
if (keyword.startsWith("\\#")) {
122-
keyword = keyword.replace("\\#", "#");
135+
keywords.add(position, new KeywordModel(pieces[0].trim()));
136+
if (keywords.get(position).getKeyword().startsWith("\\#")) {
137+
keywords.add(position, new KeywordModel(
138+
keywords.get(position).getKeyword().replace("\\#", "#")));
123139
}
124140

125141
if (pieces.length >= 2) {
126-
keywordOldToken.put(keyword, pieces[1]);
142+
keywordOldToken.put(keywords.get(position).getKeyword(), pieces[1]);
143+
keywords.get(position).setType(pieces[1]);
127144
}
128-
129145
if (pieces.length >= 3) {
130-
parseHTMLReferenceFileName(pieces[2], keyword);
146+
parseHTMLReferenceFileName(pieces[2],
147+
keywords.get(position).getKeyword());
148+
keywords.get(position).setType("FUNCTION");
131149
}
132150
if (pieces.length >= 4) {
133-
parseRSyntaxTextAreaTokenType(pieces[3], keyword);
151+
keywords.get(position)
152+
.setType(parseRSyntaxTextAreaTokenType(pieces[3], keywords
153+
.get(position).getKeyword()));
134154
}
135-
}
136155

156+
position++;
157+
}
137158
fillMissingTokenType();
138159
} finally {
139160
IOUtils.closeQuietly(reader);
@@ -147,20 +168,19 @@ private void fillMissingTokenType() {
147168
if (!keywordTokenTypeAsString.containsKey(keyword)) {
148169
if ("KEYWORD1".equals(oldTokenEntry.getValue())) {
149170
parseRSyntaxTextAreaTokenType("DATA_TYPE", keyword);
150-
}
151-
else if ("LITERAL1".equals(oldTokenEntry.getValue())) {
171+
} else if ("LITERAL1".equals(oldTokenEntry.getValue())) {
152172
parseRSyntaxTextAreaTokenType("RESERVED_WORD_2", keyword);
153-
}
154-
else {
173+
} else {
155174
parseRSyntaxTextAreaTokenType("FUNCTION", keyword);
156175
}
157176
}
158177
}
159178
}
160179

161-
private void parseRSyntaxTextAreaTokenType(String tokenTypeAsString, String keyword) {
180+
private String parseRSyntaxTextAreaTokenType(String tokenTypeAsString,
181+
String keyword) {
162182
if (!ALPHA.matcher(keyword).find()) {
163-
return;
183+
return "";
164184
}
165185

166186
if (KNOWN_TOKEN_TYPES.containsKey(tokenTypeAsString)) {
@@ -170,6 +190,7 @@ private void parseRSyntaxTextAreaTokenType(String tokenTypeAsString, String keyw
170190
keywordTokenType.put(keyword, TokenTypes.FUNCTION);
171191
keywordTokenTypeAsString.put(keyword, "FUNCTION");
172192
}
193+
return keywordTokenTypeAsString.get(keyword);
173194
}
174195

175196
private void parseHTMLReferenceFileName(String piece, String keyword) {
@@ -190,4 +211,8 @@ public String getTokenTypeAsString(String keyword) {
190211
public int getTokenType(char[] array, int start, int end) {
191212
return keywordTokenType.get(array, start, end);
192213
}
214+
215+
public ArrayList<KeywordModel> getArrayKeyWords() {
216+
return keywords;
217+
}
193218
}

0 commit comments

Comments
 (0)