From 6ff776ba650f29b28a127c86864b1dbc8f498a6e Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 15 May 2019 11:44:49 +0900 Subject: [PATCH] Convert property to function: fix incorrectly conversion when property has explicit generic type #KT-29192 Fixed --- .../idea/intentions/ConvertPropertyToFunctionIntention.kt | 5 +++++ .../expressionBodyWithGenericType.kt | 6 ++++++ .../expressionBodyWithGenericType.kt.after | 5 +++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 4 files changed, 21 insertions(+) create mode 100644 idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt create mode 100644 idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt index c583d5a8011..d3f29941aa6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt @@ -75,6 +75,11 @@ class ConvertPropertyToFunctionIntention : SelfTargetingIntention(Kt .first { it !is PsiWhiteSpace } .nextSibling property.deleteChildRange(dropPropertyFrom, getter.prevSibling) + + val typeReference = property.typeReference + if (typeReference != null) { + property.addAfter(psiFactory.createWhiteSpace(), typeReference) + } } } property.setName(newName) diff --git a/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt b/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt new file mode 100644 index 00000000000..ace3789e046 --- /dev/null +++ b/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt @@ -0,0 +1,6 @@ +class Test { + val foo: Foo + get() = Foo() +} + +class Foo \ No newline at end of file diff --git a/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt.after b/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt.after new file mode 100644 index 00000000000..910b14e3bfe --- /dev/null +++ b/idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt.after @@ -0,0 +1,5 @@ +class Test { + fun getFoo(): Foo = Foo() +} + +class Foo \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 7050220355e..c677e94b0c7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -6614,6 +6614,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/convertPropertyToFunction/expressionBody.kt"); } + @TestMetadata("expressionBodyWithGenericType.kt") + public void testExpressionBodyWithGenericType() throws Exception { + runTest("idea/testData/intentions/convertPropertyToFunction/expressionBodyWithGenericType.kt"); + } + @TestMetadata("initializer.kt") public void testInitializer() throws Exception { runTest("idea/testData/intentions/convertPropertyToFunction/initializer.kt");