You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by en...@apache.org on 2022/05/27 09:12:02 UTC

[netbeans] branch master updated: Correction of getOwningClass(), including a test.

This is an automated email from the ASF dual-hosted git repository.

entl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new ef8fcd7339 Correction of getOwningClass(), including a test.
ef8fcd7339 is described below

commit ef8fcd7339e69845c646aa028672c4b26f13a3ef
Author: Martin Entlicher <ma...@oracle.com>
AuthorDate: Thu May 26 00:31:47 2022 +0200

    Correction of getOwningClass(), including a test.
---
 .../modules/groovy/editor/api/ASTUtils.java        |  2 +-
 .../editor/language/GroovyDeclarationFinder.java   | 16 ++++++++-------
 .../declarationfinder/ClassWithInner.groovy        | 23 ++++++++++++++++++++++
 .../editor/api/GroovyDeclarationFinderTest.java    | 15 ++++++++++++++
 4 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/ASTUtils.java b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/ASTUtils.java
index eee0861bef..04f0c78807 100644
--- a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/ASTUtils.java
+++ b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/ASTUtils.java
@@ -528,7 +528,7 @@ public class ASTUtils {
     }
 
     public static ClassNode getOwningClass(AstPath path) {
-        Iterator<ASTNode> it = path.rootToLeaf();
+        Iterator<ASTNode> it = path.leafToRoot();
         while (it.hasNext()) {
             ASTNode node = it.next();
             if (node instanceof ClassNode) {
diff --git a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/language/GroovyDeclarationFinder.java b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/language/GroovyDeclarationFinder.java
index 14e21c6097..2bd81a8b7d 100644
--- a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/language/GroovyDeclarationFinder.java
+++ b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/language/GroovyDeclarationFinder.java
@@ -265,10 +265,7 @@ public class GroovyDeclarationFinder implements DeclarationFinder {
                 if (scope != null) {
                     ASTNode variable = ASTUtils.getVariable(scope, variableExpression.getName(), path, doc, lexOffset);
                     if (variable != null) {
-                        // I am using getRange and not getOffset, because getRange is adding 'def_' to offset of field
-                        int offset = ASTUtils.getRange(variable, doc).getStart();
-                        // FIXME parsing API
-                        return new DeclarationLocation(info.getSnapshot().getSource().getFileObject(), offset);
+                        return getVariableLocation(variable, doc, info);
                     }
                 }
             // find a field ?
@@ -289,9 +286,7 @@ public class GroovyDeclarationFinder implements DeclarationFinder {
                         }
                         ASTNode variable = ASTUtils.getVariable(scope, ((ConstantExpression) property).getText(), path, doc, lexOffset);
                         if (variable != null) {
-                            int offset = ASTUtils.getOffset(doc, variable.getLineNumber(), variable.getColumnNumber());
-                            // FIXME parsing API
-                            return new DeclarationLocation(info.getSnapshot().getSource().getFileObject(), offset);
+                            return getVariableLocation(variable, doc, info);
                         }
                     } else {
                         // find variable type
@@ -384,6 +379,13 @@ public class GroovyDeclarationFinder implements DeclarationFinder {
         return DeclarationLocation.NONE;
     }
 
+    private DeclarationLocation getVariableLocation(ASTNode variable, BaseDocument doc, ParserResult info) {
+        // I am using getRange and not getOffset, because getRange is adding 'def_' to offset of field
+        int offset = ASTUtils.getRange(variable, doc).getStart();
+        // FIXME parsing API
+        return new DeclarationLocation(info.getSnapshot().getSource().getFileObject(), offset);
+    }
+
     private DeclarationLocation findType(String fqName, OffsetRange range,
             BaseDocument doc, ParserResult info, GroovyIndex index) throws BadLocationException {
 
diff --git a/groovy/groovy.editor/test/unit/data/testfiles/declarationfinder/ClassWithInner.groovy b/groovy/groovy.editor/test/unit/data/testfiles/declarationfinder/ClassWithInner.groovy
new file mode 100644
index 0000000000..0c397e3bfc
--- /dev/null
+++ b/groovy/groovy.editor/test/unit/data/testfiles/declarationfinder/ClassWithInner.groovy
@@ -0,0 +1,23 @@
+class ClassWithInner {
+
+    String x = "A"
+    String y = "B"
+
+    class Inner {
+
+        Point x = new Point();
+
+        def method() {
+            this.x = null
+            y.isEmpty()
+        }
+    }
+
+    def method() {
+        this.x.isNumber()
+    }
+
+    class Point {
+        int x, y
+    }
+}
diff --git a/groovy/groovy.editor/test/unit/src/org/netbeans/modules/groovy/editor/api/GroovyDeclarationFinderTest.java b/groovy/groovy.editor/test/unit/src/org/netbeans/modules/groovy/editor/api/GroovyDeclarationFinderTest.java
index 852984f947..45b981cfe0 100644
--- a/groovy/groovy.editor/test/unit/src/org/netbeans/modules/groovy/editor/api/GroovyDeclarationFinderTest.java
+++ b/groovy/groovy.editor/test/unit/src/org/netbeans/modules/groovy/editor/api/GroovyDeclarationFinderTest.java
@@ -180,4 +180,19 @@ public class GroovyDeclarationFinderTest extends GroovyTestBase {
         checkDeclaration(TEST_BASE + "Annotations.groovy",
                 "    @Annot^ation public String method() {}", "Annotation.java", 30);
     }
+
+    public void testGroovyClassInner1() throws Exception {
+        checkDeclaration(TEST_BASE + "ClassWithInner.groovy",
+                "            this.^x = null", "ClassWithInner.groovy", 96);
+    }
+
+    public void testGroovyClassInner2() throws Exception {
+        checkDeclaration(TEST_BASE + "ClassWithInner.groovy",
+                "            ^y.isEmpty()", "ClassWithInner.groovy", 54);
+    }
+
+    public void testGroovyClassInner3() throws Exception {
+        checkDeclaration(TEST_BASE + "ClassWithInner.groovy",
+                "        this.^x.isNumber()", "ClassWithInner.groovy", 35);
+    }
 }


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

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