From 838cfc7dca1455e803072cd9af42d93d5be924fc Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 27 Jul 2016 14:48:26 +0300 Subject: [PATCH] Make constructor parameter a property: select 'private' to be able to remove it #KT-13187 Fixed (cherry picked from commit dc8c195) --- .../quickfix/MakeConstructorParameterPropertyFix.kt | 10 +++++++--- .../makeConstructorParameterProperty/inner.kt.after | 4 ++-- .../makeConstructorParameterProperty/val.kt.after | 4 ++-- .../makeConstructorParameterProperty/var.kt.after | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeConstructorParameterPropertyFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeConstructorParameterPropertyFix.kt index af1b409fb11..90501f01404 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeConstructorParameterPropertyFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MakeConstructorParameterPropertyFix.kt @@ -28,9 +28,7 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtNameReferenceExpression import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.psiUtil.containingClass -import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS -import org.jetbrains.kotlin.psi.psiUtil.nonStaticOuterClasses +import org.jetbrains.kotlin.psi.psiUtil.* class MakeConstructorParameterPropertyFix( element: KtParameter, private val kotlinValVar: KotlinValVar, className: String? @@ -49,6 +47,12 @@ class MakeConstructorParameterPropertyFix( override fun invoke(project: Project, editor: Editor?, file: KtFile) { element.addBefore(kotlinValVar.createKeyword(KtPsiFactory(project))!!, element.firstChild) element.addModifier(KtTokens.PRIVATE_KEYWORD) + element.visibilityModifier()?.let { private -> + editor?.apply { + selectionModel.setSelection(private.startOffset, private.endOffset) + caretModel.moveToOffset(private.endOffset) + } + } } companion object Factory : KotlinIntentionActionsFactory() { diff --git a/idea/testData/quickfix/makeConstructorParameterProperty/inner.kt.after b/idea/testData/quickfix/makeConstructorParameterProperty/inner.kt.after index 9b82e6bdf9d..8480bbf4ae2 100644 --- a/idea/testData/quickfix/makeConstructorParameterProperty/inner.kt.after +++ b/idea/testData/quickfix/makeConstructorParameterProperty/inner.kt.after @@ -1,10 +1,10 @@ // "Make constructor parameter a property in class 'B'" "true" -class B(private val bar: String) { +class B(private val bar: String) { inner class A { fun foo() { - val a = bar + val a = bar } } } \ No newline at end of file diff --git a/idea/testData/quickfix/makeConstructorParameterProperty/val.kt.after b/idea/testData/quickfix/makeConstructorParameterProperty/val.kt.after index 84cdbe58369..4b09e8de887 100644 --- a/idea/testData/quickfix/makeConstructorParameterProperty/val.kt.after +++ b/idea/testData/quickfix/makeConstructorParameterProperty/val.kt.after @@ -1,7 +1,7 @@ // "Make constructor parameter a property" "true" -class A(private val foo: String) { +class A(private val foo: String) { fun bar() { - val a = foo + val a = foo } } \ No newline at end of file diff --git a/idea/testData/quickfix/makeConstructorParameterProperty/var.kt.after b/idea/testData/quickfix/makeConstructorParameterProperty/var.kt.after index 708ab4fe53d..a48e19d3a98 100644 --- a/idea/testData/quickfix/makeConstructorParameterProperty/var.kt.after +++ b/idea/testData/quickfix/makeConstructorParameterProperty/var.kt.after @@ -1,7 +1,7 @@ // "Make constructor parameter a property" "true" -class A(private var foo: String) { +class A(private var foo: String) { fun bar() { - foo = "" + foo = "" } } \ No newline at end of file