-
Notifications
You must be signed in to change notification settings - Fork 82
/
JavaPairMatcher.java
250 lines (224 loc) · 9.09 KB
/
JavaPairMatcher.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
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Christian Plesner Hansen (plesner@quenta.org) - changed implementation to use DefaultCharacterPairMatcher
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.text.IJavaPartitions;
/**
* Helper class for match pairs of characters.
*/
public final class JavaPairMatcher extends DefaultCharacterPairMatcher implements ISourceVersionDependent {
/**
* Stores the source version state.
* @since 3.1
*/
private boolean fHighlightAngularBrackets= false;
public JavaPairMatcher(char[] pairs) {
super(pairs, IJavaPartitions.JAVA_PARTITIONING, true);
}
/* @see ICharacterPairMatcher#match(IDocument, int) */
@Override
public IRegion match(IDocument document, int offset) {
try {
return performMatch(document, offset);
} catch (BadLocationException ble) {
return null;
}
}
/*
* Performs the actual work of matching for #match(IDocument, int).
*/
private IRegion performMatch(IDocument document, int offset) throws BadLocationException {
if (offset < 0 || document == null) return null;
char prevChar= document.getChar(Math.max(offset - 1, 0));
char currChar= (offset != document.getLength()) ? document.getChar(offset) : Character.MIN_VALUE;
if (prevChar == '>' && currChar != '>') { //https://bugs.eclipse.org/bugs/show_bug.cgi?id=372516
offset--;
currChar= prevChar;
prevChar= document.getChar(Math.max(offset - 1, 0));
} else if (currChar == '<' && (prevChar != '>' && prevChar != '<')) {
offset++;
prevChar= currChar;
currChar= document.getChar(offset);
}
if ((prevChar == '<' || currChar == '>') && !fHighlightAngularBrackets)
return null;
if (prevChar == '<' && isLessThanOperator(document, offset - 1))
return null;
final IRegion region= super.match(document, offset);
if (region == null) return region;
if (currChar == '>') {
final int peer= region.getOffset();
if (isLessThanOperator(document, peer)) return null;
}
return region;
}
/**
* Returns <code>true</code> if the character at the specified offset is a less-than sign, rather than
* the opening angle bracket of a type parameter list.
*
* @param document a document
* @param offset an offset within the document
* @return <code>true</code> if the character at the specified offset is a less-than sign
* @throws BadLocationException if offset is invalid in the document
*/
private boolean isLessThanOperator(IDocument document, int offset) throws BadLocationException {
if (offset < 0) return false;
String contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
if (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
return false;
}
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document, IJavaPartitions.JAVA_PARTITIONING, contentType);
return !isTypeParameterOpeningBracket(offset, document, scanner);
}
/**
* Checks if the angular bracket at <code>offset</code> is a type parameter opening bracket.
*
* @param offset the offset of the opening bracket
* @param document the document
* @param scanner a java heuristic scanner on <code>document</code>
* @return <code>true</code> if the bracket is part of a type parameter, <code>false</code>
* otherwise
* @since 3.1
*/
private boolean isTypeParameterOpeningBracket(int offset, IDocument document, JavaHeuristicScanner scanner) {
/*
* type parameter come after braces (closing or opening), semicolons, or after
* a Type name (heuristic: starts with capital character), or after a modifier
* keyword in a method declaration (visibility, static, synchronized, final)
*/
try {
IRegion line= document.getLineInformationOfOffset(offset);
int prevToken= scanner.previousToken(offset - 1, line.getOffset());
int prevTokenOffset= scanner.getPosition() + 1;
String previous= prevToken == Symbols.TokenEOF ? null : document.get(prevTokenOffset, offset - prevTokenOffset).trim();
if ( prevToken == Symbols.TokenLBRACE
|| prevToken == Symbols.TokenRBRACE
|| prevToken == Symbols.TokenSEMICOLON
|| prevToken == Symbols.TokenSYNCHRONIZED
|| prevToken == Symbols.TokenSTATIC
|| (prevToken == Symbols.TokenIDENT && isTypeParameterIntroducer(previous))
|| prevToken == Symbols.TokenEOF)
return true;
} catch (BadLocationException e) {
return false;
}
return false;
}
/**
* Returns true if the character at the specified offset is a greater-than sign, rather than an
* type parameter list close angle bracket.
*
* @param document a document
* @param offset an offset within the document
* @return true if the character at the specified offset is a greater-than sign
* @throws BadLocationException if offset is invalid in the document
*/
private boolean isGreaterThanOperator(IDocument document, int offset) throws BadLocationException {
if (offset < 0)
return false;
String contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, false);
if (!IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
return false;
}
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document, IJavaPartitions.JAVA_PARTITIONING, contentType);
return !isTypeParameterClosingBracket(offset, document, scanner);
}
/**
* Checks if the angular bracket at <code>offset</code> is a type parameter closing bracket.
*
* @param offset the offset of the closing bracket
* @param document the document
* @param scanner a java heuristic scanner on <code>document</code>
* @return <code>true</code> if the bracket is part of a type parameter, <code>false</code>
* otherwise
* @since 3.8
*/
private boolean isTypeParameterClosingBracket(int offset, IDocument document, JavaHeuristicScanner scanner) {
/*
* type parameter closing brackets come after question marks, other type parameter
* closing brackets, or after a Type name (heuristic: starts with capital character)
*/
try {
IRegion line= document.getLineInformationOfOffset(offset);
int prevToken= scanner.previousToken(offset - 1, line.getOffset());
int prevTokenOffset= scanner.getPosition() + 1;
String previous= prevToken == Symbols.TokenEOF ? null : document.get(prevTokenOffset, offset - prevTokenOffset).trim();
if ((prevToken == Symbols.TokenIDENT && (previous != null && previous.length() > 0 && Character.isUpperCase(previous.charAt(0))))
|| prevToken == Symbols.TokenEOF
|| prevToken == Symbols.TokenGREATERTHAN
|| prevToken == Symbols.TokenQUESTIONMARK)
return true;
} catch (BadLocationException e) {
return false;
}
return false;
}
/**
* Returns <code>true</code> if <code>identifier</code> is an identifier
* that could come right before a type parameter list. It uses a heuristic:
* if the identifier starts with an upper case, it is assumed a type name.
* Also, if <code>identifier</code> is a method modifier, it is assumed
* that the angular bracket is part of the generic type parameter of a
* method.
*
* @param identifier the identifier to check
* @return <code>true</code> if the identifier could introduce a type
* parameter list
* @since 3.1
*/
private boolean isTypeParameterIntroducer(String identifier) {
return identifier.length() > 0
&& (Character.isUpperCase(identifier.charAt(0))
|| identifier.startsWith("final") //$NON-NLS-1$
|| identifier.startsWith("public") //$NON-NLS-1$
|| identifier.startsWith("protected") //$NON-NLS-1$
|| identifier.startsWith("private")); //$NON-NLS-1$
}
/*
* @see org.eclipse.jdt.internal.ui.text.ISourceVersionDependent#setSourceVersion(java.lang.String)
*/
@Override
public void setSourceVersion(String version) {
if (JavaCore.compareJavaVersions(JavaCore.VERSION_1_5, version) <= 0)
fHighlightAngularBrackets= true;
else
fHighlightAngularBrackets= false;
}
/*
* @see org.eclipse.jface.text.source.ICharacterPairMatcherExtension#isMatchedChar(char, org.eclipse.jface.text.IDocument, int)
*/
@Override
public boolean isMatchedChar(char ch, IDocument document, int offset) {
try {
if (ch == '<') {
if (isLessThanOperator(document, offset)) {
return false;
}
} else if (ch == '>') {
if (isGreaterThanOperator(document, offset)) {
return false;
}
}
} catch (BadLocationException e) {
// do nothing
}
return super.isMatchedChar(ch, document, offset);
}
}