Introduce Variable: Retain entered name after changing "Specify type explicitly" option

#KT-13128 Fixed
This commit is contained in:
Alexey Sedunov
2016-07-26 17:49:18 +03:00
parent 792177f206
commit 00b6839e59
3 changed files with 22 additions and 1 deletions
+1
View File
@@ -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
@@ -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<D: KtNamedDeclaration>(
}
}
protected fun updateVariableName() {
val currentName = inputName.quoteIfNeeded()
if (KotlinNameSuggester.isIdentifier(currentName)) {
localVariable.setName(currentName)
}
}
override fun getActionName(): String? = null
override fun restoreExpression(
@@ -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<Pair<PsiElement, TextRange>>,
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
}