mbien commented on code in PR #8834:
URL: https://github.com/apache/netbeans/pull/8834#discussion_r2508410975
##########
java/java.editor/src/org/netbeans/modules/editor/java/JavaBracesMatcher.java:
##########
@@ -139,20 +153,42 @@ public int[] findMatches() throws InterruptedException,
BadLocationException {
TokenSequence<?> seq = sequences.get(sequences.size() - 1);
TokenHierarchy<Document> th =
TokenHierarchy.get(context.getDocument());
- List<TokenSequence<?>> list;
- if (backward) {
- list = th.tokenSequenceList(seq.languagePath(), 0,
originOffset);
- } else {
- list = th.tokenSequenceList(seq.languagePath(),
originOffset + 1, context.getDocument().getLength());
- }
+ List<TokenSequence<?>> list = null;
int counter = 0;
-
seq.move(originOffset);
- if (seq.moveNext()) {
+ boolean seqMoved = seq.moveNext();
+ if (seqMoved) {
+ Token<?> token = seq.token();
+ if (containsToken(token.id(),
GENERIC_CANDIDATE_TOKEN_IDS)) {
+ // for a token with length > 1 we need to change the
token sequence list because we want to process GTGT or GTGTGT tokens only once.
+ // Also we need to change the counter for a multichar
token because we need to consider a cursor position within the multichar token
+ if (token.id().equals(JavaTokenId.GTGT)) {
+ if (backward) {
+ list =
th.tokenSequenceList(seq.languagePath(), 0, originOffset - token.length() + 1);
+ }
+ counter += originOffset - seq.offset();
+ } else if (token.id().equals(JavaTokenId.GTGTGT)) {
+ if (backward) {
+ list =
th.tokenSequenceList(seq.languagePath(), 0, originOffset - token.length() + 1);
+ }
+ counter += originOffset - seq.offset();
+ }
Review Comment:
both branches are the same?
given this snippet:
```
int n1 = 10 >> 1;
int n2 = 10>> 1;
int n3 = 10 >>> 1;
int n4 = 10>>> 1;
```
n2 and n4 would try to match opening brackets, while n1 and n3 work which
have a space before the first `>`.
<img width="152" height="56" alt="image"
src="https://github.com/user-attachments/assets/a75d4871-dfa5-45b5-b6f4-7cfa03ea3cd3"
/>
also reproducible with the `<<` counterpart
##########
java/java.editor/src/org/netbeans/modules/editor/java/JavaBracesMatcher.java:
##########
@@ -243,40 +279,120 @@ public int[] findMatches() throws InterruptedException,
BadLocationException {
}
}
}
- return null;
+ return null;
}
}
- JavaTokenId originId = getTokenId(originChar);
- JavaTokenId lookingForId = getTokenId(matchingChar);
+ JavaTokenId[] originId = getTokenId(originChar);
+ JavaTokenId[] lookingForId = getTokenId(matchingChar);
for(TokenSequenceIterator tsi = new
TokenSequenceIterator(list, backward); tsi.hasMore(); ) {
TokenSequence<?> sq = tsi.getSequence();
-
- if (originId == sq.token().id()) {
- counter++;
- } else if (lookingForId == sq.token().id()) {
- if (counter == 0) {
- matchStart = sq.offset();
- return new int [] { sq.offset(), sq.offset() +
sq.token().length() };
+ if (containsToken(sq.token().id(), originId)) {
+ if (containsToken(sq.token().id(),
GENERIC_CANDIDATE_TOKEN_IDS)) {
+ if (sq.token().id().equals(JavaTokenId.GTGT)) {
+ counter += 2;
+ } else if
(sq.token().id().equals(JavaTokenId.GTGTGT)) {
+ counter += 3;
+ } else {
+ counter++;
+ }
+ } else {
+ counter++;
+ }
+ } else if (containsToken(sq.token().id(), lookingForId)) {
+ if (containsToken(sq.token().id(),
GENERIC_CANDIDATE_TOKEN_IDS)) {
+ boolean multipledGT = false;
+ if (counter == 0) {
+ matchStart = sq.offset();
+ return new int[]{sq.offset(), sq.offset() + 1};
+ } else {
+ if (sq.token().id().equals(JavaTokenId.GTGT)) {
+ multipledGT = true;
+ counter -= 2;
+ } else if
(sq.token().id().equals(JavaTokenId.GTGTGT)) {
+ multipledGT = true;
+ counter -= 3;
+ } else {
+ counter--;
+ }
+ // we need to check the counter again for a
multichar token >> or >>> because we can have the counter < 0
+ // i.e for example
Map<String,Map<Integer,List<String>>> will have counter = -1
+ if (multipledGT && counter <= 0) {
+ matchStart = sq.offset();
+ return new int[]{sq.offset() +
sq.token().length() + counter, sq.offset() + sq.token().length() + counter + 1};
+ }
+ }
} else {
- counter--;
+ if (counter == 0) {
+ matchStart = sq.offset();
+ return new int[]{sq.offset(), sq.offset() +
sq.token().length()};
+ } else {
+ counter--;
+ }
}
+
}
}
}
-
return null;
} finally {
((AbstractDocument) context.getDocument()).readUnlock();
}
}
-
+
+ /**
+ * Check if token is java generic.
+ *
+ * @param doc
+ * @param srcOffset
+ * @return true if token is java generic.
+ */
+ private boolean isPartOfGeneric(Document doc, final int srcOffset) {
+ JavaSource javaSource = JavaSource.forDocument(doc);
+ if (javaSource == null) {
+ return false;
+ }
+ final AtomicBoolean valid = new AtomicBoolean();
+ try {
+ javaSource.runUserActionTask(new Task<CompilationController>() {
+ @Override
+ public void run(CompilationController ctrl) throws Exception {
Review Comment:
feel free to use a lambda if you want, a bit less noisy
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists