From 4c03a64d8671b57459bfc5767227f86ffb9c1373 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Fri, 12 Jan 2018 20:49:42 +0300 Subject: [PATCH] Uast-Groovy: better `uastAnchor`-s (IDEA-CR-28371) --- .../plugins/groovy/lang/psi/uast/GrUClass.kt | 8 +++--- .../plugins/groovy/uast/GroovyUastApiTest.kt | 26 +++++++++++++++++++ .../groovy/testdata/uast/SimpleClass.groovy | 2 +- .../groovy/testdata/uast/SimpleClass.log.txt | 1 + .../testdata/uast/SimpleClass.render.txt | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/uast/GroovyUastApiTest.kt diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/uast/GrUClass.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/uast/GrUClass.kt index 321a8170aee3..56fbcaf533b5 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/uast/GrUClass.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/uast/GrUClass.kt @@ -32,7 +32,7 @@ class GrUClass(val grElement: GrTypeDefinition, parentProvider: () -> UElement?) } override val uastAnchor: UElement? - get() = UIdentifier(psi.nameIdentifier, this) + get() = UIdentifier(grElement.nameIdentifierGroovy, this) override val uastParent by lazy(parentProvider) @@ -67,7 +67,7 @@ class GrUMethod(val grElement: GrMethod, parentProvider: () -> UElement?) : UMet override val isOverride: Boolean by lazy { psi.modifierList.findAnnotation("java.lang.Override") != null } override val uastAnchor: UElement? - get() = UIdentifier((psi.originalElement as? PsiNameIdentifierOwner)?.nameIdentifier ?: psi.nameIdentifier, this) + get() = UIdentifier(grElement.nameIdentifierGroovy, this) override val annotations: List by lazy { grAnnotations(grElement.modifierList, this) } @@ -87,14 +87,14 @@ class GrUParameter(val grElement: GrParameter, override val psi = javaPsi override val uastInitializer by lazy { - val initializer = psi.initializer ?: return@lazy null + val initializer = grElement.initializerGroovy ?: return@lazy null getLanguagePlugin().convertElement(initializer, this) as? UExpression } override val typeReference: UTypeReferenceExpression? = null //not implemented override val uastAnchor: UElement - get() = UIdentifier(psi.nameIdentifier, this) + get() = UIdentifier(grElement.nameIdentifierGroovy, this) override val annotations: List by lazy { grAnnotations(grElement.modifierList, this) } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/uast/GroovyUastApiTest.kt b/plugins/groovy/test/org/jetbrains/plugins/groovy/uast/GroovyUastApiTest.kt new file mode 100644 index 000000000000..25eb76c8d3f9 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/uast/GroovyUastApiTest.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package org.jetbrains.plugins.groovy.uast + +import junit.framework.TestCase +import org.jetbrains.uast.UFile +import org.junit.Test + +class GroovyUastApiTest : AbstractGroovyUastTest() { + override fun check(testName: String, file: UFile) { + } + + @Test + fun testUastAnchors() { + doTest("SimpleClass.groovy") { name, file -> + val uClass = file.classes.single { it.qualifiedName == "SimpleClass" } + TestCase.assertEquals("SimpleClass", uClass.uastAnchor?.psi?.text) + val uMethod = uClass.methods.single { it.name == "bar" } + TestCase.assertEquals("bar", uMethod.uastAnchor?.psi?.text) + val uParameter = uMethod.uastParameters.single() + TestCase.assertEquals("param", uParameter.uastAnchor?.psi?.text) + } + } + +} \ No newline at end of file diff --git a/plugins/groovy/testdata/uast/SimpleClass.groovy b/plugins/groovy/testdata/uast/SimpleClass.groovy index 4e379339fce8..21edaf77b146 100644 --- a/plugins/groovy/testdata/uast/SimpleClass.groovy +++ b/plugins/groovy/testdata/uast/SimpleClass.groovy @@ -8,5 +8,5 @@ class SimpleClass { @java.lang.Deprecated def foo() { return field } - String bar() { return "abc$field" } + String bar(int param) { return "abc$field$param" } } diff --git a/plugins/groovy/testdata/uast/SimpleClass.log.txt b/plugins/groovy/testdata/uast/SimpleClass.log.txt index 9f37e99fdf79..2efd1559c997 100644 --- a/plugins/groovy/testdata/uast/SimpleClass.log.txt +++ b/plugins/groovy/testdata/uast/SimpleClass.log.txt @@ -5,3 +5,4 @@ UFile (package = ) UMethod (name = foo) UAnnotation (fqName = java.lang.Deprecated) UMethod (name = bar) + UParameter (name = param) diff --git a/plugins/groovy/testdata/uast/SimpleClass.render.txt b/plugins/groovy/testdata/uast/SimpleClass.render.txt index cc3c7fe47bf1..cf477ec13141 100644 --- a/plugins/groovy/testdata/uast/SimpleClass.render.txt +++ b/plugins/groovy/testdata/uast/SimpleClass.render.txt @@ -4,5 +4,5 @@ public abstract annotation SimpleAnnotation { public class SimpleClass { @java.lang.Deprecated public fun foo() : java.lang.Object = UastEmptyExpression - public fun bar() : java.lang.String = UastEmptyExpression + public fun bar(param: int) : java.lang.String = UastEmptyExpression }