Minor. Introduce 'isVar' parameter for KotlinIntroduceVariableHandler.doRefactoring

This commit is contained in:
Denis Zharkov
2016-07-11 11:14:31 +03:00
committed by Nikolay Krasko
parent 4d28199cc2
commit 7ba914f92a
4 changed files with 29 additions and 15 deletions
@@ -56,6 +56,8 @@ class IntroduceVariableIntention : SelfTargetingRangeIntention<PsiElement>(
override fun applyTo(element: PsiElement, editor: Editor?) { override fun applyTo(element: PsiElement, editor: Editor?) {
val expression = getExpressionToProcess(element) ?: return val expression = getExpressionToProcess(element) ?: return
KotlinIntroduceVariableHandler.doRefactoring(element.project, editor, expression, null, null) KotlinIntroduceVariableHandler.doRefactoring(
element.project, editor, expression, isVar = false, occurrencesToReplace = null, onNonInteractiveFinish = null
)
} }
} }
@@ -99,8 +99,8 @@ fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpressi
KotlinIntroduceVariableHandler.doRefactoring(project, KotlinIntroduceVariableHandler.doRefactoring(project,
editor, editor,
occurrenceInConditional, occurrenceInConditional,
listOf(occurrenceInConditional, occurrenceInThenClause), false,
null) listOf(occurrenceInConditional, occurrenceInThenClause), null)
} }
fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) { fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) {
@@ -203,9 +203,13 @@ class KotlinFunctionCallUsage(
KtSuperTypeListEntry::class.java, KtSuperTypeListEntry::class.java,
KtParameter::class.java) == null) { KtParameter::class.java) == null) {
KotlinIntroduceVariableHandler.doRefactoring(project, null, argumentExpression, listOf(argumentExpression)) { KotlinIntroduceVariableHandler.doRefactoring(
project, null, argumentExpression,
isVar = false,
occurrencesToReplace = listOf(argumentExpression),
onNonInteractiveFinish = {
argumentExpression = psiFactory.createExpression(it.name!!) argumentExpression = psiFactory.createExpression(it.name!!)
} })
} }
var expressionToReplace: KtExpression = nameCounterpartMap[ref.element] ?: continue var expressionToReplace: KtExpression = nameCounterpartMap[ref.element] ?: continue
@@ -130,7 +130,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
return result return result
} }
private fun runRefactoring ( private fun runRefactoring(
isVar: Boolean,
expression: KtExpression, expression: KtExpression,
commonContainer: PsiElement, commonContainer: PsiElement,
commonParent: PsiElement, commonParent: PsiElement,
@@ -139,16 +140,18 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val initializer = (expression as? KtParenthesizedExpression)?.expression ?: expression val initializer = (expression as? KtParenthesizedExpression)?.expression ?: expression
val initializerText = if (initializer.mustBeParenthesizedInInitializerPosition()) "(${initializer.text})" else initializer.text val initializerText = if (initializer.mustBeParenthesizedInInitializerPosition()) "(${initializer.text})" else initializer.text
val varOvVal = if (isVar) "var" else "val"
var property: KtDeclaration = if (componentFunctions.isNotEmpty()) { var property: KtDeclaration = if (componentFunctions.isNotEmpty()) {
buildString { buildString {
componentFunctions.indices.joinTo(this, prefix = "val (", postfix = ")") { nameSuggestions[it].first() } componentFunctions.indices.joinTo(this, prefix = "$varOvVal (", postfix = ")") { nameSuggestions[it].first() }
append(" = ") append(" = ")
append(initializerText) append(initializerText)
}.let { psiFactory.createDestructuringDeclaration(it) } }.let { psiFactory.createDestructuringDeclaration(it) }
} }
else { else {
buildString { buildString {
append("val ") append("$varOvVal ")
append(nameSuggestions.single().first()) append(nameSuggestions.single().first())
if (noTypeInference) { if (noTypeInference) {
val typeToRender = expressionType ?: resolutionFacade.moduleDescriptor.builtIns.anyType val typeToRender = expressionType ?: resolutionFacade.moduleDescriptor.builtIns.anyType
@@ -281,8 +284,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
} }
} }
fun runRefactoring() { fun runRefactoring(isVar: Boolean) {
if (commonContainer !is KtDeclarationWithBody) return runRefactoring(expression, commonContainer, commonParent, allReplaces) if (commonContainer !is KtDeclarationWithBody) return runRefactoring(isVar, expression, commonContainer, commonParent, allReplaces)
commonContainer.bodyExpression.sure { "Original body is not found: " + commonContainer } commonContainer.bodyExpression.sure { "Original body is not found: " + commonContainer }
@@ -306,7 +309,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
} ?: newReplace } ?: newReplace
} }
runRefactoring(newExpression, newCommonContainer, newCommonParent, newAllReplaces) runRefactoring(isVar, newExpression, newCommonContainer, newCommonParent, newAllReplaces)
} }
} }
@@ -446,6 +449,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
occurrenceContainer: KtElement, occurrenceContainer: KtElement,
resolutionFacade: ResolutionFacade, resolutionFacade: ResolutionFacade,
bindingContext: BindingContext, bindingContext: BindingContext,
isVar: Boolean,
occurrencesToReplace: List<KtExpression>?, occurrencesToReplace: List<KtExpression>?,
onNonInteractiveFinish: ((KtDeclaration) -> Unit)? onNonInteractiveFinish: ((KtDeclaration) -> Unit)?
) { ) {
@@ -555,7 +559,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
) )
project.executeCommand(INTRODUCE_VARIABLE, null) { project.executeCommand(INTRODUCE_VARIABLE, null) {
runWriteAction { introduceVariableContext.runRefactoring() } runWriteAction { introduceVariableContext.runRefactoring(isVar) }
val property = introduceVariableContext.propertyRef ?: return@executeCommand val property = introduceVariableContext.propertyRef ?: return@executeCommand
@@ -582,7 +586,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
introduceVariableContext.reference, introduceVariableContext.reference,
introduceVariableContext.references.toTypedArray(), introduceVariableContext.references.toTypedArray(),
suggestedNames.single(), suggestedNames.single(),
/*todo*/ false, isVar,
/*todo*/ false, /*todo*/ false,
expressionType, expressionType,
noTypeInference, noTypeInference,
@@ -683,6 +687,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
project: Project, project: Project,
editor: Editor?, editor: Editor?,
expressionToExtract: KtExpression?, expressionToExtract: KtExpression?,
isVar: Boolean,
occurrencesToReplace: List<KtExpression>?, occurrencesToReplace: List<KtExpression>?,
onNonInteractiveFinish: ((KtDeclaration) -> Unit)? onNonInteractiveFinish: ((KtDeclaration) -> Unit)?
) { ) {
@@ -695,7 +700,10 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val bindingContext = resolutionFacade.analyze(physicalExpression, BodyResolveMode.FULL) val bindingContext = resolutionFacade.analyze(physicalExpression, BodyResolveMode.FULL)
fun runWithChosenContainers(container: KtElement, occurrenceContainer: KtElement) { fun runWithChosenContainers(container: KtElement, occurrenceContainer: KtElement) {
doRefactoring(project, editor, expression, container, occurrenceContainer, resolutionFacade, bindingContext, occurrencesToReplace, onNonInteractiveFinish) doRefactoring(
project, editor, expression, container, occurrenceContainer, resolutionFacade, bindingContext,
isVar, occurrencesToReplace, onNonInteractiveFinish
)
} }
val candidateContainers = expression.getCandidateContainers(resolutionFacade, bindingContext).ifEmpty { val candidateContainers = expression.getCandidateContainers(resolutionFacade, bindingContext).ifEmpty {
@@ -720,7 +728,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
try { try {
selectElement(editor, file, listOf(CodeInsightUtils.ElementKind.EXPRESSION)) { selectElement(editor, file, listOf(CodeInsightUtils.ElementKind.EXPRESSION)) {
doRefactoring(project, editor, it as KtExpression?, null, null) doRefactoring(project, editor, it as KtExpression?, false, null, null)
} }
} }
catch (e: IntroduceRefactoringException) { catch (e: IntroduceRefactoringException) {