From 5414b9dfb3084447a2201798312f0b2778b0e758 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Mon, 30 Mar 2020 15:49:31 +0300 Subject: [PATCH] Refactor Move refactoring --- .../messages/KotlinBundle.properties | 2 ++ .../MoveKotlinDeclarationsHandler.kt | 8 ++--- ...KotlinAwareMoveFilesOrDirectoriesDialog.kt | 29 +++++++++++-------- .../MoveKotlinTopLevelDeclarationsDialog.java | 4 +-- .../ui/MoveKotlinTopLevelDeclarationsModel.kt | 16 +++++----- .../KotlinMoveFilesOrDirectoriesHandler.kt | 2 +- .../kotlin/idea/refactoring/move/moveUtils.kt | 11 ++++--- 7 files changed, 40 insertions(+), 32 deletions(-) diff --git a/idea/resources/messages/KotlinBundle.properties b/idea/resources/messages/KotlinBundle.properties index 42d7db813e6..dc8916d55f7 100644 --- a/idea/resources/messages/KotlinBundle.properties +++ b/idea/resources/messages/KotlinBundle.properties @@ -930,6 +930,8 @@ text.member.extension.call.will.not.be.processed.0=Member extension call won''t text.move.declaration.no.support.for.companion.objects=Move declaration is not supported for companion objects text.move.declaration.no.support.for.enums=Move declaration is not supported for enum entries text.move.declaration.supports.only.top.levels.and.nested.classes=Move declaration is only supported for top-level declarations and nested classes +text.move.declaration.proceed.move.without.mpp.counterparts.text=This refactoring will move selected declaration without it's expect/actual counterparts that may lead to compilation errors.\nDo you wish to proceed? +text.move.declaration.proceed.move.without.mpp.counterparts.title=MPP declarations does not supported by this refactoring. text.move.declarations=Move declarations text.move.file.0=Move {0} text.move.refactoring.not.available.during.indexing=Move refactoring is not available while indexing is in progress diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt index e13c2d8d9bc..7b81d32e130 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt @@ -172,11 +172,11 @@ class MoveKotlinDeclarationsHandler internal constructor(private val handlerActi } val initialTargetDirectory = MoveFilesOrDirectoriesUtil.resolveToDirectory(project, initialTargetElement) - if (!ApplicationManager.getApplication().isUnitTestMode && elementsToSearch.any { it.isExpectDeclaration() || it.isEffectivelyActual() } + if (!ApplicationManager.getApplication().isUnitTestMode && + elementsToSearch.any { it.isExpectDeclaration() || it.isEffectivelyActual() } ) { - val message = RefactoringBundle.getCannotRefactorMessage( - "This refactoring will move selected declaration without it's expect/actual counterparts that may lead to compilation errors.\n\"Do you wish to proceed?\"") - val title = RefactoringBundle.getCannotRefactorMessage("MPP declarations does not supported by this refactoring.") + val message = RefactoringBundle.getCannotRefactorMessage(KotlinBundle.message("text.move.declaration.proceed.move.without.mpp.counterparts.text")) + val title = RefactoringBundle.getCannotRefactorMessage(KotlinBundle.message("text.move.declaration.proceed.move.without.mpp.counterparts.title")) val proceedWithIncompleteRefactoring = Messages.showYesNoDialog(project, message, title, Messages.getWarningIcon()) if (proceedWithIncompleteRefactoring != Messages.YES) return true } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinAwareMoveFilesOrDirectoriesDialog.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinAwareMoveFilesOrDirectoriesDialog.kt index 4f1737e4f08..8de4720592f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinAwareMoveFilesOrDirectoriesDialog.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinAwareMoveFilesOrDirectoriesDialog.kt @@ -40,7 +40,6 @@ import org.jetbrains.kotlin.idea.refactoring.isInKotlinAwareSourceRoot import org.jetbrains.kotlin.idea.refactoring.move.logFusForMoveRefactoring import org.jetbrains.kotlin.idea.util.application.executeCommand import org.jetbrains.kotlin.psi.KtFile -import java.util.* import javax.swing.JComponent class KotlinAwareMoveFilesOrDirectoriesDialog( @@ -66,6 +65,12 @@ class KotlinAwareMoveFilesOrDirectoriesDialog( !ApplicationManager.getApplication().isUnitTestMode && PropertiesComponent.getInstance().getBoolean(id, defaultValue) } + private data class CheckboxesState( + val searchReferences: Boolean, + val openInEditor: Boolean, + val updatePackageDirective: Boolean + ) + private val nameLabel = JBLabelDecorator.createJBLabelDecorator().setBold(true) private val targetDirectoryField = TextFieldWithHistoryWithBrowseButton() private val searchReferencesCb = NonFocusableCheckBox(KotlinBundle.message("checkbox.text.search.references")).apply { @@ -73,7 +78,7 @@ class KotlinAwareMoveFilesOrDirectoriesDialog( } private val openInEditorCb = NonFocusableCheckBox(KotlinBundle.message("checkbox.text.open.moved.files.in.editor")) private val updatePackageDirectiveCb = NonFocusableCheckBox() - private val initializedCheckBoxesState: BitSet + private val initializedCheckBoxesState: CheckboxesState override fun getHelpId() = HELP_ID @@ -85,11 +90,11 @@ class KotlinAwareMoveFilesOrDirectoriesDialog( initializedCheckBoxesState = getCheckboxesState(applyDefaults = true) } - private fun getCheckboxesState(applyDefaults: Boolean) = BitSet(3).apply { - set(0, applyDefaults || searchReferencesCb.isSelected) //searchReferencesCb is true by default - set(1, !applyDefaults && openInEditorCb.isSelected) //openInEditorCb is false by default - set(2, updatePackageDirectiveCb.isSelected) - } + private fun getCheckboxesState(applyDefaults: Boolean) = CheckboxesState( + searchReferences = applyDefaults || searchReferencesCb.isSelected, //searchReferencesCb is true by default + openInEditor = !applyDefaults && openInEditorCb.isSelected, //openInEditorCb is false by default + updatePackageDirective = updatePackageDirectiveCb.isSelected + ) override fun createActions() = arrayOf(okAction, cancelAction, helpAction) @@ -212,11 +217,11 @@ class KotlinAwareMoveFilesOrDirectoriesDialog( project.executeCommand(MoveHandler.REFACTORING_NAME) { with(modelResult) { logFusForMoveRefactoring( - elementsCount, - entityToMove, - destination, - getCheckboxesState(applyDefaults = false) == initializedCheckBoxesState, - Runnable { processor.run() } + numberOfEntities = elementsCount, + entity = entityToMove, + destination = destination, + isDefault = getCheckboxesState(applyDefaults = false) == initializedCheckBoxesState, + body = processor ) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsDialog.java b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsDialog.java index 98c855a8bd4..84e14b2176b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsDialog.java +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsDialog.java @@ -180,7 +180,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog { @NotNull Set elementsToMove, @NotNull List declarations ) { - ////KotlinMemberInfo run resolve on declaration so it is good to place it to the process + //KotlinMemberInfo run resolve on declaration so it is good to place it to the process List memberInfos = MoveUtilsKt.mapWithReadActionInProcess(declarations, myProject, MoveHandler.REFACTORING_NAME, (declaration) -> { KotlinMemberInfo memberInfo = new KotlinMemberInfo(declaration, false); memberInfo.setChecked(elementsToMove.contains(declaration)); @@ -336,7 +336,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog { } private boolean isMPPDeclarationInList(List declarations) { - for(KtNamedDeclaration element : declarations) { + for (KtNamedDeclaration element : declarations) { if (ExpectActualUtilKt.isEffectivelyActual(element, true) || ExpectActualUtilKt.isExpectDeclaration(element)) { return true; diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsModel.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsModel.kt index e4eeb6947f1..a7133aabd91 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsModel.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinTopLevelDeclarationsModel.kt @@ -219,14 +219,16 @@ internal class MoveKotlinTopLevelDeclarationsModel( } private fun getFUSParameters(): Pair { - val classType = if (elementsToMoveHasMPP) MovedEntity.MPPCLASSES else MovedEntity.CLASSES - val functionType = if (elementsToMoveHasMPP) MovedEntity.MPPFUNCTIONS else MovedEntity.FUNCTIONS - val mixedType = if (elementsToMoveHasMPP) MovedEntity.MPPMIXED else MovedEntity.MIXED + val (classType, functionType, mixedType) = + if (elementsToMoveHasMPP) + Triple(MovedEntity.MPPCLASSES, MovedEntity.MPPFUNCTIONS, MovedEntity.MPPMIXED) + else + Triple(MovedEntity.CLASSES, MovedEntity.FUNCTIONS, MovedEntity.MIXED) - val allClasses = elementsToMove.any { element -> element is KtClassOrObject } - val allFunctions = elementsToMove.any { element -> element is KtFunction } - val entity = if (allClasses && allFunctions) mixedType else - if (allClasses) classType else functionType + val classesAreGoingToMove = elementsToMove.any { it is KtClassOrObject } + val functionsAreGoingToMove = elementsToMove.any { it is KtFunction } + val entity = if (classesAreGoingToMove && functionsAreGoingToMove) mixedType else + if (classesAreGoingToMove) classType else functionType val destination = if (isMoveToPackage) MoveRefactoringDestination.PACKAGE else MoveRefactoringDestination.FILE diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveFilesOrDirectories/KotlinMoveFilesOrDirectoriesHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveFilesOrDirectories/KotlinMoveFilesOrDirectoriesHandler.kt index c2182b301d9..81a71a1af9e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveFilesOrDirectories/KotlinMoveFilesOrDirectoriesHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveFilesOrDirectories/KotlinMoveFilesOrDirectoriesHandler.kt @@ -79,7 +79,7 @@ class KotlinMoveFilesOrDirectoriesHandler : MoveFilesOrDirectoriesHandlerCompat( entityToMove, destination, true, - Runnable { processor.run() } + processor ) } else { processor.run() diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt index 3e08b29956a..7f51962462a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveUtils.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.idea.refactoring.move import com.intellij.ide.util.DirectoryUtil -import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task @@ -43,6 +42,7 @@ import org.jetbrains.kotlin.idea.references.KtSimpleNameReference.ShorteningMode import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.statistics.MoveRefactoringFUSCollector import org.jetbrains.kotlin.idea.util.application.executeCommand +import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor import org.jetbrains.kotlin.name.FqName @@ -720,15 +720,14 @@ internal fun List.mapWithReadActionInProcess( val result = mutableListOf() val task: Task.Modal = object : Task.Modal(project, title, false) { override fun run(indicator: ProgressIndicator) { - val count = 0 val fraction: Double = 1.0 / declarations.size indicator.fraction = 0.0 - ApplicationManager.getApplication().runReadAction(Runnable { - for (declaration in declarations) { + runReadAction { + declarations.forEachIndexed { index, declaration -> result.add(body(declaration)) - indicator.fraction = fraction * count + indicator.fraction = fraction * index } - }) + } } } ProgressManager.getInstance().run(task)