From ee62d318ee49efbfebb8386c36bb0c17ad0af64b Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 25 Dec 2013 20:01:07 +0400 Subject: [PATCH] Fixed bug in reference shortening --- .../jet/plugin/codeInsight/ShortenReferences.kt | 16 +++++++++++++--- .../specifyType/afterClassNameClashing.kt | 4 +++- idea/testData/shortenRefs/type/GenericType3.kt | 1 + .../shortenRefs/type/GenericType3.kt.after | 3 +++ .../shortenRefs/ShortenRefsTestGenerated.java | 5 +++++ 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 idea/testData/shortenRefs/type/GenericType3.kt create mode 100644 idea/testData/shortenRefs/type/GenericType3.kt.after diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt index ef364777b62..4914a549d6a 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt @@ -43,12 +43,12 @@ public object ShortenReferences { } override fun visitJetElement(element : JetElement) { - element.acceptChildren(this) + acceptChildren(element) } override fun visitUserType(userType: JetUserType) { val resultElement = processType(userType) - resultElement.acceptChildren(this) + acceptChildren(resultElement) } private fun processType(userType: JetUserType): PsiElement { @@ -88,7 +88,7 @@ public object ShortenReferences { override fun visitDotQualifiedExpression(expression: JetDotQualifiedExpression) { val resultElement = processDotQualifiedExpression(expression) - resultElement.acceptChildren(this) + acceptChildren(resultElement) } private fun processDotQualifiedExpression(qualifiedExpression: JetDotQualifiedExpression): PsiElement { @@ -180,6 +180,16 @@ public object ShortenReferences { private fun addImportIfNeeded(descriptor : DeclarationDescriptor) { ImportInsertHelper.addImportDirectiveIfNeeded(DescriptorUtils.getFqNameSafe(descriptor), file) } + + // we do not use standard PsiElement.acceptChildren because it won't work correctly if the element is replaced by the visitor + private fun acceptChildren(element: PsiElement) { + var child = element.getFirstChild() + while(child != null) { + val nextChild = child!!.getNextSibling() + child!!.accept(this) + child = nextChild + } + } } private fun referenceExpression(selectorExpression: JetExpression) = if (selectorExpression is JetCallExpression) diff --git a/idea/testData/intentions/specifyType/afterClassNameClashing.kt b/idea/testData/intentions/specifyType/afterClassNameClashing.kt index c8a23b0fff4..5eb6842a07f 100644 --- a/idea/testData/intentions/specifyType/afterClassNameClashing.kt +++ b/idea/testData/intentions/specifyType/afterClassNameClashing.kt @@ -1,5 +1,7 @@ // "Specify type explicitly" "true" +import java.sql + fun getEntry() : Map.Entry, java.sql.Array> { } -val x: Map.Entry, java.sql.Array> = getEntry() \ No newline at end of file +val x: Map.Entry, sql.Array> = getEntry() \ No newline at end of file diff --git a/idea/testData/shortenRefs/type/GenericType3.kt b/idea/testData/shortenRefs/type/GenericType3.kt new file mode 100644 index 00000000000..884e7545de6 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType3.kt @@ -0,0 +1 @@ +val x: jet.Map.Entry, java.sql.Array> \ No newline at end of file diff --git a/idea/testData/shortenRefs/type/GenericType3.kt.after b/idea/testData/shortenRefs/type/GenericType3.kt.after new file mode 100644 index 00000000000..327c6170359 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType3.kt.after @@ -0,0 +1,3 @@ +import java.sql + +val x: Map.Entry, sql.Array> \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java index abd11013060..023423b0273 100644 --- a/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java @@ -126,6 +126,11 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest { doTest("idea/testData/shortenRefs/type/GenericType2.kt"); } + @TestMetadata("GenericType3.kt") + public void testGenericType3() throws Exception { + doTest("idea/testData/shortenRefs/type/GenericType3.kt"); + } + @TestMetadata("LeaveQualified.kt") public void testLeaveQualified() throws Exception { doTest("idea/testData/shortenRefs/type/LeaveQualified.kt");