-
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathCompletionCandidate.java
More file actions
345 lines (277 loc) · 10.1 KB
/
Copy pathCompletionCandidate.java
File metadata and controls
345 lines (277 loc) · 10.1 KB
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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-18 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
// TODO when building the label in some variants in this file,
// getReturnType2() is used instead of getReturnType().
// need to check whether that's identical in how it performs,
// and if so, use makeLabel() and makeCompletion() more [fry 180326]
// https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FMethodDeclaration.html
public class CompletionCandidate implements Comparable<CompletionCandidate> {
private final String elementName;
private final String label; // the toString value
private final String completion;
private final Object wrappedObject;
private final int type;
static final int PREDEF_CLASS = 0;
static final int PREDEF_FIELD = 1;
static final int PREDEF_METHOD = 2;
static final int LOCAL_CLASS = 3;
static final int LOCAL_METHOD = 4;
static final int LOCAL_FIELD = 5;
static final int LOCAL_VAR = 6;
CompletionCandidate(Method method) {
// return value ignored? [fry 180326]
//method.getDeclaringClass().getName();
elementName = method.getName();
label = makeLabel(method);
completion = makeCompletion(method);
type = PREDEF_METHOD;
wrappedObject = method;
}
CompletionCandidate(SingleVariableDeclaration svd) {
completion = svd.getName().toString();
elementName = svd.getName().toString();
type = (svd.getParent() instanceof FieldDeclaration) ?
LOCAL_FIELD : LOCAL_VAR;
label = svd.getName() + " : " + svd.getType();
wrappedObject = svd;
}
CompletionCandidate(VariableDeclarationFragment vdf) {
completion = vdf.getName().toString();
elementName = vdf.getName().toString();
type = (vdf.getParent() instanceof FieldDeclaration) ?
LOCAL_FIELD : LOCAL_VAR;
label = vdf.getName() + " : " + CompletionGenerator.extracTypeInfo2(vdf);
wrappedObject = vdf;
}
CompletionCandidate(MethodDeclaration method) {
elementName = method.getName().toString();
type = LOCAL_METHOD;
@SuppressWarnings("unchecked")
List<ASTNode> params = (List<ASTNode>)
method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY);
{ // label
StringBuilder labelBuilder = new StringBuilder(elementName);
labelBuilder.append('(');
for (int i = 0; i < params.size(); i++) {
labelBuilder.append(params.get(i).toString());
if (i < params.size() - 1) {
labelBuilder.append(',');
}
}
labelBuilder.append(')');
if (method.getReturnType2() != null) {
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType2());
}
label = labelBuilder.toString();
}
{ // completion
StringBuilder compBuilder = new StringBuilder(elementName);
compBuilder.append('(');
for (int i = 0; i < params.size(); i++) {
if (i < params.size() - 1) {
compBuilder.append(',');
}
}
if (params.size() == 1) {
compBuilder.append(' ');
}
compBuilder.append(')');
completion = compBuilder.toString();
}
wrappedObject = method;
}
CompletionCandidate(TypeDeclaration td) {
type = LOCAL_CLASS;
elementName = td.getName().toString();
label = elementName;
completion = elementName;
wrappedObject = td;
}
CompletionCandidate(Field f) {
//f.getDeclaringClass().getName();
elementName = f.getName();
type = PREDEF_FIELD;
label = "<html>" +
f.getName() + " : " +
f.getType().getSimpleName() + " - " +
"<font color=#777777>" +
f.getDeclaringClass().getSimpleName() +
"</font></html>";
completion = elementName;
wrappedObject = f;
}
CompletionCandidate(String elementName, String label,
String completion, int type) {
this(elementName, label, completion, type, null);
}
private CompletionCandidate(String elementName, String label,
String completion, int type,
Object wrappedObject) {
this.elementName = elementName;
this.label = label;
this.completion = completion;
this.type = type;
this.wrappedObject = wrappedObject;
}
public String getElementName() {
return elementName;
}
public String getCompletionString() {
return completion;
}
public int getType() {
return type;
}
public String getLabel() {
return label;
}
CompletionCandidate withLabelAndCompString(String withLabel,
String withCompletion) {
return new CompletionCandidate(elementName,
withLabel, withCompletion,
type, wrappedObject);
}
CompletionCandidate withRegeneratedCompString() {
if (wrappedObject instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration)wrappedObject;
@SuppressWarnings("unchecked")
List<ASTNode> params = (List<ASTNode>)
method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY);
// build the html label
StringBuilder labelBuilder = new StringBuilder(elementName);
labelBuilder.append('(');
for (int i = 0; i < params.size(); i++) {
labelBuilder.append(params.get(i));
if (i < params.size() - 1) {
labelBuilder.append(',');
}
}
labelBuilder.append(')');
if (method.getReturnType2() != null) {
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType2());
}
// build the completion str
StringBuilder compBuilder = new StringBuilder();
compBuilder.append(method.getName());
compBuilder.append('(');
for (int i = 0; i < params.size(); i++) {
if (i < params.size() - 1) {
compBuilder.append(',');
}
}
if (params.size() == 1) {
compBuilder.append(' ');
}
compBuilder.append(')');
return withLabelAndCompString(labelBuilder.toString(), compBuilder.toString());
} else if (wrappedObject instanceof Method) {
Method method = (Method) wrappedObject;
Class<?>[] types = method.getParameterTypes();
// build html label
StringBuilder labelBuilder = new StringBuilder();
labelBuilder.append("<html>");
labelBuilder.append(method.getName());
labelBuilder.append('(');
for (int i = 0; i < types.length; i++) {
labelBuilder.append(types[i].getSimpleName());
if (i < types.length - 1) {
labelBuilder.append(',');
}
}
labelBuilder.append(')');
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
labelBuilder.append(" - <font color=#777777>");
labelBuilder.append(method.getDeclaringClass().getSimpleName());
labelBuilder.append("</font>");
labelBuilder.append("</html>");
// make completion string
StringBuilder compBuilder = new StringBuilder(method.getName());
compBuilder.append('(');
for (int i = 0; i < types.length; i++) {
if (i < types.length - 1) {
compBuilder.append(',');
}
}
if (types.length == 1) {
compBuilder.append(' ');
}
compBuilder.append(')');
return withLabelAndCompString(labelBuilder.toString(), compBuilder.toString());
}
// fall-through silently does nothing? [fry 180326]
return this;
}
static private String makeLabel(Method method) {
Class<?>[] types = method.getParameterTypes();
StringBuilder labelBuilder = new StringBuilder();
labelBuilder.append("<html>");
labelBuilder.append(method.getName());
labelBuilder.append('(');
for (int i = 0; i < types.length; i++) {
labelBuilder.append(types[i].getSimpleName());
if (i < types.length - 1) {
labelBuilder.append(',');
}
}
labelBuilder.append(")");
labelBuilder.append(" : ");
labelBuilder.append(method.getReturnType().getSimpleName());
labelBuilder.append(" - <font color=#777777>");
labelBuilder.append(method.getDeclaringClass().getSimpleName());
labelBuilder.append("</font>");
labelBuilder.append("</html>");
return labelBuilder.toString();
}
static private String makeCompletion(Method method) {
Class<?>[] types = method.getParameterTypes();
StringBuilder compBuilder = new StringBuilder();
compBuilder.append(method.getName());
compBuilder.append('(');
for (int i = 0; i < types.length; i++) {
if (i < types.length - 1) {
compBuilder.append(','); // wtf? [fry 180326]
}
}
if (types.length == 1) {
compBuilder.append(' ');
}
compBuilder.append(')');
return compBuilder.toString();
}
@Override
public int compareTo(CompletionCandidate cc) {
if (type != cc.getType()) {
return cc.getType() - type;
}
return elementName.compareTo(cc.getElementName());
}
public String toString() {
return label;
}
}