Minor: refactoring 'refactoring' package

This commit is contained in:
Dmitry Gridin
2019-05-13 18:08:10 +07:00
parent 98a4c4588e
commit 379152303c
11 changed files with 197 additions and 187 deletions
@@ -25,7 +25,7 @@ abstract class AbstractIntroduceAction : BasePlatformRefactoringAction() {
setInjectedContext(true) setInjectedContext(true)
} }
override final fun setInjectedContext(worksInInjected: Boolean) { final override fun setInjectedContext(worksInInjected: Boolean) {
super.setInjectedContext(worksInInjected) super.setInjectedContext(worksInInjected)
} }
@@ -58,8 +58,7 @@ abstract class AbstractKotlinInplaceIntroducer<D: KtNamedDeclaration>(
// myExprMarker was invalidated by stopIntroduce() // myExprMarker was invalidated by stopIntroduce()
myExprMarker = myExpr?.let { createMarker(it) } myExprMarker = myExpr?.let { createMarker(it) }
startInplaceIntroduceTemplate() startInplaceIntroduceTemplate()
} } finally {
finally {
myEditor.putUserData(InplaceRefactoring.INTRODUCE_RESTART, false) myEditor.putUserData(InplaceRefactoring.INTRODUCE_RESTART, false)
} }
} }
@@ -98,8 +98,7 @@ class ExtractSuperRefactoring(
val member = memberInfo.member ?: continue val member = memberInfo.member ?: continue
if (memberInfo.isSuperClass) { if (memberInfo.isSuperClass) {
superInterfacesToMove += member superInterfacesToMove += member
} } else {
else {
elementsToMove[member] = memberInfo elementsToMove[member] = memberInfo
} }
} }
@@ -107,7 +106,8 @@ class ExtractSuperRefactoring(
val superTypeList = originalClass.getSuperTypeList() val superTypeList = originalClass.getSuperTypeList()
if (superTypeList != null) { if (superTypeList != null) {
for (superTypeListEntry in originalClass.superTypeListEntries) { for (superTypeListEntry in originalClass.superTypeListEntries) {
val superType = superTypeListEntry.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, superTypeListEntry.typeReference] val superType =
superTypeListEntry.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, superTypeListEntry.typeReference]
?: continue ?: continue
val superClassDescriptor = superType.constructor.declarationDescriptor ?: continue val superClassDescriptor = superType.constructor.declarationDescriptor ?: continue
val superClass = DescriptorToSourceUtilsIde.getAnyDeclaration(project, superClassDescriptor) as? KtClass ?: continue val superClass = DescriptorToSourceUtilsIde.getAnyDeclaration(project, superClassDescriptor) as? KtClass ?: continue
@@ -144,8 +144,7 @@ class ExtractSuperRefactoring(
val moveTarget = if (targetParent is PsiDirectory) { val moveTarget = if (targetParent is PsiDirectory) {
val targetPackage = targetParent.getPackage() ?: return conflicts val targetPackage = targetParent.getPackage() ?: return conflicts
KotlinMoveTargetForDeferredFile(FqName(targetPackage.qualifiedName), targetParent) { null } KotlinMoveTargetForDeferredFile(FqName(targetPackage.qualifiedName), targetParent) { null }
} } else {
else {
KotlinMoveTargetForExistingElement(targetParent as KtElement) KotlinMoveTargetForExistingElement(targetParent as KtElement)
} }
val conflictChecker = MoveConflictChecker( val conflictChecker = MoveConflictChecker(
@@ -163,11 +162,11 @@ class ExtractSuperRefactoring(
ReferencesSearch.search(element).mapTo(usages) { MoveRenameUsageInfo(it, element) } ReferencesSearch.search(element).mapTo(usages) { MoveRenameUsageInfo(it, element) }
if (element is KtCallableDeclaration) { if (element is KtCallableDeclaration) {
element.toLightMethods().flatMapTo(usages) { element.toLightMethods().flatMapTo(usages) {
MethodReferencesSearch.search(it).map { MoveRenameUsageInfo(it, element) } MethodReferencesSearch.search(it).map { reference -> MoveRenameUsageInfo(reference, element) }
} }
} }
} }
conflictChecker.checkAllConflicts(usages, LinkedHashSet<UsageInfo>(), conflicts) conflictChecker.checkAllConflicts(usages, LinkedHashSet(), conflicts)
if (targetParent is PsiDirectory) { if (targetParent is PsiDirectory) {
ExtractSuperClassUtil.checkSuperAccessible(targetParent, conflicts, originalClass.toLightClass()) ExtractSuperClassUtil.checkSuperAccessible(targetParent, conflicts, originalClass.toLightClass())
} }
@@ -228,8 +227,7 @@ class ExtractSuperRefactoring(
NewKotlinFileAction.createFileFromTemplate(extractInfo.targetFileName, template, targetParent) ?: return null NewKotlinFileAction.createFileFromTemplate(extractInfo.targetFileName, template, targetParent) ?: return null
} }
file.add(prototype) as KtClass file.add(prototype) as KtClass
} } else {
else {
val targetSibling = originalClass.parentsWithSelf.first { it.parent == targetParent } val targetSibling = originalClass.parentsWithSelf.first { it.parent == targetParent }
insertDeclaration(prototype, targetSibling) insertDeclaration(prototype, targetSibling)
} }
@@ -261,8 +259,7 @@ class ExtractSuperRefactoring(
|| originalClass.secondaryConstructors.isEmpty()) || originalClass.secondaryConstructors.isEmpty())
val newSuperTypeListEntry = if (needSuperCall) { val newSuperTypeListEntry = if (needSuperCall) {
psiFactory.createSuperTypeCallEntry("$superTypeText()") psiFactory.createSuperTypeCallEntry("$superTypeText()")
} } else {
else {
psiFactory.createSuperTypeEntry(superTypeText) psiFactory.createSuperTypeEntry(superTypeText)
} }
if (superClassEntry != null) { if (superClassEntry != null) {
@@ -271,12 +268,10 @@ class ExtractSuperRefactoring(
} }
val superClassEntryToAdd = if (qualifiedTypeRefText != null) { val superClassEntryToAdd = if (qualifiedTypeRefText != null) {
superClassEntry.copied().apply { typeReference?.replace(psiFactory.createType(qualifiedTypeRefText)) } superClassEntry.copied().apply { typeReference?.replace(psiFactory.createType(qualifiedTypeRefText)) }
} } else superClassEntry
else superClassEntry
newClass.addSuperTypeListEntry(superClassEntryToAdd) newClass.addSuperTypeListEntry(superClassEntryToAdd)
ShortenReferences.DEFAULT.process(superClassEntry.replaced(newSuperTypeListEntry)) ShortenReferences.DEFAULT.process(superClassEntry.replaced(newSuperTypeListEntry))
} } else {
else {
ShortenReferences.DEFAULT.process(originalClass.addSuperTypeListEntry(newSuperTypeListEntry)) ShortenReferences.DEFAULT.process(originalClass.addSuperTypeListEntry(newSuperTypeListEntry))
} }
@@ -297,8 +292,7 @@ class ExtractSuperRefactoring(
originalClass.superTypeListEntries.firstOrNull { originalClass.superTypeListEntries.firstOrNull {
bindingContext[BindingContext.TYPE, it.typeReference]?.constructor?.declarationDescriptor == superClassDescriptor bindingContext[BindingContext.TYPE, it.typeReference]?.constructor?.declarationDescriptor == superClassDescriptor
} }
} } else null
else null
project.runSynchronouslyWithProgress(RefactoringBundle.message("progress.text"), true) { runReadAction { analyzeContext() } } project.runSynchronouslyWithProgress(RefactoringBundle.message("progress.text"), true) { runReadAction { analyzeContext() } }
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtClassOrObject
object KotlinExtractInterfaceHandler : KotlinExtractSuperHandlerBase(true) { object KotlinExtractInterfaceHandler : KotlinExtractSuperHandlerBase(true) {
val REFACTORING_NAME = "Extract Interface" const val REFACTORING_NAME = "Extract Interface"
override fun getErrorMessage(klass: KtClassOrObject): String? { override fun getErrorMessage(klass: KtClassOrObject): String? {
val superMessage = super.getErrorMessage(klass) val superMessage = super.getErrorMessage(klass)
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtClassOrObject
object KotlinExtractSuperclassHandler : KotlinExtractSuperHandlerBase(false) { object KotlinExtractSuperclassHandler : KotlinExtractSuperHandlerBase(false) {
val REFACTORING_NAME = "Extract Superclass" const val REFACTORING_NAME = "Extract Superclass"
override fun getErrorMessage(klass: KtClassOrObject): String? { override fun getErrorMessage(klass: KtClassOrObject): String? {
val superMessage = super.getErrorMessage(klass) val superMessage = super.getErrorMessage(klass)
@@ -40,9 +40,16 @@ class KotlinExtractInterfaceDialog(
targetParent: PsiElement, targetParent: PsiElement,
conflictChecker: (KotlinExtractSuperDialogBase) -> Boolean, conflictChecker: (KotlinExtractSuperDialogBase) -> Boolean,
refactoring: (ExtractSuperInfo) -> Unit refactoring: (ExtractSuperInfo) -> Unit
) : KotlinExtractSuperDialogBase(originalClass, targetParent, conflictChecker, true, KotlinExtractInterfaceHandler.REFACTORING_NAME, refactoring) { ) : KotlinExtractSuperDialogBase(
originalClass,
targetParent,
conflictChecker,
true,
KotlinExtractInterfaceHandler.REFACTORING_NAME,
refactoring
) {
companion object { companion object {
private val DESTINATION_PACKAGE_RECENT_KEY = "KotlinExtractInterfaceDialog.RECENT_KEYS" private const val DESTINATION_PACKAGE_RECENT_KEY = "KotlinExtractInterfaceDialog.RECENT_KEYS"
} }
init { init {
@@ -64,7 +64,12 @@ abstract class KotlinExtractSuperDialogBase(
originalClass: KtClassOrObject, originalClass: KtClassOrObject,
val memberInfos: List<KotlinMemberInfo>, val memberInfos: List<KotlinMemberInfo>,
interfaceContainmentVerifier: (KtNamedDeclaration) -> Boolean interfaceContainmentVerifier: (KtNamedDeclaration) -> Boolean
) : KotlinUsesAndInterfacesDependencyMemberInfoModel<KtNamedDeclaration, KotlinMemberInfo>(originalClass, null, false, interfaceContainmentVerifier) { ) : KotlinUsesAndInterfacesDependencyMemberInfoModel<KtNamedDeclaration, KotlinMemberInfo>(
originalClass,
null,
false,
interfaceContainmentVerifier
) {
override fun isMemberEnabled(member: KotlinMemberInfo): Boolean { override fun isMemberEnabled(member: KotlinMemberInfo): Boolean {
val declaration = member.member ?: return false val declaration = member.member ?: return false
return !declaration.hasModifier(KtTokens.CONST_KEYWORD) return !declaration.hasModifier(KtTokens.CONST_KEYWORD)
@@ -35,9 +35,16 @@ class KotlinExtractSuperclassDialog(
targetParent: PsiElement, targetParent: PsiElement,
conflictChecker: (KotlinExtractSuperDialogBase) -> Boolean, conflictChecker: (KotlinExtractSuperDialogBase) -> Boolean,
refactoring: (ExtractSuperInfo) -> Unit refactoring: (ExtractSuperInfo) -> Unit
) : KotlinExtractSuperDialogBase(originalClass, targetParent, conflictChecker, false, KotlinExtractSuperclassHandler.REFACTORING_NAME, refactoring) { ) : KotlinExtractSuperDialogBase(
originalClass,
targetParent,
conflictChecker,
false,
KotlinExtractSuperclassHandler.REFACTORING_NAME,
refactoring
) {
companion object { companion object {
private val DESTINATION_PACKAGE_RECENT_KEY = "KotlinExtractSuperclassDialog.RECENT_KEYS" private const val DESTINATION_PACKAGE_RECENT_KEY = "KotlinExtractSuperclassDialog.RECENT_KEYS"
} }
init { init {
@@ -133,8 +133,7 @@ fun selectElementsWithTargetParent(
selectElement(editor, file, false, elementKinds) { expr -> selectElement(editor, file, false, elementKinds) { expr ->
if (expr != null) { if (expr != null) {
selectTargetContainer(listOf(expr)) selectTargetContainer(listOf(expr))
} } else {
else {
if (!editor.selectionModel.hasSelection()) { if (!editor.selectionModel.hasSelection()) {
if (elementKinds.singleOrNull() == CodeInsightUtils.ElementKind.EXPRESSION) { if (elementKinds.singleOrNull() == CodeInsightUtils.ElementKind.EXPRESSION) {
val elementAtCaret = file.findElementAt(editor.caretModel.offset) val elementAtCaret = file.findElementAt(editor.caretModel.offset)
@@ -221,7 +220,7 @@ fun ExtractableSubstringInfo.replaceWith(replacement: KtExpression): KtExpressio
fun KtExpression.mustBeParenthesizedInInitializerPosition(): Boolean { fun KtExpression.mustBeParenthesizedInInitializerPosition(): Boolean {
if (this !is KtBinaryExpression) return false if (this !is KtBinaryExpression) return false
if (left?.mustBeParenthesizedInInitializerPosition() ?: false) return true if (left?.mustBeParenthesizedInInitializerPosition() == true) return true
return PsiChildRange(left, operationReference).any { (it is PsiWhiteSpace) && it.textContains('\n') } return PsiChildRange(left, operationReference).any { (it is PsiWhiteSpace) && it.textContains('\n') }
} }
@@ -111,8 +111,7 @@ fun extractClassMembers(
val typeReference = it.typeReference ?: return@mapNotNull null val typeReference = it.typeReference ?: return@mapNotNull null
val type = typeReference.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference] val type = typeReference.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
val classDescriptor = type?.constructor?.declarationDescriptor as? ClassDescriptor val classDescriptor = type?.constructor?.declarationDescriptor as? ClassDescriptor
val classPsi = classDescriptor?.source?.getPsi() when (val classPsi = classDescriptor?.source?.getPsi()) {
when (classPsi) {
is KtClass -> classPsi is KtClass -> classPsi
is PsiClass -> KtPsiClassWrapper(classPsi) is PsiClass -> KtPsiClassWrapper(classPsi)
else -> null else -> null