forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCompletionProviderWithContext.java
executable file
·376 lines (280 loc) · 12.1 KB
/
CompletionProviderWithContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* This file is part of Arduino.
*
* Copyright 2015 Ricardo JL Rufino ([email protected])
* Copyright 2015 Arduino LLC
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package cc.arduino.packages.autocomplete;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.text.JTextComponent;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.DefaultCompletionProvider;
import org.fife.ui.autocomplete.FunctionCompletion;
import org.fife.ui.autocomplete.Util;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import processing.app.helpers.Predicate;
import br.com.criativasoft.cpluslibparser.LibraryIndex;
import br.com.criativasoft.cpluslibparser.metadata.TAttribute;
import br.com.criativasoft.cpluslibparser.metadata.TClass;
import br.com.criativasoft.cpluslibparser.metadata.TClass.TClassType;
import br.com.criativasoft.cpluslibparser.metadata.TElement;
import br.com.criativasoft.cpluslibparser.metadata.TElementLocation;
import br.com.criativasoft.cpluslibparser.metadata.TFunction;
import br.com.criativasoft.cpluslibparser.metadata.TLibrary;
/**
* Autocomplete logic implementation for instance functions, static functions and local variables.
* It determines the options that will be presented in accordance with the current context.
* @author Ricardo JL Rufino ([email protected])
*/
public class CompletionProviderWithContext extends DefaultCompletionProvider{
private final static Logger LOG = Logger.getLogger(CompletionProviderWithContext.class.getName());
private static final String DOT = ".";
private static final String POINTER = "->";
private static final String STATIC = "::";
private TLibrary sketchLibrary; // classes, variables, functions of sketch
private List<Completion> sketchCompletions = new ArrayList<Completion>(); // variables and functions of sketch
public CompletionProviderWithContext() {
super();
}
public void setSketchLibrary(TLibrary sketchLibrary) {
this.sketchLibrary = sketchLibrary;
}
@Override
public List<Completion> getCompletionsImpl(JTextComponent comp) {
RSyntaxTextArea textarea = (RSyntaxTextArea) comp;
String entredText = getAlreadyEnteredText(comp);
List<Completion> contextCompletions = null;
LOG.finest("entredText = "+ entredText);
// Functions/Attrs of Classes
if (entredText != null && entredText.length() > 0) {
String separator = null;
if (entredText.contains(DOT)) separator = DOT;
if (entredText.contains(POINTER))separator = POINTER;
if (entredText.contains(STATIC)) separator = STATIC;
if (separator != null) {
String var = entredText.substring(0, entredText.lastIndexOf(separator));
String startMethod = entredText.substring(entredText.lastIndexOf(separator) + separator.length());
contextCompletions = getCompletionsForClass(var, separator, startMethod, textarea);
}
}
// Only show functions/attrs of class.
if (contextCompletions != null) return contextCompletions;
List<Completion> list = new LinkedList<Completion>();
TFunction functionScope = getCurrentFunctionScope(textarea);
if (functionScope != null && sketchCompletions != null && !sketchCompletions.isEmpty()) {
// Variables and functions
list.addAll(sketchCompletions);
// Local variables of functions.
list.addAll(getVariablesInCurrentScope(textarea));
// Need to order before searching
if(list.size() != sketchCompletions.size()){
Collections.sort(list, comparator);
}
List<Completion> filterCompletions = filterCompletions(entredText, list);
if (filterCompletions != null) list = filterCompletions;
}
List<Completion> completions = super.getCompletionsImpl(comp);
// Outside of function (only show classes)
if (functionScope == null) {
for (Completion completion : completions) {
if (!(completion instanceof FunctionCompletion)) {
list.add(completion);
}
}
} else {
list.addAll(completions);
}
return list;
}
public List<Completion> getVariablesInCurrentScope(RSyntaxTextArea textarea){
int caretPos = textarea.getCaretPosition();
TFunction functionScope = getCurrentFunctionScope(textarea);
List<Completion> completions = new ArrayList<Completion>();
if(functionScope != null){
Set<TAttribute> localVariables = functionScope.getLocalVariables();
if(localVariables != null){
for (TAttribute var : localVariables) {
if(var.getLocation().getStartOffset() <= caretPos){ // try to filter variables not declared at cursor position
// TODO: this may not work if many rows were changed after the last parser
completions.add(new TElementCompletion(this, var));
}
}
}
}
return completions;
}
public List<Completion> getCompletionsForClass(String context, String token, String startMethod, RSyntaxTextArea textarea){
List<Completion> list = new LinkedList<Completion>();
boolean isInstance = (token == POINTER || Character.isLowerCase(context.charAt(0)));
boolean isStatic = (token == STATIC && Character.isUpperCase(context.charAt(0)));
TClass tClass = null;
if(!isStatic){
// Find the correct type based on global/local variables
Set<TAttribute> variables = new HashSet<TAttribute>();
variables.addAll(sketchLibrary.getGlobalVariables());
TFunction functionScope = getCurrentFunctionScope(textarea);
if(functionScope != null){
variables.addAll(functionScope.getLocalVariables());
}
for (TAttribute attribute : variables) {
if(attribute.name().equals(context)){
tClass = LibraryIndex.getClass(attribute.getType());
isInstance = true;
break;
}
}
}
if(tClass == null){
tClass = LibraryIndex.getClass(context); // if isInstance, ignore case !!
}
if(tClass == null){
TLibrary library = LibraryIndex.getLibrary(context);
if(library != null){
TElement variable = library.findMember(context);
if(variable instanceof TAttribute){
tClass = LibraryIndex.getClass(((TAttribute) variable).getType());
if(tClass != null) isInstance = true;
}
}
}
if(tClass != null){
Collection<TFunction> functions = tClass.getFunctions(true);
Collection<TAttribute> attributes = tClass.getAttributes();
// if the user had entered part of the function/attr name
if(startMethod != null && startMethod.length() > 0){
int matchMode = (startMethod.length() > 2 ? TElementFilter.MATCH_CONTAINS : TElementFilter.MATCH_START);
functions = Predicate.filter(functions, new TFunctionFilter(startMethod, matchMode));
attributes = Predicate.filter(attributes, new TAttributeFilter(startMethod, matchMode));
}
String startTemplate = context + token;
for (TFunction tFunction : functions) {
if(!tFunction.isPublic() || tFunction.isConstructor()) continue;
boolean add = false;
// Instance
if(isInstance && !tFunction.isStatic()){
add = true;
}
// Static
if(!isInstance && tFunction.isStatic()){
add = true;
}
// Extern (show static and instance)
if(tClass.getType() == TClassType.EXTERN){
add = true;
}
if(add){
list.add(new TFunctionCompletion(this, tFunction, startTemplate));
}
}
for (TAttribute attr : attributes) {
// Instance
if(isInstance && !attr.isStatic()){
list.add(new TElementCompletion(this, attr , startTemplate));
}
// Static
if(!isInstance && attr.isStatic() || tClass.getType() == TClassType.EXTERN){
list.add(new TElementCompletion(this, attr , startTemplate));
}
}
}
return list;
}
public void setSketchCompletions(List<Completion> sketchCompletions) {
this.sketchCompletions = sketchCompletions;
Collections.sort(sketchCompletions, comparator);
}
public List<Completion> getSketchCompletions() {
return sketchCompletions;
}
/**
* Find function at the caret position
* @param textarea
* @return
*/
public TFunction getCurrentFunctionScope(RSyntaxTextArea textarea){
int caretPos = textarea.getCaretPosition();
Set<TFunction> globalFunctions = sketchLibrary.getGlobalFunctions();
for (TFunction tFunction : globalFunctions) {
TElementLocation location = tFunction.getLocation();
if(location != null && location.containsOffset(caretPos)){
LOG.finest("function = " + tFunction + ", loc: " + location.toString());
return tFunction;
}
}
return null;
}
/**
* Filter through the options that can be applied to the text that the user entered
* @param alreadyEnteredText
* @param completions - Must be ordered before searching
* @return
*/
protected List<Completion> filterCompletions(String alreadyEnteredText, List<Completion> completions) {
if(alreadyEnteredText == null || alreadyEnteredText.isEmpty()) return new ArrayList<Completion>(completions);
List<Completion> retVal = new ArrayList<Completion>();
if (alreadyEnteredText!=null) {
int index = Collections.binarySearch(completions, alreadyEnteredText, comparator);
if (index<0) { // No exact match
index = -index - 1;
}
else {
// If there are several overloads for the function being
// completed, Collections.binarySearch() will return the index
// of one of those overloads, but we must return all of them,
// so search backward until we find the first one.
int pos = index - 1;
while (pos>0 &&
comparator.compare(completions.get(pos), alreadyEnteredText)==0) {
retVal.add(completions.get(pos));
pos--;
}
}
while (index<completions.size()) {
Completion c = completions.get(index);
if (Util.startsWithIgnoreCase(c.getInputText(), alreadyEnteredText)) {
retVal.add(c);
index++;
}
else {
break;
}
}
}
return retVal;
}
@Override
protected boolean isValidChar(char ch) {
return super.isValidChar(ch) || '.' == ch || '>' == ch || '-' == ch || '<' == ch || '#' == ch || ':' == ch /**|| getParameterListStart() == ch */;
}
}