From 00b6839e591fa294dd91918cd196b64d67a8e1f5 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 26 Jul 2016 17:49:18 +0300 Subject: [PATCH] Introduce Variable: Retain entered name after changing "Specify type explicitly" option #KT-13128 Fixed --- ChangeLog.md | 1 + .../introduce/AbstractKotlinInplaceIntroducer.kt | 9 +++++++++ .../KotlinVariableInplaceIntroducer.kt | 13 ++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index f393485f91c..abc414ea50a 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -245,6 +245,7 @@ Using 'this' as function argument in constructor of non-final class - [`KT-12943`](https://youtrack.jetbrains.com/issue/KT-12943) Rename: Show function signatures in "Rename Overloads" dialog - [`KT-13157`](https://youtrack.jetbrains.com/issue/KT-13157) Extract Function: Automatically quote function name if necessary - [`KT-13010`](https://youtrack.jetbrains.com/issue/KT-13010) Extract Function: Fix generation of destructuring declarations +- [`KT-13128`](https://youtrack.jetbrains.com/issue/KT-13128) Introduce Variable: Retain entered name after changing "Specify type explicitly" option ##### New features diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractKotlinInplaceIntroducer.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractKotlinInplaceIntroducer.kt index 6c696169c91..c1076d48243 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractKotlinInplaceIntroducer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractKotlinInplaceIntroducer.kt @@ -24,6 +24,8 @@ import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer import com.intellij.refactoring.rename.inplace.InplaceRefactoring import com.intellij.util.ui.FormBuilder import org.jetbrains.kotlin.idea.KotlinFileType +import org.jetbrains.kotlin.idea.core.KotlinNameSuggester +import org.jetbrains.kotlin.idea.core.quoteIfNeeded import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.psi.* @@ -62,6 +64,13 @@ abstract class AbstractKotlinInplaceIntroducer( } } + protected fun updateVariableName() { + val currentName = inputName.quoteIfNeeded() + if (KotlinNameSuggester.isIdentifier(currentName)) { + localVariable.setName(currentName) + } + } + override fun getActionName(): String? = null override fun restoreExpression( diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinVariableInplaceIntroducer.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinVariableInplaceIntroducer.kt index 5a42f6e7a21..37a9eaffca1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinVariableInplaceIntroducer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinVariableInplaceIntroducer.kt @@ -28,6 +28,8 @@ import com.intellij.psi.PsiFile import com.intellij.psi.PsiReference import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil import com.intellij.ui.NonFocusableCheckBox +import org.jetbrains.kotlin.idea.core.KotlinNameSuggester +import org.jetbrains.kotlin.idea.core.quoteIfNeeded import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractKotlinInplaceIntroducer import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers @@ -39,6 +41,8 @@ import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.types.KotlinType +import java.util.* +import javax.swing.JCheckBox class KotlinVariableInplaceIntroducer( val addedVariable: KtProperty, @@ -61,6 +65,7 @@ class KotlinVariableInplaceIntroducer( editor ) { private val suggestedNames = suggestedNames.toTypedArray() + private lateinit var expressionTypeCheckBox: JCheckBox init { initFormComponents { @@ -81,11 +86,12 @@ class KotlinVariableInplaceIntroducer( } if (expressionType != null && !noTypeInference) { - val expressionTypeCheckBox = NonFocusableCheckBox("Specify type explicitly") + expressionTypeCheckBox = NonFocusableCheckBox("Specify type explicitly") expressionTypeCheckBox.isSelected = false expressionTypeCheckBox.setMnemonic('t') expressionTypeCheckBox.addActionListener { runWriteCommandAndRestart { + updateVariableName() if (expressionTypeCheckBox.isSelected) { val renderedType = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(expressionType) addedVariable.setTypeReference(KtPsiFactory(myProject).createType(renderedType)) @@ -117,6 +123,8 @@ class KotlinVariableInplaceIntroducer( stringUsages: Collection>, scope: PsiElement, containingFile: PsiFile): Boolean { + myNameSuggestions = myNameSuggestions.mapTo(LinkedHashSet(), String::quoteIfNeeded) + myEditor.caretModel.moveToOffset(nameIdentifier!!.startOffset) val result = super.buildTemplateAndStart(refs, stringUsages, scope, containingFile) @@ -129,7 +137,10 @@ class KotlinVariableInplaceIntroducer( return result } + override fun getInitialName() = super.getInitialName().quoteIfNeeded() + override fun updateTitle(variable: KtProperty?, value: String?) { + expressionTypeCheckBox.isEnabled = value == null || KotlinNameSuggester.isIdentifier(value) // No preview to update }