matthiasblaesing commented on code in PR #8834:
URL: https://github.com/apache/netbeans/pull/8834#discussion_r2512043833


##########
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:
   Reproducible problem with this full source:
   
   ```java
   public class AA {
       public static void main(String[] args) {
           int n1 = 10 >> 1;
           int n2 = 10>> 1;
       }
   }
   ```
   
   The issue is, that the javac gives us kind `INT_LITERAL` when you query it 
for the position of the third `>` (the first in the fourth line).
   
   @jherkel quick idea for a test would be:
   
   
   ```java
       @Test
       public void testAngleBrackets() throws Exception {
           // original tests
           assertHasNoOrigin("int n1 = 10 ^>> 1;");
           assertHasNoOrigin("int n1 = 10 >^> 1;");
           assertHasNoOrigin("int n1 = 10 >>^ 1;");
           assertHasNoOrigin("int n1 = 10^>> 1;"); // This is the only failing 
one here
           assertHasNoOrigin("int n1 = 10>^> 1;");
           assertHasNoOrigin("int n1 = 10>>^ 1;");
           // test other esoteric variants
       }
   
       /**
        * Pass a method body as {@code angleStr}, where the caret position is
        * marked by {@code ^}. The method checks, that the
        * JavaBracesMatcher#findOrigin returns null.
        *
        * @param angleStr
        * @throws Exception 
        */
       private void assertHasNoOrigin(String angleStr) throws Exception {
           testNumber++;
           String srcTmp = makeTestClass(angleStr);
           int caretPos = srcTmp.indexOf('^');
           String sourceCode = srcTmp.substring(0, caretPos) + 
srcTmp.substring(caretPos + 1);
           FileObject wd = FileUtil.toFileObject(getWorkDir());
           FileObject sourceDir = FileUtil.createFolder(wd, "src");
           FileObject buildDir = FileUtil.createFolder(wd, "build");
           FileObject cacheFolder = FileUtil.createFolder(wd, "cache");
           Paths.get(cacheFolder.toURI()).toFile().mkdirs();
           FileObject testFO = FileUtil.createData(sourceDir, "test/Test" + 
testNumber + ".java");
           TestUtilities.copyStringToFile(testFO, sourceCode);
           SourceUtilsTestUtil.prepareTest(sourceDir, buildDir, cacheFolder);
           JavaSource source = JavaSource.forFileObject(testFO);
           assertNotNull(source);
           DataObject od = DataObject.find(testFO);
           EditorCookie ec = od.getCookie(EditorCookie.class);
           Document doc = ec.openDocument();
           doc.putProperty(Language.class, JavaTokenId.language());
           doc.putProperty("mimeType", JavaKit.JAVA_MIME_TYPE);
           BracesMatcherFactory factory = new JavaBracesMatcher();
           MatcherContext context = 
BracesMatchingTestUtils.createMatcherContext(doc, caretPos, false, 1);
           BracesMatcher matcher = factory.createMatcher(context);
           assertNull(matcher.findOrigin());
       }
   ```



-- 
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

Reply via email to