25
25
package processing .app .syntax ;
26
26
27
27
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 ;
28
31
import org .fife .ui .rsyntaxtextarea .TokenMap ;
29
32
import org .fife .ui .rsyntaxtextarea .TokenTypes ;
30
33
import processing .app .Base ;
36
39
import java .io .File ;
37
40
import java .io .FileInputStream ;
38
41
import java .io .InputStreamReader ;
42
+ import java .util .ArrayList ;
43
+ import java .util .Collection ;
39
44
import java .util .HashMap ;
45
+ import java .util .LinkedList ;
46
+ import java .util .List ;
40
47
import java .util .Map ;
41
48
import java .util .regex .Pattern ;
42
49
43
-
44
50
public class PdeKeywords {
45
51
46
52
private static final Map <String , Integer > KNOWN_TOKEN_TYPES = new HashMap <>();
@@ -65,6 +71,10 @@ public class PdeKeywords {
65
71
// lookup table that maps keywords to their html reference pages
66
72
private final Map <String , String > keywordToReference ;
67
73
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
+
68
78
public PdeKeywords () {
69
79
this .keywordTokenType = new TokenMap ();
70
80
this .keywordOldToken = new HashMap <>();
@@ -75,65 +85,76 @@ public PdeKeywords() {
75
85
/**
76
86
* Handles loading of keywords file.
77
87
* <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.
80
89
* <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.
83
91
*/
84
92
public void reload () {
85
93
try {
86
- parseKeywordsTxt (new File (BaseNoGui .getContentFile ("lib" ), "keywords.txt" ));
94
+ parseKeywordsTxt (new File (BaseNoGui .getContentFile ("lib" ),
95
+ "keywords.txt" ));
87
96
TargetPlatform tp = BaseNoGui .getTargetPlatform ();
88
97
if (tp != null ) {
89
98
File platformKeywords = new File (tp .getFolder (), "keywords.txt" );
90
- if (platformKeywords .exists ()) parseKeywordsTxt (platformKeywords );
99
+ if (platformKeywords .exists ())
100
+ parseKeywordsTxt (platformKeywords );
91
101
}
92
- for (UserLibrary lib : BaseNoGui .librariesIndexer .getInstalledLibraries ()) {
102
+ for (UserLibrary lib : BaseNoGui .librariesIndexer
103
+ .getInstalledLibraries ()) {
93
104
File keywords = new File (lib .getInstalledFolder (), "keywords.txt" );
94
105
if (keywords .exists ()) {
95
106
parseKeywordsTxt (keywords );
96
107
}
97
108
}
98
109
} catch (Exception e ) {
99
- Base .showError ("Problem loading keywords" , "Could not load keywords.txt,\n please re-install Arduino." , e );
110
+ e .printStackTrace ();
111
+
112
+ Base .showError ("Problem loading keywords" ,
113
+ "Could not load keywords.txt,\n please re-install Arduino." ,
114
+ e );
100
115
System .exit (1 );
101
116
}
102
117
}
103
118
104
119
private void parseKeywordsTxt (File input ) throws Exception {
105
120
BufferedReader reader = null ;
106
121
try {
107
- reader = new BufferedReader (new InputStreamReader (new FileInputStream (input )));
122
+ reader = new BufferedReader (
123
+ new InputStreamReader (new FileInputStream (input )));
108
124
109
125
String line ;
110
126
while ((line = reader .readLine ()) != null ) {
111
- //System.out.println("line is " + line);
127
+ // System.out.println("line is " + line);
112
128
// in case there's any garbage on the line
113
129
line = line .trim ();
114
130
if (line .length () == 0 || line .startsWith ("#" )) {
115
131
continue ;
116
132
}
117
-
118
133
String pieces [] = line .split ("\t " );
119
134
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 ("\\ #" , "#" )));
123
139
}
124
140
125
141
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 ]);
127
144
}
128
-
129
145
if (pieces .length >= 3 ) {
130
- parseHTMLReferenceFileName (pieces [2 ], keyword );
146
+ parseHTMLReferenceFileName (pieces [2 ],
147
+ keywords .get (position ).getKeyword ());
148
+ keywords .get (position ).setType ("FUNCTION" );
131
149
}
132
150
if (pieces .length >= 4 ) {
133
- parseRSyntaxTextAreaTokenType (pieces [3 ], keyword );
151
+ keywords .get (position )
152
+ .setType (parseRSyntaxTextAreaTokenType (pieces [3 ], keywords
153
+ .get (position ).getKeyword ()));
134
154
}
135
- }
136
155
156
+ position ++;
157
+ }
137
158
fillMissingTokenType ();
138
159
} finally {
139
160
IOUtils .closeQuietly (reader );
@@ -147,20 +168,19 @@ private void fillMissingTokenType() {
147
168
if (!keywordTokenTypeAsString .containsKey (keyword )) {
148
169
if ("KEYWORD1" .equals (oldTokenEntry .getValue ())) {
149
170
parseRSyntaxTextAreaTokenType ("DATA_TYPE" , keyword );
150
- }
151
- else if ("LITERAL1" .equals (oldTokenEntry .getValue ())) {
171
+ } else if ("LITERAL1" .equals (oldTokenEntry .getValue ())) {
152
172
parseRSyntaxTextAreaTokenType ("RESERVED_WORD_2" , keyword );
153
- }
154
- else {
173
+ } else {
155
174
parseRSyntaxTextAreaTokenType ("FUNCTION" , keyword );
156
175
}
157
176
}
158
177
}
159
178
}
160
179
161
- private void parseRSyntaxTextAreaTokenType (String tokenTypeAsString , String keyword ) {
180
+ private String parseRSyntaxTextAreaTokenType (String tokenTypeAsString ,
181
+ String keyword ) {
162
182
if (!ALPHA .matcher (keyword ).find ()) {
163
- return ;
183
+ return "" ;
164
184
}
165
185
166
186
if (KNOWN_TOKEN_TYPES .containsKey (tokenTypeAsString )) {
@@ -170,6 +190,7 @@ private void parseRSyntaxTextAreaTokenType(String tokenTypeAsString, String keyw
170
190
keywordTokenType .put (keyword , TokenTypes .FUNCTION );
171
191
keywordTokenTypeAsString .put (keyword , "FUNCTION" );
172
192
}
193
+ return keywordTokenTypeAsString .get (keyword );
173
194
}
174
195
175
196
private void parseHTMLReferenceFileName (String piece , String keyword ) {
@@ -190,4 +211,8 @@ public String getTokenTypeAsString(String keyword) {
190
211
public int getTokenType (char [] array , int start , int end ) {
191
212
return keywordTokenType .get (array , start , end );
192
213
}
214
+
215
+ public ArrayList <KeywordModel > getArrayKeyWords () {
216
+ return keywords ;
217
+ }
193
218
}
0 commit comments