This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
ParameterContextInformation.java
395 lines (353 loc) · 11.2 KB
/
ParameterContextInformation.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.xbase.ui.contentassist;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtext.ui.editor.contentassist.ISmartContextInformation;
import com.google.common.collect.Lists;
/**
* This class is mainly adopted from {@link org.eclipse.jdt.internal.ui.text.java.ProposalContextInformation}
* and friends.
* @see org.eclipse.jdt.internal.ui.text.java.JavaParameterListValidator
* @author Sebastian Zarnekow - Initial contribution and API
* @noextend This class is not intended to be subclassed by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
*/
@SuppressWarnings("restriction")
public class ParameterContextInformation implements ISmartContextInformation {
private final ParameterData data;
private final String contextDisplayString;
private int parameterListOffset;
private int currentParameter = -1;
private int initialCaretOffset;
public ParameterContextInformation(ParameterData data, String contextDisplayString, int parameterListOffset, int initialCarretOffset) {
this.data = data;
this.contextDisplayString = contextDisplayString;
this.parameterListOffset = parameterListOffset;
this.initialCaretOffset = initialCarretOffset;
}
@Override
public boolean equals(Object object) {
if (object instanceof IContextInformation) {
IContextInformation contextInformation= (IContextInformation) object;
boolean equals= getInformationDisplayString().equalsIgnoreCase(contextInformation.getInformationDisplayString());
if (getContextDisplayString() != null)
equals= equals && getContextDisplayString().equalsIgnoreCase(contextInformation.getContextDisplayString());
return equals;
}
return false;
}
@Override
public int hashCode() {
int low= getContextDisplayString() != null ? getContextDisplayString().hashCode() : 0;
return (getInformationDisplayString().hashCode() << 16) | low;
}
@Override
public String getInformationDisplayString() {
return data.getDisplayString();
}
public List<String> internalGetInformationDisplayString() {
return data.getRawDisplayString();
}
@Override
public Image getImage() {
// TODO implement
return null;
}
@Override
public String getContextDisplayString() {
return contextDisplayString;
}
@Override
public int getContextInformationPosition() {
return initialCaretOffset;
}
@Override
public void setContextInformationPosition(int pos) {
this.initialCaretOffset = pos;
}
@Override
public boolean isContextInformationValid(ITextViewer viewer, int position) {
try {
if (position < this.parameterListOffset)
return false;
IDocument document= viewer.getDocument();
IRegion line= document.getLineInformationOfOffset(this.parameterListOffset);
if (position < line.getOffset() || position >= document.getLength())
return false;
return getCharCount(document, this.parameterListOffset, position, "(<", ")>", false) >= 0; //$NON-NLS-1$ //$NON-NLS-2$
} catch (BadLocationException x) {
return false;
}
}
@Override
public boolean updatePresentation(ITextViewer viewer, int position, TextPresentation presentation) {
int currentParameter= -1;
try {
currentParameter= getCharCount(viewer.getDocument(), this.parameterListOffset, position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$
} catch (BadLocationException x) {
return false;
}
if (currentParameter != -1) {
if (currentParameter == this.currentParameter)
return false;
}
presentation.clear();
this.currentParameter= currentParameter;
List<String> rawStrings = internalGetInformationDisplayString();
List<int[]> commaArrays = Lists.newArrayList();
for(String s: rawStrings) {
commaArrays.add(computeCommaPositions(s));
}
int offset = 0;
for(int i = 0; i < rawStrings.size(); i++) {
String raw = rawStrings.get(i);
int[] commas = commaArrays.get(i);
if (commas.length - 2 < this.currentParameter && !data.isVarArgs(i)) {
presentation.addStyleRange(new StyleRange(offset, raw.length(), JFaceResources.getColorRegistry().get(JFacePreferences.QUALIFIER_COLOR), null, SWT.NORMAL));
} else {
int actualParameter = this.currentParameter;
if (actualParameter + 1 >= commas.length)
actualParameter = commas.length - 2;
int start= commas[actualParameter] + 1;
int end= commas[actualParameter + 1];
if (start > 0)
presentation.addStyleRange(new StyleRange(offset, start, null, null, SWT.NORMAL));
if (end > start)
presentation.addStyleRange(new StyleRange(offset + start, end - start, null, null, SWT.BOLD));
if (end < raw.length())
presentation.addStyleRange(new StyleRange(offset + end, raw.length() - end, null, null, SWT.NORMAL));
}
offset += raw.length() + 1;
}
return true;
}
protected int[] computeCommaPositions(String code) {
final int length= code.length();
int pos= 0;
List<Integer> positions= new ArrayList<Integer>();
positions.add(new Integer(-1));
while (pos < length && pos != -1) {
char ch= code.charAt(pos);
switch (ch) {
case ',':
positions.add(new Integer(pos));
break;
case '<':
pos = advance(code, length, pos, '<', '>');
break;
case '[':
pos = advance(code, length, pos, '[', ']');
break;
case '(':
pos = advance(code, length, pos, '(', ')');
break;
default:
break;
}
if (pos != -1)
pos++;
}
positions.add(new Integer(length));
int[] fields= new int[positions.size()];
for (int i= 0; i < fields.length; i++)
fields[i]= positions.get(i).intValue();
return fields;
}
protected int advance(String code, final int length, int pos, char left, char right) {
int remaining = 1;
pos++;
while(pos < length && pos != -1 && remaining > 0) {
char ch2 = code.charAt(pos);
if (ch2 == left)
remaining++;
else if (ch2 == right)
remaining--;
pos++;
}
return pos;
}
// see JavaParameterListValidator
protected int getCharCount(IDocument document, final int start, final int end, String increments, String decrements, boolean considerNesting) throws BadLocationException {
Assert.isTrue((increments.length() != 0 || decrements.length() != 0) && !increments.equals(decrements));
final int NONE= 0;
final int BRACKET= 1;
final int BRACE= 2;
final int PAREN= 3;
final int ANGLE= 4;
int nestingMode= NONE;
int nestingLevel= 0;
int charCount= 0;
int offset= start;
while (offset < end) {
char curr= document.getChar(offset++);
switch (curr) {
case '/':
if (offset < end) {
char next= document.getChar(offset);
if (next == '*') {
// a comment starts, advance to the comment end
offset= getCommentEnd(document, offset + 1, end);
} else if (next == '/') {
// '//'-comment: nothing to do anymore on this line
int nextLine= document.getLineOfOffset(offset) + 1;
if (nextLine == document.getNumberOfLines())
offset= end;
else
offset= document.getLineOffset(nextLine);
}
}
break;
case '*':
if (offset < end) {
char next= document.getChar(offset);
if (next == '/') {
// we have been in a comment: forget what we read before
charCount= 0;
++ offset;
}
}
break;
case '"':
case '\'':
offset= getStringEnd(document, offset, end, curr);
break;
case '[':
if (considerNesting) {
if (nestingMode == BRACKET || nestingMode == NONE) {
nestingMode= BRACKET;
nestingLevel++;
}
break;
}
//$FALL-THROUGH$
case ']':
if (considerNesting) {
if (nestingMode == BRACKET)
if (--nestingLevel == 0)
nestingMode= NONE;
break;
}
//$FALL-THROUGH$
case '(':
if (considerNesting) {
if (nestingMode == ANGLE) {
// generics heuristic failed
nestingMode=PAREN;
nestingLevel= 1;
}
if (nestingMode == PAREN || nestingMode == NONE) {
nestingMode= PAREN;
nestingLevel++;
}
break;
}
//$FALL-THROUGH$
case ')':
if (considerNesting) {
if (nestingMode == PAREN)
if (--nestingLevel == 0)
nestingMode= NONE;
break;
}
//$FALL-THROUGH$
case '{':
if (considerNesting) {
if (nestingMode == ANGLE) {
// generics heuristic failed
nestingMode=BRACE;
nestingLevel= 1;
}
if (nestingMode == BRACE || nestingMode == NONE) {
nestingMode= BRACE;
nestingLevel++;
}
break;
}
//$FALL-THROUGH$
case '}':
if (considerNesting) {
if (nestingMode == BRACE)
if (--nestingLevel == 0)
nestingMode= NONE;
break;
}
//$FALL-THROUGH$
case '<':
if (considerNesting) {
if (nestingMode == ANGLE || nestingMode == NONE && checkGenericsHeuristic(document, offset - 1, start - 1)) {
nestingMode= ANGLE;
nestingLevel++;
}
break;
}
//$FALL-THROUGH$
case '>':
if (considerNesting) {
if (nestingMode == ANGLE)
if (--nestingLevel == 0)
nestingMode= NONE;
break;
}
//$FALL-THROUGH$
default:
if (nestingLevel != 0)
continue;
if (increments.indexOf(curr) >= 0) {
++ charCount;
}
if (decrements.indexOf(curr) >= 0) {
-- charCount;
}
}
}
return charCount;
}
protected int getCommentEnd(IDocument d, int pos, int end) throws BadLocationException {
while (pos < end) {
char curr= d.getChar(pos);
pos++;
if (curr == '*') {
if (pos < end && d.getChar(pos) == '/') {
return pos + 1;
}
}
}
return end;
}
protected int getStringEnd(IDocument d, int pos, int end, char ch) throws BadLocationException {
while (pos < end) {
char curr= d.getChar(pos);
pos++;
if (curr == '\\') {
// ignore escaped characters
pos++;
} else if (curr == ch) {
return pos;
}
}
return end;
}
protected boolean checkGenericsHeuristic(IDocument document, int end, int bound) {
JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
return scanner.looksLikeClassInstanceCreationBackward(end, bound);
}
}