2525package processing .app .syntax ;
2626
2727import 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 ;
2831import org .fife .ui .rsyntaxtextarea .TokenMap ;
2932import org .fife .ui .rsyntaxtextarea .TokenTypes ;
3033import processing .app .Base ;
3639import java .io .File ;
3740import java .io .FileInputStream ;
3841import java .io .InputStreamReader ;
42+ import java .util .ArrayList ;
43+ import java .util .Collection ;
3944import java .util .HashMap ;
45+ import java .util .LinkedList ;
46+ import java .util .List ;
4047import java .util .Map ;
4148import java .util .regex .Pattern ;
4249
43-
4450public 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,\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 );
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