KotlinIntroduceVariableHandler: cleanup code

This commit is contained in:
Dmitry Gridin
2020-04-22 13:59:26 +07:00
parent b574917314
commit d21e383094
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -74,15 +74,12 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
private class TypeCheckerImpl(private val project: Project) : KotlinTypeChecker by KotlinTypeChecker.DEFAULT { private class TypeCheckerImpl(private val project: Project) : KotlinTypeChecker by KotlinTypeChecker.DEFAULT {
private inner class ContextImpl : ClassicTypeCheckerContext(false) { private inner class ContextImpl : ClassicTypeCheckerContext(false) {
override fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean { override fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean =
return compareDescriptors(project, a.declarationDescriptor, b.declarationDescriptor) compareDescriptors(project, a.declarationDescriptor, b.declarationDescriptor)
}
} }
override fun equalTypes(a: KotlinType, b: KotlinType): Boolean { override fun equalTypes(a: KotlinType, b: KotlinType): Boolean = with(NewKotlinTypeChecker.Default) {
return with(NewKotlinTypeChecker.Default) { ContextImpl().equalTypes(a.unwrap(), b.unwrap())
ContextImpl().equalTypes(a.unwrap(), b.unwrap())
}
} }
} }
@@ -105,9 +102,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
var reference: SmartPsiElementPointer<KtExpression>? = null var reference: SmartPsiElementPointer<KtExpression>? = null
val references = ArrayList<SmartPsiElementPointer<KtExpression>>() val references = ArrayList<SmartPsiElementPointer<KtExpression>>()
private fun findElementByOffsetAndText(offset: Int, text: String, newContainer: PsiElement): PsiElement? { private fun findElementByOffsetAndText(offset: Int, text: String, newContainer: PsiElement): PsiElement? =
return newContainer.findElementAt(offset)?.parentsWithSelf?.firstOrNull { (it as? KtExpression)?.text == text } newContainer.findElementAt(offset)?.parentsWithSelf?.firstOrNull { (it as? KtExpression)?.text == text }
}
private fun replaceExpression(expressionToReplace: KtExpression, addToReferences: Boolean): KtExpression { private fun replaceExpression(expressionToReplace: KtExpression, addToReferences: Boolean): KtExpression {
val isActualExpression = expression == expressionToReplace val isActualExpression = expression == expressionToReplace
@@ -294,7 +290,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
allReplaces allReplaces
) )
commonContainer.bodyExpression.sure { "Original body is not found: " + commonContainer } commonContainer.bodyExpression.sure { "Original body is not found: $commonContainer" }
expression.putCopyableUserData(EXPRESSION_KEY, true) expression.putCopyableUserData(EXPRESSION_KEY, true)
for (replace in allReplaces) { for (replace in allReplaces) {
@@ -304,8 +300,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val newDeclaration = ConvertToBlockBodyIntention.convert(commonContainer) val newDeclaration = ConvertToBlockBodyIntention.convert(commonContainer)
val newCommonContainer = newDeclaration.bodyBlockExpression val newCommonContainer = newDeclaration.bodyBlockExpression.sure { "New body is not found: $newDeclaration" }
.sure { "New body is not found: " + newDeclaration }
val newExpression = newCommonContainer.findExpressionByCopyableDataAndClearIt(EXPRESSION_KEY) val newExpression = newCommonContainer.findExpressionByCopyableDataAndClearIt(EXPRESSION_KEY)
val newCommonParent = newCommonContainer.findElementByCopyableDataAndClearIt(COMMON_PARENT_KEY) val newCommonParent = newCommonContainer.findElementByCopyableDataAndClearIt(COMMON_PARENT_KEY)
@@ -322,18 +317,19 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
private fun calculateAnchor(commonParent: PsiElement, commonContainer: PsiElement, allReplaces: List<KtExpression>): PsiElement? { private fun calculateAnchor(commonParent: PsiElement, commonContainer: PsiElement, allReplaces: List<KtExpression>): PsiElement? {
if (commonParent != commonContainer) return commonParent.parentsWithSelf.firstOrNull { it.parent == commonContainer } if (commonParent != commonContainer) return commonParent.parentsWithSelf.firstOrNull { it.parent == commonContainer }
val startOffset = allReplaces.fold(commonContainer.endOffset) { offset, expr ->
min(offset, expr.substringContextOrThis.startOffset)
}
val startOffset = return commonContainer.allChildren.lastOrNull { it.textRange.contains(startOffset) }
allReplaces.fold(commonContainer.endOffset) { offset, expr -> min(offset, expr.substringContextOrThis.startOffset) }
return commonContainer.allChildren.lastOrNull { it.textRange.contains(startOffset) } ?: return null
} }
private fun PsiElement.isAssignmentLHS(): Boolean { private fun PsiElement.isAssignmentLHS(): Boolean = parents.any {
return parents.any { KtPsiUtil.isAssignment(it) && (it as KtBinaryExpression).left == this } KtPsiUtil.isAssignment(it) && (it as KtBinaryExpression).left == this
} }
private fun KtExpression.findOccurrences(occurrenceContainer: PsiElement): List<KtExpression> { private fun KtExpression.findOccurrences(occurrenceContainer: PsiElement): List<KtExpression> =
return toRange().match(occurrenceContainer, KotlinPsiUnifier.DEFAULT).mapNotNull { toRange().match(occurrenceContainer, KotlinPsiUnifier.DEFAULT).mapNotNull {
val candidate = it.range.elements.first() val candidate = it.range.elements.first()
if (candidate.isAssignmentLHS()) return@mapNotNull null if (candidate.isAssignmentLHS()) return@mapNotNull null
@@ -344,7 +340,6 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
else -> throw AssertionError("Unexpected candidate element: " + candidate.text) else -> throw AssertionError("Unexpected candidate element: " + candidate.text)
} }
} }
}
private fun KtExpression.shouldReplaceOccurrence(bindingContext: BindingContext, container: PsiElement?): Boolean { private fun KtExpression.shouldReplaceOccurrence(bindingContext: BindingContext, container: PsiElement?): Boolean {
val effectiveParent = (parent as? KtScriptInitializer)?.parent ?: parent val effectiveParent = (parent as? KtScriptInitializer)?.parent ?: parent
@@ -368,14 +363,11 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
}?.second as? KtElement }?.second as? KtElement
} }
private fun KtContainerNode.isBadContainerNode(place: PsiElement): Boolean { private fun KtContainerNode.isBadContainerNode(place: PsiElement): Boolean = when (val parent = parent) {
val parent = parent is KtIfExpression -> parent.condition == place
return when (parent) { is KtLoopExpression -> parent.body != place
is KtIfExpression -> parent.condition == place is KtArrayAccessExpression -> true
is KtLoopExpression -> parent.body != place else -> false
is KtArrayAccessExpression -> true
else -> false
}
} }
private fun KtExpression.getOccurrenceContainer(): KtElement? { private fun KtExpression.getOccurrenceContainer(): KtElement? {
@@ -383,7 +375,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
for ((place, parent) in parentsWithSelf.zip(parents)) { for ((place, parent) in parentsWithSelf.zip(parents)) {
when { when {
parent is KtContainerNode && place !is KtBlockExpression && !parent.isBadContainerNode(place) -> result = parent parent is KtContainerNode && place !is KtBlockExpression && !parent.isBadContainerNode(place) -> result = parent
parent is KtClassBody || parent is KtFile -> return if (result == null) parent as KtElement else result parent is KtClassBody || parent is KtFile -> return result ?: parent as? KtElement
parent is KtBlockExpression -> result = parent parent is KtBlockExpression -> result = parent
parent is KtWhenEntry && place !is KtBlockExpression -> result = parent parent is KtWhenEntry && place !is KtBlockExpression -> result = parent
parent is KtDeclarationWithBody && parent.bodyExpression == place && place !is KtBlockExpression -> result = parent parent is KtDeclarationWithBody && parent.bodyExpression == place && place !is KtBlockExpression -> result = parent
@@ -437,20 +429,15 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
editor, editor,
builder.buildInlineTemplate(), builder.buildInlineTemplate(),
object : TemplateEditingAdapter() { object : TemplateEditingAdapter() {
private fun finishMarkAction() { private fun finishMarkAction() = FinishMarkAction.finish(project, editor, startMarkAction)
FinishMarkAction.finish(project, editor, startMarkAction)
}
override fun templateFinished(template: Template, brokenOff: Boolean) { override fun templateFinished(template: Template, brokenOff: Boolean) {
if (!brokenOff) { if (!brokenOff) postProcess(declaration)
postProcess(declaration)
}
finishMarkAction() finishMarkAction()
} }
override fun templateCancelled(template: Template?) { override fun templateCancelled(template: Template?) = finishMarkAction()
finishMarkAction()
}
} }
) )
} }
@@ -668,14 +655,11 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val project = file.project val project = file.project
return references.all { return references.all {
val originalDescriptor = originalContext[BindingContext.REFERENCE_TARGET, it] val originalDescriptor = originalContext[BindingContext.REFERENCE_TARGET, it]
val newDescriptor = newContext[BindingContext.REFERENCE_TARGET, it] if (originalDescriptor is ValueParameterDescriptor && (originalContext[BindingContext.AUTO_CREATED_IT, originalDescriptor] == true)) {
if (originalDescriptor is ValueParameterDescriptor
&& (originalContext[BindingContext.AUTO_CREATED_IT, originalDescriptor] ?: false)
) {
return@all originalDescriptor.containingDeclaration.source.getPsi().isAncestor(neighbour, true) return@all originalDescriptor.containingDeclaration.source.getPsi().isAncestor(neighbour, true)
} }
val newDescriptor = newContext[BindingContext.REFERENCE_TARGET, it]
compareDescriptors(project, newDescriptor, originalDescriptor) compareDescriptors(project, newDescriptor, originalDescriptor)
} }
} }