Move: Get rid of MoveKotlinTopLevelDeclarationsOptions.sourceFile
This commit is contained in:
-1
@@ -42,7 +42,6 @@ public class KotlinChangePackageRefactoring(val file: JetFile) {
|
|||||||
val declarationProcessor = MoveKotlinTopLevelDeclarationsProcessor(
|
val declarationProcessor = MoveKotlinTopLevelDeclarationsProcessor(
|
||||||
project,
|
project,
|
||||||
MoveKotlinTopLevelDeclarationsOptions(
|
MoveKotlinTopLevelDeclarationsOptions(
|
||||||
sourceFile = file,
|
|
||||||
elementsToMove = file.getDeclarations().filterIsInstance<JetNamedDeclaration>(),
|
elementsToMove = file.getDeclarations().filterIsInstance<JetNamedDeclaration>(),
|
||||||
moveTarget = object: KotlinMoveTarget {
|
moveTarget = object: KotlinMoveTarget {
|
||||||
override val packageWrapper = PackageWrapper(file.getManager(), newFqName.asString())
|
override val packageWrapper = PackageWrapper(file.getManager(), newFqName.asString())
|
||||||
|
|||||||
-1
@@ -61,7 +61,6 @@ public class MoveKotlinFileHandler : MoveFileHandler() {
|
|||||||
val declarationMoveProcessor = MoveKotlinTopLevelDeclarationsProcessor(
|
val declarationMoveProcessor = MoveKotlinTopLevelDeclarationsProcessor(
|
||||||
project,
|
project,
|
||||||
MoveKotlinTopLevelDeclarationsOptions(
|
MoveKotlinTopLevelDeclarationsOptions(
|
||||||
sourceFile = psiFile,
|
|
||||||
elementsToMove = psiFile.getDeclarations().filterIsInstance<JetNamedDeclaration>(),
|
elementsToMove = psiFile.getDeclarations().filterIsInstance<JetNamedDeclaration>(),
|
||||||
moveTarget = DeferredJetFileKotlinMoveTarget(project, packageNameInfo.newPackageName) {
|
moveTarget = DeferredJetFileKotlinMoveTarget(project, packageNameInfo.newPackageName) {
|
||||||
MoveFilesOrDirectoriesUtil.doMoveFile(psiFile, newParent)
|
MoveFilesOrDirectoriesUtil.doMoveFile(psiFile, newParent)
|
||||||
|
|||||||
+34
-31
@@ -19,10 +19,7 @@ package org.jetbrains.kotlin.idea.refactoring.move.moveTopLevelDeclarations
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.Ref
|
import com.intellij.openapi.util.Ref
|
||||||
import com.intellij.openapi.util.text.StringUtil
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.PsiModifier
|
|
||||||
import com.intellij.psi.PsiModifierListOwner
|
|
||||||
import com.intellij.psi.PsiReference
|
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import com.intellij.refactoring.BaseRefactoringProcessor
|
import com.intellij.refactoring.BaseRefactoringProcessor
|
||||||
import com.intellij.refactoring.move.MoveCallback
|
import com.intellij.refactoring.move.MoveCallback
|
||||||
@@ -80,13 +77,12 @@ interface Mover: (originalElement: JetNamedDeclaration, targetFile: JetFile) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class MoveKotlinTopLevelDeclarationsOptions(
|
public class MoveKotlinTopLevelDeclarationsOptions(
|
||||||
val sourceFile: JetFile,
|
|
||||||
val elementsToMove: Collection<JetNamedDeclaration>,
|
val elementsToMove: Collection<JetNamedDeclaration>,
|
||||||
val moveTarget: KotlinMoveTarget,
|
val moveTarget: KotlinMoveTarget,
|
||||||
val searchInCommentsAndStrings: Boolean = true,
|
val searchInCommentsAndStrings: Boolean = true,
|
||||||
val searchInNonCode: Boolean = true,
|
val searchInNonCode: Boolean = true,
|
||||||
val updateInternalReferences: Boolean = true,
|
val updateInternalReferences: Boolean = true,
|
||||||
val deleteSourceFile: Boolean = false,
|
val deleteSourceFiles: Boolean = false,
|
||||||
val moveCallback: MoveCallback? = null
|
val moveCallback: MoveCallback? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -100,7 +96,9 @@ public class MoveKotlinTopLevelDeclarationsProcessor(
|
|||||||
|
|
||||||
private var nonCodeUsages: Array<NonCodeUsageInfo>? = null
|
private var nonCodeUsages: Array<NonCodeUsageInfo>? = null
|
||||||
private val elementsToMove = options.elementsToMove.filter { e -> e.getContainingFile() != options.moveTarget.getTargetPsiIfExists(e) }
|
private val elementsToMove = options.elementsToMove.filter { e -> e.getContainingFile() != options.moveTarget.getTargetPsiIfExists(e) }
|
||||||
private val kotlinToLightElements = elementsToMove.keysToMap { it.toLightElements() }
|
private val kotlinToLightElementsBySourceFile = elementsToMove
|
||||||
|
.groupBy { it.getContainingJetFile() }
|
||||||
|
.mapValues { it.value.keysToMap { it.toLightElements() } }
|
||||||
private val conflicts = MultiMap<PsiElement, String>()
|
private val conflicts = MultiMap<PsiElement, String>()
|
||||||
|
|
||||||
override fun createUsageViewDescriptor(usages: Array<out UsageInfo>): UsageViewDescriptor {
|
override fun createUsageViewDescriptor(usages: Array<out UsageInfo>): UsageViewDescriptor {
|
||||||
@@ -113,8 +111,8 @@ public class MoveKotlinTopLevelDeclarationsProcessor(
|
|||||||
public override fun findUsages(): Array<UsageInfo> {
|
public override fun findUsages(): Array<UsageInfo> {
|
||||||
val newPackageName = options.moveTarget.packageWrapper?.getQualifiedName() ?: ""
|
val newPackageName = options.moveTarget.packageWrapper?.getQualifiedName() ?: ""
|
||||||
|
|
||||||
fun collectUsages(): List<UsageInfo> {
|
fun collectUsages(kotlinToLightElements: Map<JetNamedDeclaration, List<PsiNamedElement>>, result: MutableList<UsageInfo>) {
|
||||||
return kotlinToLightElements.values().flatMap { it }.flatMap { lightElement ->
|
kotlinToLightElements.values().flatMap { it }.flatMapTo(result) { lightElement ->
|
||||||
val newFqName = StringUtil.getQualifiedName(newPackageName, lightElement.getName())
|
val newFqName = StringUtil.getQualifiedName(newPackageName, lightElement.getName())
|
||||||
|
|
||||||
val foundReferences = HashSet<PsiReference>()
|
val foundReferences = HashSet<PsiReference>()
|
||||||
@@ -219,12 +217,17 @@ public class MoveKotlinTopLevelDeclarationsProcessor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to find and process usages if package is not changed
|
val usages = ArrayList<UsageInfo>()
|
||||||
if (options.sourceFile.getPackageFqName().asString() == newPackageName) return UsageInfo.EMPTY_ARRAY
|
for ((sourceFile, kotlinToLightElements) in kotlinToLightElementsBySourceFile) {
|
||||||
|
// No need to find and process usages if package is not changed
|
||||||
|
if (sourceFile.getPackageFqName().asString() == newPackageName) return UsageInfo.EMPTY_ARRAY
|
||||||
|
|
||||||
|
collectUsages(kotlinToLightElements, usages)
|
||||||
|
collectConflictsInUsages(usages)
|
||||||
|
collectConflictsInDeclarations()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
val usages = collectUsages()
|
|
||||||
collectConflictsInUsages(usages)
|
|
||||||
collectConflictsInDeclarations()
|
|
||||||
return UsageViewUtil.removeDuplicatedUsages(usages.toTypedArray())
|
return UsageViewUtil.removeDuplicatedUsages(usages.toTypedArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,29 +267,29 @@ public class MoveKotlinTopLevelDeclarationsProcessor(
|
|||||||
val usageList = usages.toArrayList()
|
val usageList = usages.toArrayList()
|
||||||
|
|
||||||
val oldToNewElementsMapping = HashMap<PsiElement, PsiElement>()
|
val oldToNewElementsMapping = HashMap<PsiElement, PsiElement>()
|
||||||
for ((oldDeclaration, oldLightElements) in kotlinToLightElements) {
|
for ((sourceFile, kotlinToLightElements) in kotlinToLightElementsBySourceFile) {
|
||||||
val oldFile = oldDeclaration.getContainingJetFile()
|
for ((oldDeclaration, oldLightElements) in kotlinToLightElements) {
|
||||||
|
val newDeclaration = moveDeclaration(oldDeclaration, options.moveTarget, usageList)
|
||||||
val newDeclaration = moveDeclaration(oldDeclaration, options.moveTarget, usageList)
|
if (newDeclaration == null) {
|
||||||
if (newDeclaration == null) {
|
for (oldElement in oldLightElements) {
|
||||||
for (oldElement in oldLightElements) {
|
oldToNewElementsMapping[oldElement] = oldElement
|
||||||
oldToNewElementsMapping[oldElement] = oldElement
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
oldToNewElementsMapping[sourceFile] = newDeclaration.getContainingJetFile()
|
||||||
|
|
||||||
|
getTransaction()!!.getElementListener(oldDeclaration).elementMoved(newDeclaration)
|
||||||
|
for ((oldElement, newElement) in oldLightElements.asSequence() zip newDeclaration.toLightElements().asSequence()) {
|
||||||
|
oldToNewElementsMapping[oldElement] = newElement
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldToNewElementsMapping[oldFile] = newDeclaration.getContainingJetFile()
|
if (options.deleteSourceFiles) {
|
||||||
|
sourceFile.delete()
|
||||||
getTransaction()!!.getElementListener(oldDeclaration).elementMoved(newDeclaration)
|
|
||||||
for ((oldElement, newElement) in oldLightElements.asSequence() zip newDeclaration.toLightElements().asSequence()) {
|
|
||||||
oldToNewElementsMapping[oldElement] = newElement
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.deleteSourceFile) {
|
|
||||||
options.sourceFile.delete()
|
|
||||||
}
|
|
||||||
|
|
||||||
nonCodeUsages = postProcessMoveUsages(usageList, oldToNewElementsMapping).toTypedArray()
|
nonCodeUsages = postProcessMoveUsages(usageList, oldToNewElementsMapping).toTypedArray()
|
||||||
}
|
}
|
||||||
catch (e: IncorrectOperationException) {
|
catch (e: IncorrectOperationException) {
|
||||||
|
|||||||
+1
-1
@@ -624,7 +624,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MoveKotlinTopLevelDeclarationsOptions options = new MoveKotlinTopLevelDeclarationsOptions(
|
MoveKotlinTopLevelDeclarationsOptions options = new MoveKotlinTopLevelDeclarationsOptions(
|
||||||
sourceFile, elementsToMove, target, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback
|
elementsToMove, target, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback
|
||||||
);
|
);
|
||||||
invokeRefactoring(new MoveKotlinTopLevelDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE$));
|
invokeRefactoring(new MoveKotlinTopLevelDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE$));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,7 +255,7 @@ enum class MoveAction {
|
|||||||
JetFileKotlinMoveTarget(PsiManager.getInstance(project).findFile(rootDir.findFileByRelativePath(filePath)!!) as JetFile)
|
JetFileKotlinMoveTarget(PsiManager.getInstance(project).findFile(rootDir.findFileByRelativePath(filePath)!!) as JetFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
val options = MoveKotlinTopLevelDeclarationsOptions(mainFile as JetFile, listOf(elementToMove), moveTarget)
|
val options = MoveKotlinTopLevelDeclarationsOptions(listOf(elementToMove), moveTarget)
|
||||||
MoveKotlinTopLevelDeclarationsProcessor(mainFile.getProject(), options).run()
|
MoveKotlinTopLevelDeclarationsProcessor(mainFile.getProject(), options).run()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user