You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/09/15 16:37:41 UTC

[GitHub] geertjanw closed pull request #879: Fixing TreeUtilities.pathFor for for-each's expressions.

geertjanw closed pull request #879: Fixing TreeUtilities.pathFor for for-each's expressions.
URL: https://github.com/apache/incubator-netbeans/pull/879
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
index dabee6847b..4532d426cb 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
@@ -381,14 +381,16 @@ public Void visitMethod(MethodTree node, Void p) {
             @Override
             public Void visitEnhancedForLoop(EnhancedForLoopTree node, Void p) {
                 int exprEndPos = (int) sourcePositions.getEndPosition(getCurrentPath().getCompilationUnit(), node.getExpression());
-                TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()).subSequence(exprEndPos, pos);
-                boolean hasNonWhiteSpace;
-                while (hasNonWhiteSpace = ts.moveNext()) {
-                    if (!IGNORE_TOKENS.contains(ts.token().id()))
-                        break;
-                }
-                if (!hasNonWhiteSpace) {
-                    pos = exprEndPos;
+                if (exprEndPos < pos) {
+                    TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()).subSequence(exprEndPos, pos);
+                    boolean hasNonWhiteSpace;
+                    while (hasNonWhiteSpace = ts.moveNext()) {
+                        if (!IGNORE_TOKENS.contains(ts.token().id()))
+                            break;
+                    }
+                    if (!hasNonWhiteSpace) {
+                        pos = exprEndPos;
+                    }
                 }
                 return super.visitEnhancedForLoop(node, p);
             }
diff --git a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
index f555807157..e52a0967f8 100644
--- a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
+++ b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
@@ -22,7 +22,9 @@
 import com.sun.source.tree.BlockTree;
 import com.sun.source.tree.ClassTree;
 import com.sun.source.tree.ExpressionTree;
+import com.sun.source.tree.IdentifierTree;
 import com.sun.source.tree.MemberSelectTree;
+import com.sun.source.tree.MethodInvocationTree;
 import com.sun.source.tree.MethodTree;
 import com.sun.source.tree.Scope;
 import com.sun.source.tree.StatementTree;
@@ -643,4 +645,24 @@ public void testIsPartOfCompoundVariableDeclaration() throws Exception {
         assertFalse(info.getTreeUtilities().isPartOfCompoundVariableDeclaration(var3));
 
     }
+
+    public void testForEachLoop() throws Exception {
+        prepareTest("Test", "package test; public class Test { public Test(java.util.List<String> ll) { for (String s : ll.subList(0, ll.size())  ) { } } }");
+
+        TreePath tp1 = info.getTreeUtilities().pathFor(122 - 30);
+        assertEquals(Kind.IDENTIFIER, tp1.getLeaf().getKind());
+        assertEquals("ll", ((IdentifierTree) tp1.getLeaf()).getName().toString());
+
+        TreePath tp2 = info.getTreeUtilities().pathFor(127 - 30);
+        assertEquals(Kind.MEMBER_SELECT, tp2.getLeaf().getKind());
+        assertEquals("subList", ((MemberSelectTree) tp2.getLeaf()).getIdentifier().toString());
+
+        TreePath tp3 = info.getTreeUtilities().pathFor(140 - 30);
+        assertEquals(Kind.MEMBER_SELECT, tp3.getLeaf().getKind());
+        assertEquals("size", ((MemberSelectTree) tp3.getLeaf()).getIdentifier().toString());
+
+        TreePath tp4 = info.getTreeUtilities().pathFor(146 - 30);
+        assertEquals(Kind.METHOD_INVOCATION, tp4.getLeaf().getKind());
+        assertEquals("subList", ((MemberSelectTree) ((MethodInvocationTree) tp4.getLeaf()).getMethodSelect()).getIdentifier().toString());
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists