Clean up move refactoring
This commit is contained in:
+3
-14
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move
|
||||
@@ -28,7 +17,7 @@ sealed class AutocreatingPsiDirectoryWrapper {
|
||||
|
||||
class ByMoveDestination(private val moveDestination: MoveDestination) : AutocreatingPsiDirectoryWrapper() {
|
||||
override fun getPackageName() = moveDestination.targetPackage.qualifiedName
|
||||
override fun getOrCreateDirectory(source: PsiDirectory) = moveDestination.getTargetDirectory(source)
|
||||
override fun getOrCreateDirectory(source: PsiDirectory): PsiDirectory = moveDestination.getTargetDirectory(source)
|
||||
}
|
||||
|
||||
abstract fun getPackageName(): String
|
||||
|
||||
+3
-15
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move
|
||||
@@ -35,8 +24,7 @@ class MoveJavaInnerClassKotlinUsagesHandler: MoveInnerClassUsagesHandler {
|
||||
|
||||
val argumentToAdd = psiFactory.createArgument(receiverExpression ?: psiFactory.createExpression("this"))
|
||||
|
||||
val argumentList =
|
||||
innerCall.valueArgumentList
|
||||
val argumentList = innerCall.valueArgumentList
|
||||
?: (innerCall.lambdaArguments.firstOrNull()?.let { lambdaArg ->
|
||||
val anchor = PsiTreeUtil.skipSiblingsBackward(lambdaArg, PsiWhiteSpace::class.java)
|
||||
innerCall.addAfter(psiFactory.createCallArguments("()"), anchor)
|
||||
|
||||
+2
-13
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveClassesOrPackages
|
||||
|
||||
+10
-14
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -98,7 +87,14 @@ class ExtractDeclarationFromCurrentFileIntention :
|
||||
true,
|
||||
true,
|
||||
MoveCallback {
|
||||
runBlocking { withTimeoutOrNull(TIMEOUT_FOR_IMPORT_OPTIMIZING_MS) { OptimizeImportsProcessor(project, file).run() } }
|
||||
runBlocking {
|
||||
withTimeoutOrNull(TIMEOUT_FOR_IMPORT_OPTIMIZING_MS) {
|
||||
OptimizeImportsProcessor(
|
||||
project,
|
||||
file
|
||||
).run()
|
||||
}
|
||||
}
|
||||
}
|
||||
).show()
|
||||
}
|
||||
|
||||
+11
-32
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -41,40 +30,31 @@ class KotlinAwareMoveFilesOrDirectoriesProcessor @JvmOverloads constructor (
|
||||
searchInNonJavaFiles: Boolean,
|
||||
moveCallback: MoveCallback?,
|
||||
prepareSuccessfulCallback: Runnable = EmptyRunnable.INSTANCE
|
||||
) : MoveFilesOrDirectoriesProcessor(project,
|
||||
elementsToMove.toTypedArray<PsiElement>(),
|
||||
) : MoveFilesOrDirectoriesProcessor(
|
||||
project,
|
||||
elementsToMove.toTypedArray(),
|
||||
targetDirectory,
|
||||
true,
|
||||
searchInComments,
|
||||
searchInNonJavaFiles,
|
||||
moveCallback,
|
||||
prepareSuccessfulCallback) {
|
||||
prepareSuccessfulCallback
|
||||
) {
|
||||
override fun createUsageViewDescriptor(usages: Array<out UsageInfo>): UsageViewDescriptor {
|
||||
return MoveFilesWithDeclarationsViewDescriptor(elementsToMove.toTypedArray<PsiElement>(), targetDirectory)
|
||||
return MoveFilesWithDeclarationsViewDescriptor(elementsToMove.toTypedArray(), targetDirectory)
|
||||
}
|
||||
|
||||
override fun findUsages(): Array<UsageInfo> {
|
||||
try {
|
||||
markScopeToMove(elementsToMove)
|
||||
return if (searchReferences) super.findUsages() else UsageInfo.EMPTY_ARRAY
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
markScopeToMove(null)
|
||||
}
|
||||
}
|
||||
|
||||
override fun preprocessUsages(refUsages: Ref<Array<UsageInfo>>): Boolean {
|
||||
val usages = refUsages.get()
|
||||
|
||||
val (conflictUsages, usagesToProcess) = usages.partition { it is ConflictUsageInfo }
|
||||
|
||||
val conflicts = MultiMap<PsiElement, String>()
|
||||
for (conflictUsage in conflictUsages) {
|
||||
conflicts.putValues(conflictUsage.element, (conflictUsage as ConflictUsageInfo).messages)
|
||||
}
|
||||
|
||||
refUsages.set(usagesToProcess.toTypedArray())
|
||||
|
||||
val (conflicts, usages) = MoveToKotlinFileProcessor.preprocessConflictUsages(refUsages)
|
||||
return showConflicts(conflicts, usages)
|
||||
}
|
||||
|
||||
@@ -101,8 +81,7 @@ class KotlinAwareMoveFilesOrDirectoriesProcessor @JvmOverloads constructor (
|
||||
try {
|
||||
markShouldFixFqName(true)
|
||||
super.performRefactoring(usages)
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
markShouldFixFqName(false)
|
||||
}
|
||||
}
|
||||
|
||||
+10
-21
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -53,12 +42,12 @@ interface KotlinDirectoryBasedMoveTarget : KotlinMoveTarget {
|
||||
}
|
||||
|
||||
object EmptyKotlinMoveTarget : KotlinMoveTarget {
|
||||
override val targetContainerFqName = null
|
||||
override val targetFile = null
|
||||
override val targetContainerFqName: FqName? = null
|
||||
override val targetFile: VirtualFile? = null
|
||||
|
||||
override fun getOrCreateTargetPsi(originalPsi: PsiElement) = null
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement) = null
|
||||
override fun verify(file: PsiFile) = null
|
||||
override fun getOrCreateTargetPsi(originalPsi: PsiElement): KtElement? = null
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement): KtElement? = null
|
||||
override fun verify(file: PsiFile): String? = null
|
||||
}
|
||||
|
||||
class KotlinMoveTargetForExistingElement(val targetElement: KtElement) : KotlinMoveTarget {
|
||||
@@ -101,7 +90,7 @@ class KotlinMoveTargetForDeferredFile(
|
||||
return createdFiles.getOrPutNullable(originalFile) { createFile(originalFile) }
|
||||
}
|
||||
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement) = null
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement): KtElement? = null
|
||||
|
||||
// No additional verification is needed
|
||||
override fun verify(file: PsiFile): String? = null
|
||||
@@ -115,9 +104,9 @@ class KotlinDirectoryMoveTarget(
|
||||
|
||||
override fun getOrCreateTargetPsi(originalPsi: PsiElement) = originalPsi.containingFile as? KtFile
|
||||
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement) = null
|
||||
override fun getTargetPsiIfExists(originalPsi: PsiElement): KtElement? = null
|
||||
|
||||
override fun verify(file: PsiFile) = null
|
||||
override fun verify(file: PsiFile): String? = null
|
||||
}
|
||||
|
||||
fun KotlinMoveTarget.getTargetModule(project: Project) = targetScope?.getModule(project)
|
||||
+3
-15
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -87,8 +76,7 @@ sealed class MoveDeclarationsDelegate {
|
||||
moveDescriptor: MoveDeclarationsDescriptor,
|
||||
companionDescriptor: ClassDescriptor
|
||||
): Boolean {
|
||||
val moveTarget = moveDescriptor.moveTarget
|
||||
return when (moveTarget) {
|
||||
return when (val moveTarget = moveDescriptor.moveTarget) {
|
||||
is KotlinMoveTargetForCompanion -> true
|
||||
is KotlinMoveTargetForExistingElement -> {
|
||||
val targetClass = moveTarget.targetElement as? KtClassOrObject ?: return false
|
||||
|
||||
+11
-19
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -33,13 +22,16 @@ internal class MoveFilesWithDeclarationsViewDescriptor(
|
||||
|
||||
init {
|
||||
if (myElementsToMove.size == 1) {
|
||||
myProcessedElementsHeader = RefactoringBundle.message("move.single.element.elements.header",
|
||||
myProcessedElementsHeader = RefactoringBundle.message(
|
||||
"move.single.element.elements.header",
|
||||
UsageViewUtil.getType(myElementsToMove[0]),
|
||||
newParent.virtualFile.presentableUrl).capitalize()
|
||||
myCodeReferencesText = "References in code to ${UsageViewUtil.getType(myElementsToMove[0])} ${UsageViewUtil.getLongName(myElementsToMove[0])} and its declarations"
|
||||
}
|
||||
else {
|
||||
myProcessedElementsHeader = StringUtil.capitalize(RefactoringBundle.message("move.files.elements.header", newParent.virtualFile.presentableUrl))
|
||||
newParent.virtualFile.presentableUrl
|
||||
).capitalize()
|
||||
myCodeReferencesText =
|
||||
"References in code to ${UsageViewUtil.getType(myElementsToMove[0])} ${UsageViewUtil.getLongName(myElementsToMove[0])} and its declarations"
|
||||
} else {
|
||||
myProcessedElementsHeader =
|
||||
StringUtil.capitalize(RefactoringBundle.message("move.files.elements.header", newParent.virtualFile.presentableUrl))
|
||||
myCodeReferencesText = RefactoringBundle.message("references.found.in.code")
|
||||
}
|
||||
}
|
||||
|
||||
+8
-17
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -153,10 +142,13 @@ class MoveKotlinDeclarationsProcessor(
|
||||
|
||||
private var nonCodeUsages: Array<NonCodeUsageInfo>? = null
|
||||
private val moveEntireFile = descriptor.moveSource is MoveSource.File
|
||||
private val elementsToMove = descriptor.moveSource.elementsToMove.filter { e -> e.parent != descriptor.moveTarget.getTargetPsiIfExists(e) }
|
||||
private val elementsToMove = descriptor.moveSource.elementsToMove.filter { e ->
|
||||
e.parent != descriptor.moveTarget.getTargetPsiIfExists(e)
|
||||
}
|
||||
|
||||
private val kotlinToLightElementsBySourceFile = elementsToMove
|
||||
.groupBy { it.containingKtFile }
|
||||
.mapValues { it.value.keysToMap { it.toLightElements().ifEmpty { listOf(it) } } }
|
||||
.mapValues { it.value.keysToMap { declaration -> declaration.toLightElements().ifEmpty { listOf(declaration) } } }
|
||||
private val conflicts = MultiMap<PsiElement, String>()
|
||||
|
||||
override fun getRefactoringId() = REFACTORING_ID
|
||||
@@ -211,8 +203,7 @@ class MoveKotlinDeclarationsProcessor(
|
||||
.mapNotNullTo(ArrayList()) { ref ->
|
||||
if (foundReferences.add(ref) && elementsToMove.none { it.isAncestor(ref.element) }) {
|
||||
createMoveUsageInfoIfPossible(ref, lightElement, addImportToOriginalFile = true, isInternal = false)
|
||||
}
|
||||
else null
|
||||
} else null
|
||||
}
|
||||
|
||||
val name = lightElement.getKotlinFqName()?.quoteIfNeeded()?.asString()
|
||||
|
||||
+25
-15
@@ -38,14 +38,16 @@ class MoveToKotlinFileProcessor @JvmOverloads constructor (
|
||||
searchInNonJavaFiles: Boolean,
|
||||
moveCallback: MoveCallback?,
|
||||
prepareSuccessfulCallback: Runnable = EmptyRunnable.INSTANCE
|
||||
) : MoveFilesOrDirectoriesProcessor(project,
|
||||
) : MoveFilesOrDirectoriesProcessor(
|
||||
project,
|
||||
arrayOf(sourceFile),
|
||||
targetDirectory,
|
||||
true,
|
||||
searchInComments,
|
||||
searchInNonJavaFiles,
|
||||
moveCallback,
|
||||
prepareSuccessfulCallback) {
|
||||
prepareSuccessfulCallback
|
||||
) {
|
||||
override fun getCommandName() = "Move ${sourceFile.name}"
|
||||
|
||||
override fun createUsageViewDescriptor(usages: Array<out UsageInfo>): UsageViewDescriptor {
|
||||
@@ -53,17 +55,7 @@ class MoveToKotlinFileProcessor @JvmOverloads constructor (
|
||||
}
|
||||
|
||||
override fun preprocessUsages(refUsages: Ref<Array<UsageInfo>>): Boolean {
|
||||
val usages = refUsages.get()
|
||||
|
||||
val (conflictUsages, usagesToProcess) = usages.partition { it is ConflictUsageInfo }
|
||||
|
||||
val conflicts = MultiMap<PsiElement, String>()
|
||||
for (conflictUsage in conflictUsages) {
|
||||
conflicts.putValues(conflictUsage.element, (conflictUsage as ConflictUsageInfo).messages)
|
||||
}
|
||||
|
||||
refUsages.set(usagesToProcess.toTypedArray())
|
||||
|
||||
val (conflicts, usages) = preprocessConflictUsages(refUsages)
|
||||
return showConflicts(conflicts, usages)
|
||||
}
|
||||
|
||||
@@ -85,11 +77,29 @@ class MoveToKotlinFileProcessor @JvmOverloads constructor (
|
||||
|
||||
try {
|
||||
super.performRefactoring(usages)
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
if (needTemporaryRename) {
|
||||
sourceFile.name = targetFileName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
data class ConflictUsages(val conflicts: MultiMap<PsiElement, String>, @Suppress("ArrayInDataClass") val usages: Array<UsageInfo>)
|
||||
|
||||
fun preprocessConflictUsages(refUsages: Ref<Array<UsageInfo>>): ConflictUsages {
|
||||
val usages: Array<UsageInfo> = refUsages.get()
|
||||
|
||||
val (conflictUsages, usagesToProcess) = usages.partition { it is ConflictUsageInfo }
|
||||
|
||||
val conflicts = MultiMap<PsiElement, String>()
|
||||
for (conflictUsage in conflictUsages) {
|
||||
conflicts.putValues(conflictUsage.element, (conflictUsage as ConflictUsageInfo).messages)
|
||||
}
|
||||
|
||||
refUsages.set(usagesToProcess.toTypedArray())
|
||||
|
||||
return ConflictUsages(conflicts, usages)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+13
-28
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
|
||||
@@ -62,6 +51,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.load.java.JavaVisibilities
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.jvm.isJvm
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy
|
||||
@@ -73,7 +63,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isSubclassOf
|
||||
import org.jetbrains.kotlin.platform.jvm.isJvm
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.lazy.descriptors.findPackageFragmentForFile
|
||||
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
|
||||
@@ -115,9 +104,7 @@ class MoveConflictChecker(
|
||||
|
||||
private fun KotlinMoveTarget.getContainerDescriptor(): DeclarationDescriptor? {
|
||||
return when (this) {
|
||||
is KotlinMoveTargetForExistingElement -> {
|
||||
val targetElement = targetElement
|
||||
when (targetElement) {
|
||||
is KotlinMoveTargetForExistingElement -> when (val targetElement = targetElement) {
|
||||
is KtNamedDeclaration -> resolutionFacade.resolveToDescriptor(targetElement)
|
||||
|
||||
is KtFile -> {
|
||||
@@ -131,7 +118,6 @@ class MoveConflictChecker(
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
is KotlinDirectoryBasedMoveTarget -> {
|
||||
val packageFqName = targetContainerFqName ?: return null
|
||||
@@ -159,8 +145,7 @@ class MoveConflictChecker(
|
||||
): DeclarationDescriptor? {
|
||||
if (newContainer == null && newVisibility == null) return this
|
||||
|
||||
val wrappedDescriptor = this
|
||||
return when (wrappedDescriptor) {
|
||||
return when (val wrappedDescriptor = this) {
|
||||
// We rely on visibility not depending on more specific type of CallableMemberDescriptor
|
||||
is CallableMemberDescriptor -> object : CallableMemberDescriptor by wrappedDescriptor {
|
||||
override fun getOriginal() = this
|
||||
@@ -255,7 +240,7 @@ class MoveConflictChecker(
|
||||
val referencedElementsToSkip = newConflicts.keySet().mapNotNullTo(HashSet()) { it.namedUnwrappedElement }
|
||||
externalUsages.removeIf {
|
||||
it is MoveRenameUsageInfo &&
|
||||
it.referencedElement?.namedUnwrappedElement?.let { it in referencedElementsToSkip } ?: false
|
||||
it.referencedElement?.namedUnwrappedElement?.let { element -> element in referencedElementsToSkip } ?: false
|
||||
}
|
||||
conflicts.putAllValues(newConflicts)
|
||||
}
|
||||
@@ -318,20 +303,20 @@ class MoveConflictChecker(
|
||||
val renderedImportableTarget = DESCRIPTOR_RENDERER_FOR_COMPARISON.render(importableDescriptor)
|
||||
val renderedTarget by lazy { DESCRIPTOR_RENDERER_FOR_COMPARISON.render(targetDescriptor) }
|
||||
|
||||
return newTargetDescriptors.any {
|
||||
if (DESCRIPTOR_RENDERER_FOR_COMPARISON.render(it) != renderedImportableTarget) return@any false
|
||||
return newTargetDescriptors.any { descriptor ->
|
||||
if (DESCRIPTOR_RENDERER_FOR_COMPARISON.render(descriptor) != renderedImportableTarget) return@any false
|
||||
if (importableDescriptor == targetDescriptor) return@any true
|
||||
|
||||
val candidateDescriptors: Collection<DeclarationDescriptor> = when (targetDescriptor) {
|
||||
is ConstructorDescriptor -> {
|
||||
(it as? ClassDescriptor)?.constructors ?: emptyList<DeclarationDescriptor>()
|
||||
(descriptor as? ClassDescriptor)?.constructors ?: emptyList()
|
||||
}
|
||||
|
||||
is PropertyAccessorDescriptor -> {
|
||||
(it as? PropertyDescriptor)
|
||||
(descriptor as? PropertyDescriptor)
|
||||
?.let { if (targetDescriptor is PropertyGetterDescriptor) it.getter else it.setter }
|
||||
?.let { listOf(it) }
|
||||
?: emptyList<DeclarationDescriptor>()
|
||||
?: emptyList()
|
||||
}
|
||||
|
||||
else -> emptyList()
|
||||
@@ -375,7 +360,7 @@ class MoveConflictChecker(
|
||||
referencesToSkip += refExpr
|
||||
}
|
||||
}
|
||||
internalUsages.removeIf { it.reference?.element?.let { it in referencesToSkip } ?: false }
|
||||
internalUsages.removeIf { it.reference?.element?.let { element -> element in referencesToSkip } ?: false }
|
||||
}
|
||||
|
||||
fun checkVisibilityInUsages(usages: Collection<UsageInfo>, conflicts: MultiMap<PsiElement, String>) {
|
||||
@@ -396,7 +381,7 @@ class MoveConflictChecker(
|
||||
) continue
|
||||
|
||||
val container = element.getUsageContext()
|
||||
if (!declarationToContainers.getOrPut(referencedElement) { HashSet<PsiElement>() }.add(container)) continue
|
||||
if (!declarationToContainers.getOrPut(referencedElement) { HashSet() }.add(container)) continue
|
||||
|
||||
val targetContainer = moveTarget.getContainerDescriptor() ?: continue
|
||||
|
||||
|
||||
+14
-12
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
@@ -46,8 +46,8 @@ class KotlinAwareMoveFilesOrDirectoriesDialog(
|
||||
private val callback: (KotlinAwareMoveFilesOrDirectoriesDialog?) -> Unit
|
||||
) : DialogWrapper(project, true) {
|
||||
companion object {
|
||||
private val RECENT_KEYS = "MoveFile.RECENT_KEYS"
|
||||
private val MOVE_FILES_OPEN_IN_EDITOR = "MoveFile.OpenInEditor"
|
||||
private const val RECENT_KEYS = "MoveFile.RECENT_KEYS"
|
||||
private const val MOVE_FILES_OPEN_IN_EDITOR = "MoveFile.OpenInEditor"
|
||||
}
|
||||
|
||||
private val nameLabel = JBLabelDecorator.createJBLabelDecorator().setBold(true)
|
||||
@@ -81,11 +81,13 @@ class KotlinAwareMoveFilesOrDirectoriesDialog(
|
||||
RecentsManager.getInstance(project).getRecentEntries(RECENT_KEYS)?.let { targetDirectoryField.childComponent.history = it }
|
||||
|
||||
val descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor()
|
||||
targetDirectoryField.addBrowseFolderListener(RefactoringBundle.message("select.target.directory"),
|
||||
targetDirectoryField.addBrowseFolderListener(
|
||||
RefactoringBundle.message("select.target.directory"),
|
||||
RefactoringBundle.message("the.file.will.be.moved.to.this.directory"),
|
||||
project,
|
||||
descriptor,
|
||||
TextComponentAccessor.TEXT_FIELD_WITH_HISTORY_WHOLE_TEXT)
|
||||
TextComponentAccessor.TEXT_FIELD_WITH_HISTORY_WHOLE_TEXT
|
||||
)
|
||||
val textField = targetDirectoryField.childComponent.textEditor
|
||||
FileChooserFactory.getInstance().installFileCompletion(textField, descriptor, true, disposable)
|
||||
textField.onTextChange { validateOKButton() }
|
||||
@@ -113,8 +115,7 @@ class KotlinAwareMoveFilesOrDirectoriesDialog(
|
||||
is PsiFile -> RefactoringBundle.message("move.file.0", shortenedPath)
|
||||
else -> RefactoringBundle.message("move.directory.0", shortenedPath)
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val isFile = psiElements.all { it is PsiFile }
|
||||
val isDirectory = psiElements.all { it is PsiDirectory }
|
||||
nameLabel.text = when {
|
||||
@@ -150,7 +151,7 @@ class KotlinAwareMoveFilesOrDirectoriesDialog(
|
||||
}
|
||||
|
||||
private fun validateOKButton() {
|
||||
isOKActionEnabled = targetDirectoryField.childComponent.text.length > 0
|
||||
isOKActionEnabled = targetDirectoryField.childComponent.text.isNotEmpty()
|
||||
}
|
||||
|
||||
override fun doOKAction() {
|
||||
@@ -172,17 +173,18 @@ class KotlinAwareMoveFilesOrDirectoriesDialog(
|
||||
}
|
||||
try {
|
||||
targetDirectory = DirectoryUtil.mkdirs(PsiManager.getInstance(project), directoryName)
|
||||
}
|
||||
catch (e: IncorrectOperationException) {
|
||||
} catch (e: IncorrectOperationException) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (targetDirectory == null) {
|
||||
CommonRefactoringUtil.showErrorMessage(title,
|
||||
CommonRefactoringUtil.showErrorMessage(
|
||||
title,
|
||||
RefactoringBundle.message("cannot.create.directory"),
|
||||
helpID,
|
||||
project)
|
||||
project
|
||||
)
|
||||
return@executeCommand
|
||||
}
|
||||
|
||||
|
||||
+6
-15
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui
|
||||
@@ -97,11 +86,13 @@ internal class KotlinSelectNestedClassRefactoringDialog private constructor (
|
||||
nestedClass: KtClassOrObject,
|
||||
targetContainer: PsiElement?
|
||||
): MoveKotlinNestedClassesDialog {
|
||||
return MoveKotlinNestedClassesDialog(nestedClass.project,
|
||||
return MoveKotlinNestedClassesDialog(
|
||||
nestedClass.project,
|
||||
listOf(nestedClass),
|
||||
nestedClass.containingClassOrObject!!,
|
||||
targetContainer as? KtClassOrObject ?: nestedClass.containingClassOrObject!!,
|
||||
null)
|
||||
null
|
||||
)
|
||||
}
|
||||
|
||||
fun chooseNestedClassRefactoring(nestedClass: KtClassOrObject, targetContainer: PsiElement?) {
|
||||
|
||||
+17
-42
@@ -1,23 +1,10 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui;
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.ide.util.ClassFilter;
|
||||
import com.intellij.ide.util.TreeClassChooser;
|
||||
import com.intellij.ide.util.TreeJavaClassChooserDialog;
|
||||
import com.intellij.openapi.editor.event.DocumentEvent;
|
||||
@@ -36,7 +23,6 @@ import com.intellij.refactoring.move.MoveCallback;
|
||||
import com.intellij.refactoring.move.MoveHandler;
|
||||
import com.intellij.refactoring.ui.RefactoringDialog;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
|
||||
@@ -59,8 +45,7 @@ import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.MoveKotlinDeclarationsProcessorKt.MoveSource;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
private static final String RECENTS_KEY = MoveKotlinNestedClassesDialog.class.getName() + ".RECENTS_KEY";
|
||||
@@ -74,6 +59,7 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
private JPanel targetClassChooserPanel;
|
||||
private KotlinMemberSelectionTable memberTable;
|
||||
private PsiElement targetClass;
|
||||
|
||||
public MoveKotlinNestedClassesDialog(
|
||||
@NotNull Project project,
|
||||
@NotNull List<KtClassOrObject> elementsToMove,
|
||||
@@ -111,9 +97,7 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
RefactoringBundle.message("choose.destination.class"),
|
||||
myProject,
|
||||
GlobalSearchScope.projectScope(myProject),
|
||||
new ClassFilter() {
|
||||
@Override
|
||||
public boolean isAccepted(PsiClass aClass) {
|
||||
aClass -> {
|
||||
if (!(aClass instanceof KtLightClassForSourceDeclaration)) return false;
|
||||
KtClassOrObject classOrObject = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
|
||||
|
||||
@@ -127,7 +111,6 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
},
|
||||
null,
|
||||
null,
|
||||
@@ -144,13 +127,14 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
return LightClassUtilsKt.toLightClass(((KtClassOrObjectTreeNode) userObject).getValue());
|
||||
}
|
||||
};
|
||||
chooser.selectDirectory((targetClass != null ? targetClass : originalClass).getContainingFile().getContainingDirectory());
|
||||
chooser.selectDirectory(
|
||||
(targetClass != null ? targetClass : originalClass).getContainingFile().getContainingDirectory());
|
||||
chooser.showDialog();
|
||||
|
||||
PsiClass aClass = chooser.getSelected();
|
||||
if (aClass instanceof KtLightClassForSourceDeclaration) {
|
||||
targetClass = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
|
||||
targetClassChooser.setText(aClass.getQualifiedName());
|
||||
targetClassChooser.setText(Objects.requireNonNull(aClass.getQualifiedName()));
|
||||
}
|
||||
else {
|
||||
targetClass = aClass;
|
||||
@@ -164,16 +148,13 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
if (codeFragment != null) {
|
||||
CompletionUtilsKt.setExtraCompletionFilter(
|
||||
codeFragment,
|
||||
new Function1<LookupElement, Boolean>() {
|
||||
@Override
|
||||
public Boolean invoke(LookupElement lookupElement) {
|
||||
lookupElement -> {
|
||||
Object lookupObject = lookupElement.getObject();
|
||||
if (!(lookupObject instanceof DeclarationLookupObject)) return false;
|
||||
PsiElement psiElement = ((DeclarationLookupObject) lookupObject).getPsiElement();
|
||||
if (lookupObject instanceof PackageLookupObject) return true;
|
||||
return (psiElement instanceof KtClassOrObject) && KotlinRefactoringUtilKt.canRefactor(psiElement);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
targetClassChooser.getChildComponent().getDocument().addDocumentListener(
|
||||
@@ -193,28 +174,27 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
targetClassChooserPanel.add(targetClassChooser);
|
||||
}
|
||||
|
||||
private void initMemberInfo(@NotNull final List<KtClassOrObject> elementsToMove) {
|
||||
private void initMemberInfo(@NotNull List<KtClassOrObject> elementsToMove) {
|
||||
List<KotlinMemberInfo> memberInfos = CollectionsKt.mapNotNull(
|
||||
originalClass.getDeclarations(),
|
||||
new Function1<KtDeclaration, KotlinMemberInfo>() {
|
||||
@Override
|
||||
public KotlinMemberInfo invoke(KtDeclaration declaration) {
|
||||
declaration -> {
|
||||
if (!(declaration instanceof KtClassOrObject)) return null;
|
||||
KtClassOrObject classOrObject = (KtClassOrObject) declaration;
|
||||
|
||||
if (classOrObject instanceof KtClass && ((KtClass) classOrObject).isInner()) return null;
|
||||
if (classOrObject instanceof KtObjectDeclaration && ((KtObjectDeclaration) classOrObject).isCompanion()) return null;
|
||||
if (classOrObject instanceof KtObjectDeclaration && ((KtObjectDeclaration) classOrObject).isCompanion()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
KotlinMemberInfo memberInfo = new KotlinMemberInfo(classOrObject, false);
|
||||
memberInfo.setChecked(elementsToMove.contains(declaration));
|
||||
return memberInfo;
|
||||
}
|
||||
}
|
||||
);
|
||||
KotlinMemberSelectionPanel selectionPanel = new KotlinMemberSelectionPanel(getTitle(), memberInfos, null);
|
||||
memberTable = selectionPanel.getTable();
|
||||
MemberInfoModelImpl memberInfoModel = new MemberInfoModelImpl();
|
||||
memberInfoModel.memberInfoChanged(new MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo>(memberInfos));
|
||||
memberInfoModel.memberInfoChanged(new MemberInfoChange<>(memberInfos));
|
||||
selectionPanel.getTable().setMemberInfoModel(memberInfoModel);
|
||||
selectionPanel.getTable().addMemberInfoChangeListener(memberInfoModel);
|
||||
membersInfoPanel.add(selectionPanel, BorderLayout.CENTER);
|
||||
@@ -223,12 +203,7 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
private List<KtClassOrObject> getSelectedElementsToMove() {
|
||||
return CollectionsKt.map(
|
||||
memberTable.getSelectedMemberInfos(),
|
||||
new Function1<KotlinMemberInfo, KtClassOrObject>() {
|
||||
@Override
|
||||
public KtClassOrObject invoke(KotlinMemberInfo info) {
|
||||
return (KtClassOrObject) info.getMember();
|
||||
}
|
||||
}
|
||||
info -> (KtClassOrObject) info.getMember()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -266,7 +241,7 @@ public class MoveKotlinNestedClassesDialog extends RefactoringDialog {
|
||||
MoveDeclarationsDelegate.NestedClass delegate = new MoveDeclarationsDelegate.NestedClass();
|
||||
MoveDeclarationsDescriptor descriptor = new MoveDeclarationsDescriptor(
|
||||
myProject,
|
||||
MoveSource(elementsToMove),
|
||||
MoveKotlinDeclarationsProcessorKt.MoveSource(elementsToMove),
|
||||
target,
|
||||
delegate,
|
||||
false,
|
||||
|
||||
+30
-57
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui;
|
||||
@@ -66,13 +55,9 @@ import org.jetbrains.kotlin.types.KotlinType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.MoveKotlinDeclarationsProcessorKt.MoveSource;
|
||||
|
||||
public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
@NonNls private static final String RECENTS_KEY = MoveKotlinNestedClassesToUpperLevelDialog.class.getName() + ".RECENTS_KEY";
|
||||
|
||||
@@ -160,7 +145,7 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
}
|
||||
|
||||
private boolean isThisNeeded() {
|
||||
return innerClass instanceof KtClass && MoveUtilsKt.traverseOuterInstanceReferences((KtClass) innerClass, true);
|
||||
return innerClass instanceof KtClass && MoveUtilsKt.traverseOuterInstanceReferences(innerClass, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -180,12 +165,7 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
|
||||
if (innerClass instanceof KtClass && ((KtClass) innerClass).isInner()) {
|
||||
passOuterClassCheckBox.setSelected(true);
|
||||
passOuterClassCheckBox.addItemListener(new ItemListener() {
|
||||
@Override
|
||||
public void itemStateChanged(ItemEvent e) {
|
||||
parameterField.setEnabled(passOuterClassCheckBox.isSelected());
|
||||
}
|
||||
});
|
||||
passOuterClassCheckBox.addItemListener(e -> parameterField.setEnabled(passOuterClassCheckBox.isSelected()));
|
||||
}
|
||||
else {
|
||||
passOuterClassCheckBox.setSelected(false);
|
||||
@@ -199,12 +179,9 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
parameterField.setEnabled(thisNeeded);
|
||||
}
|
||||
|
||||
passOuterClassCheckBox.addItemListener(new ItemListener() {
|
||||
@Override
|
||||
public void itemStateChanged(ItemEvent e) {
|
||||
passOuterClassCheckBox.addItemListener(e -> {
|
||||
boolean selected = passOuterClassCheckBox.isSelected();
|
||||
parameterField.getComponent().setEnabled(selected);
|
||||
}
|
||||
});
|
||||
|
||||
if (!(targetContainer instanceof PsiDirectory)) {
|
||||
@@ -218,7 +195,7 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
innerClassBody != null
|
||||
? new NewDeclarationNameValidator(innerClassBody, (PsiElement) null,
|
||||
NewDeclarationNameValidator.Target.VARIABLES,
|
||||
Collections.<KtDeclaration>emptyList())
|
||||
Collections.emptyList())
|
||||
: new CollectingNameValidator();
|
||||
List<String> suggestions = KotlinNameSuggester.INSTANCE.suggestNamesByType(getOuterInstanceType(), validator, "outer");
|
||||
parameterField.setSuggestions(ArrayUtil.toStringArray(suggestions));
|
||||
@@ -268,8 +245,8 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
if (!Comparing.equal(oldPackageFqName != null ? oldPackageFqName.asString() : null, targetName)) {
|
||||
ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
|
||||
List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
|
||||
final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(project), targetName);
|
||||
final VirtualFile targetSourceRoot;
|
||||
PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(project), targetName);
|
||||
VirtualFile targetSourceRoot;
|
||||
if (contentSourceRoots.size() > 1) {
|
||||
PsiDirectory initialDir = null;
|
||||
PsiPackage oldPackage = oldPackageFqName != null
|
||||
@@ -293,16 +270,13 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
}
|
||||
PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
|
||||
if (dir == null) {
|
||||
dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {
|
||||
@Override
|
||||
public PsiDirectory compute() {
|
||||
dir = ApplicationManager.getApplication().runWriteAction((NullableComputable<PsiDirectory>) () -> {
|
||||
try {
|
||||
return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return dir;
|
||||
@@ -321,12 +295,20 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
String className = getClassName();
|
||||
String parameterName = getParameterName();
|
||||
|
||||
if (className != null && className.isEmpty()) throw new ConfigurationException(RefactoringBundle.message("no.class.name.specified"));
|
||||
if (!KtPsiUtilKt.isIdentifier(className)) throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(className));
|
||||
if (className != null && className.isEmpty()) {
|
||||
throw new ConfigurationException(RefactoringBundle.message("no.class.name.specified"));
|
||||
}
|
||||
if (!KtPsiUtilKt.isIdentifier(className)) {
|
||||
throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(className));
|
||||
}
|
||||
|
||||
if (passOuterClassCheckBox.isSelected()) {
|
||||
if (parameterName != null && parameterName.isEmpty()) throw new ConfigurationException(RefactoringBundle.message("no.parameter.name.specified"));
|
||||
if (!KtPsiUtilKt.isIdentifier(parameterName)) throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(parameterName));
|
||||
if (parameterName != null && parameterName.isEmpty()) {
|
||||
throw new ConfigurationException(RefactoringBundle.message("no.parameter.name.specified"));
|
||||
}
|
||||
if (!KtPsiUtilKt.isIdentifier(parameterName)) {
|
||||
throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(parameterName));
|
||||
}
|
||||
}
|
||||
|
||||
PsiElement targetContainer = getTargetContainer();
|
||||
@@ -350,7 +332,9 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
.getPackage(targetPackageFqName)
|
||||
.getMemberScope()
|
||||
.getContributedClassifier(Name.identifier(className), NoLookupLocation.FROM_IDE);
|
||||
if (existingClass != null) throw new ConfigurationException("Class " + className + " already exists in package " + targetPackageFqName);
|
||||
if (existingClass != null) {
|
||||
throw new ConfigurationException("Class " + className + " already exists in package " + targetPackageFqName);
|
||||
}
|
||||
|
||||
PsiDirectory targetDir = targetContainer instanceof PsiDirectory
|
||||
? (PsiDirectory) targetContainer
|
||||
@@ -382,34 +366,23 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
|
||||
KotlinMoveTarget moveTarget;
|
||||
if (target instanceof PsiDirectory) {
|
||||
final PsiDirectory targetDir = (PsiDirectory) target;
|
||||
PsiDirectory targetDir = (PsiDirectory) target;
|
||||
|
||||
final FqName targetPackageFqName = getTargetPackageFqName(target);
|
||||
FqName targetPackageFqName = getTargetPackageFqName(target);
|
||||
if (targetPackageFqName == null) return;
|
||||
|
||||
final String targetFileName = KotlinNameSuggester.INSTANCE.suggestNameByName(
|
||||
String targetFileName = KotlinNameSuggester.INSTANCE.suggestNameByName(
|
||||
newClassName,
|
||||
new Function1<String, Boolean>() {
|
||||
@Override
|
||||
public Boolean invoke(String s) {
|
||||
return targetDir.findFile(s + "." + KotlinFileType.EXTENSION) == null;
|
||||
}
|
||||
}
|
||||
s -> targetDir.findFile(s + "." + KotlinFileType.EXTENSION) == null
|
||||
) + "." + KotlinFileType.EXTENSION;
|
||||
moveTarget = new KotlinMoveTargetForDeferredFile(
|
||||
targetPackageFqName,
|
||||
targetDir,
|
||||
null,
|
||||
new Function1<KtFile, KtFile>() {
|
||||
@Override
|
||||
public KtFile invoke(@NotNull KtFile originalFile) {
|
||||
return KotlinRefactoringUtilKt.createKotlinFile(targetFileName, targetDir, targetPackageFqName.asString());
|
||||
}
|
||||
}
|
||||
originalFile -> KotlinRefactoringUtilKt.createKotlinFile(targetFileName, targetDir, targetPackageFqName.asString())
|
||||
);
|
||||
}
|
||||
else {
|
||||
//noinspection ConstantConditions
|
||||
moveTarget = new KotlinMoveTargetForExistingElement((KtElement) target);
|
||||
}
|
||||
|
||||
@@ -417,7 +390,7 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
MoveDeclarationsDelegate delegate = new MoveDeclarationsDelegate.NestedClass(newClassName, outerInstanceParameterName);
|
||||
MoveDeclarationsDescriptor moveDescriptor = new MoveDeclarationsDescriptor(
|
||||
project,
|
||||
MoveSource(innerClass),
|
||||
MoveKotlinDeclarationsProcessorKt.MoveSource(innerClass),
|
||||
moveTarget,
|
||||
delegate,
|
||||
isSearchInComments(),
|
||||
|
||||
+44
-143
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui;
|
||||
@@ -32,8 +21,10 @@ import com.intellij.openapi.util.Pass;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.PsiFileImpl;
|
||||
import com.intellij.refactoring.*;
|
||||
import com.intellij.refactoring.classMembers.AbstractMemberInfoModel;
|
||||
import com.intellij.refactoring.classMembers.MemberInfoBase;
|
||||
import com.intellij.refactoring.classMembers.MemberInfoChange;
|
||||
import com.intellij.refactoring.classMembers.MemberInfoChangeListener;
|
||||
import com.intellij.refactoring.move.MoveCallback;
|
||||
@@ -46,12 +37,9 @@ import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.ui.ComboboxWithBrowseButton;
|
||||
import com.intellij.ui.RecentsManager;
|
||||
import com.intellij.ui.ReferenceEditorComboWithBrowseButton;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function0;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType;
|
||||
@@ -70,19 +58,17 @@ import org.jetbrains.kotlin.idea.util.application.ApplicationUtilsKt;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.psi.KtNamedDeclaration;
|
||||
import org.jetbrains.kotlin.psi.KtPureElement;
|
||||
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.MoveKotlinDeclarationsProcessorKt.MoveSource;
|
||||
|
||||
public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
private static final String RECENTS_KEY = "MoveKotlinTopLevelDeclarationsDialog.RECENTS_KEY";
|
||||
@@ -143,12 +129,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
return CollectionsKt.distinct(
|
||||
CollectionsKt.map(
|
||||
elementsToMove,
|
||||
new Function1<KtNamedDeclaration, KtFile>() {
|
||||
@Override
|
||||
public KtFile invoke(KtNamedDeclaration declaration) {
|
||||
return declaration.getContainingKtFile();
|
||||
}
|
||||
}
|
||||
KtPureElement::getContainingKtFile
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -159,12 +140,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
CollectionsKt.distinct(
|
||||
CollectionsKt.map(
|
||||
sourceFiles,
|
||||
new Function1<KtFile, PsiDirectory>() {
|
||||
@Override
|
||||
public PsiDirectory invoke(KtFile jetFile) {
|
||||
return jetFile.getParent();
|
||||
}
|
||||
}
|
||||
PsiFileImpl::getParent
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -174,12 +150,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
return CollectionsKt.filterIsInstance(
|
||||
CollectionsKt.flatMap(
|
||||
sourceFiles,
|
||||
new Function1<KtFile, Iterable<?>>() {
|
||||
@Override
|
||||
public Iterable<?> invoke(KtFile ktFile) {
|
||||
return KtPsiUtilKt.getFileOrScriptDeclarations(ktFile);
|
||||
}
|
||||
}
|
||||
KtPsiUtilKt::getFileOrScriptDeclarations
|
||||
),
|
||||
KtNamedDeclaration.class
|
||||
);
|
||||
@@ -196,7 +167,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
private static List<PsiFile> getFilesExistingInTargetDir(
|
||||
@NotNull List<KtFile> sourceFiles,
|
||||
@Nullable String targetFileName,
|
||||
@Nullable final PsiDirectory targetDirectory
|
||||
@Nullable PsiDirectory targetDirectory
|
||||
) {
|
||||
if (targetDirectory == null) return emptyList();
|
||||
|
||||
@@ -205,62 +176,44 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
? Collections.singletonList(targetFileName)
|
||||
: CollectionsKt.map(
|
||||
sourceFiles,
|
||||
new Function1<KtFile, String>() {
|
||||
@Override
|
||||
public String invoke(KtFile jetFile) {
|
||||
return jetFile.getName();
|
||||
}
|
||||
}
|
||||
PsiFileImpl::getName
|
||||
);
|
||||
|
||||
return CollectionsKt.filterNotNull(
|
||||
CollectionsKt.map(
|
||||
fileNames,
|
||||
new Function1<String, PsiFile>() {
|
||||
@Override
|
||||
public PsiFile invoke(String s) {
|
||||
return targetDirectory.findFile(s);
|
||||
}
|
||||
}
|
||||
targetDirectory::findFile
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private void initMemberInfo(
|
||||
@NotNull final Set<KtNamedDeclaration> elementsToMove,
|
||||
@NotNull Set<KtNamedDeclaration> elementsToMove,
|
||||
@NotNull List<KtFile> sourceFiles
|
||||
) {
|
||||
final List<KotlinMemberInfo> memberInfos = CollectionsKt.map(
|
||||
List<KotlinMemberInfo> memberInfos = CollectionsKt.map(
|
||||
getAllDeclarations(sourceFiles),
|
||||
new Function1<KtNamedDeclaration, KotlinMemberInfo>() {
|
||||
@Override
|
||||
public KotlinMemberInfo invoke(KtNamedDeclaration declaration) {
|
||||
declaration -> {
|
||||
KotlinMemberInfo memberInfo = new KotlinMemberInfo(declaration, false);
|
||||
memberInfo.setChecked(elementsToMove.contains(declaration));
|
||||
return memberInfo;
|
||||
}
|
||||
}
|
||||
);
|
||||
KotlinMemberSelectionPanel selectionPanel = new KotlinMemberSelectionPanel(getTitle(), memberInfos, null);
|
||||
memberTable = selectionPanel.getTable();
|
||||
MemberInfoModelImpl memberInfoModel = new MemberInfoModelImpl();
|
||||
memberInfoModel.memberInfoChanged(new MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo>(memberInfos));
|
||||
memberInfoModel.memberInfoChanged(new MemberInfoChange<>(memberInfos));
|
||||
selectionPanel.getTable().setMemberInfoModel(memberInfoModel);
|
||||
selectionPanel.getTable().addMemberInfoChangeListener(memberInfoModel);
|
||||
selectionPanel.getTable().addMemberInfoChangeListener(
|
||||
new MemberInfoChangeListener<KtNamedDeclaration, KotlinMemberInfo>() {
|
||||
private boolean shouldUpdateFileNameField(final Collection<KotlinMemberInfo> changedMembers) {
|
||||
private boolean shouldUpdateFileNameField(Collection<KotlinMemberInfo> changedMembers) {
|
||||
if (!tfFileNameInPackage.isEnabled()) return true;
|
||||
|
||||
Collection<KtNamedDeclaration> previousDeclarations = CollectionsKt.filterNotNull(
|
||||
CollectionsKt.map(
|
||||
memberInfos,
|
||||
new Function1<KotlinMemberInfo, KtNamedDeclaration>() {
|
||||
@Override
|
||||
public KtNamedDeclaration invoke(KotlinMemberInfo info) {
|
||||
return changedMembers.contains(info) != info.isChecked() ? info.getMember() : null;
|
||||
}
|
||||
}
|
||||
info -> changedMembers.contains(info) != info.isChecked() ? info.getMember() : null
|
||||
)
|
||||
);
|
||||
String suggestedText = previousDeclarations.isEmpty()
|
||||
@@ -270,7 +223,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void memberInfoChanged(MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo> event) {
|
||||
public void memberInfoChanged(@NotNull MemberInfoChange<KtNamedDeclaration, KotlinMemberInfo> event) {
|
||||
updatePackageDirectiveCheckBox();
|
||||
updateFileNameInPackageField();
|
||||
// Update file name field only if it user hasn't changed it to some non-default value
|
||||
@@ -285,7 +238,6 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
|
||||
private void updateSuggestedFileName() {
|
||||
tfFileNameInPackage.setText(MoveUtilsKt.guessNewFileName(getSelectedElementsToMove()));
|
||||
|
||||
}
|
||||
|
||||
private void updateFileNameInPackageField() {
|
||||
@@ -316,14 +268,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
classPackageChooser.getChildComponent()
|
||||
);
|
||||
|
||||
cbSpecifyFileNameInPackage.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(@NotNull ActionEvent e) {
|
||||
updateFileNameInPackageField();
|
||||
}
|
||||
}
|
||||
);
|
||||
cbSpecifyFileNameInPackage.addActionListener(e -> updateFileNameInPackageField());
|
||||
|
||||
cbUpdatePackageDirective.setSelected(arePackagesAndDirectoryMatched(sourceFiles));
|
||||
}
|
||||
@@ -342,23 +287,17 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
}
|
||||
|
||||
rbMoveToPackage.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(@NotNull ActionEvent e) {
|
||||
e -> {
|
||||
classPackageChooser.requestFocus();
|
||||
updateControls();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
rbMoveToFile.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(@NotNull ActionEvent e) {
|
||||
e -> {
|
||||
fileChooser.requestFocus();
|
||||
updateControls();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -367,22 +306,20 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
@NotNull Set<KtNamedDeclaration> elementsToMove,
|
||||
@NotNull List<KtFile> sourceFiles
|
||||
) {
|
||||
final PsiDirectory sourceDir = sourceFiles.get(0).getParent();
|
||||
PsiDirectory sourceDir = sourceFiles.get(0).getParent();
|
||||
assert sourceDir != null : sourceFiles.get(0).getVirtualFile().getPath();
|
||||
|
||||
fileChooser.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
e -> {
|
||||
KotlinFileChooserDialog dialog = new KotlinFileChooserDialog("Choose Containing File", myProject);
|
||||
|
||||
File targetFile = new File(getTargetFilePath());
|
||||
PsiFile targetPsiFile = PhysicalFileSystemUtilsKt.toPsiFile(targetFile, myProject);
|
||||
File targetFile1 = new File(getTargetFilePath());
|
||||
PsiFile targetPsiFile = PhysicalFileSystemUtilsKt.toPsiFile(targetFile1, myProject);
|
||||
if (targetPsiFile instanceof KtFile) {
|
||||
dialog.select((KtFile) targetPsiFile);
|
||||
}
|
||||
else {
|
||||
PsiDirectory targetDir = PhysicalFileSystemUtilsKt.toPsiDirectory(targetFile.getParentFile(), myProject);
|
||||
PsiDirectory targetDir = PhysicalFileSystemUtilsKt.toPsiDirectory(targetFile1.getParentFile(), myProject);
|
||||
if (targetDir == null) {
|
||||
targetDir = sourceDir;
|
||||
}
|
||||
@@ -395,7 +332,6 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
fileChooser.setText(selectedFile.getVirtualFile().getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
String initialTargetPath =
|
||||
@@ -446,12 +382,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
private boolean isFullFileMove() {
|
||||
Map<KtFile, List<KtNamedDeclaration>> fileToElements = CollectionsKt.groupBy(
|
||||
getSelectedElementsToMove(),
|
||||
new Function1<KtNamedDeclaration, KtFile>() {
|
||||
@Override
|
||||
public KtFile invoke(KtNamedDeclaration declaration) {
|
||||
return declaration.getContainingKtFile();
|
||||
}
|
||||
}
|
||||
KtPureElement::getContainingKtFile
|
||||
);
|
||||
for (Map.Entry<KtFile, List<KtNamedDeclaration>> entry : fileToElements.entrySet()) {
|
||||
if (KtPsiUtilKt.getFileOrScriptDeclarations(entry.getKey()).size() != entry.getValue().size()) return false;
|
||||
@@ -525,9 +456,9 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
if (targetDirWithMoveDestination == null) return null;
|
||||
|
||||
VirtualFile targetDir = targetDirWithMoveDestination.getFirst();
|
||||
final MoveDestination moveDestination = targetDirWithMoveDestination.getSecond();
|
||||
MoveDestination moveDestination = targetDirWithMoveDestination.getSecond();
|
||||
|
||||
final String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
|
||||
String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
|
||||
if (targetFileName != null && !checkTargetFileName(targetFileName)) return null;
|
||||
|
||||
PsiDirectory targetDirectory = moveDestination.getTargetIfExists(sourceDirectory);
|
||||
@@ -537,12 +468,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
if (filesExistingInTargetDir.size() > 1) {
|
||||
String filePathsToReport = StringUtil.join(
|
||||
filesExistingInTargetDir,
|
||||
new Function<PsiFile, String>() {
|
||||
@Override
|
||||
public String fun(PsiFile file) {
|
||||
return file.getVirtualFile().getPath();
|
||||
}
|
||||
},
|
||||
file -> file.getVirtualFile().getPath(),
|
||||
"\n"
|
||||
);
|
||||
Messages.showErrorDialog(
|
||||
@@ -576,19 +502,14 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
new FqName(getTargetPackage()),
|
||||
moveDestination.getTargetIfExists(sourceFiles.get(0)),
|
||||
targetDir,
|
||||
new Function1<KtFile, KtFile>() {
|
||||
@Override
|
||||
public KtFile invoke(@NotNull KtFile originalFile) {
|
||||
return KotlinRefactoringUtilKt.getOrCreateKotlinFile(
|
||||
originalFile -> KotlinRefactoringUtilKt.getOrCreateKotlinFile(
|
||||
targetFileName != null ? targetFileName : originalFile.getName(),
|
||||
moveDestination.getTargetDirectory(originalFile)
|
||||
);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
final File targetFile = new File(getTargetFilePath());
|
||||
File targetFile = new File(getTargetFilePath());
|
||||
if (!checkTargetFileName(targetFile.getName())) return null;
|
||||
KtFile jetFile = (KtFile) PhysicalFileSystemUtilsKt.toPsiFile(targetFile, myProject);
|
||||
if (jetFile != null) {
|
||||
@@ -602,7 +523,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
|
||||
Path targetFilePath = targetFile.toPath();
|
||||
Path targetDirPath = targetFilePath.getParent();
|
||||
if (targetDirPath == null || !targetDirPath.startsWith(getProject().getBasePath())) {
|
||||
if (targetDirPath == null || !targetDirPath.startsWith(Objects.requireNonNull(getProject().getBasePath()))) {
|
||||
setErrorText("Incorrect target path. Directory " + targetDirPath + " does not belong to current project.");
|
||||
return null;
|
||||
}
|
||||
@@ -625,7 +546,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
}
|
||||
|
||||
File targetDir = targetDirPath.toFile();
|
||||
final PsiDirectory psiDirectory = targetDir != null ? PhysicalFileSystemUtilsKt.toPsiDirectory(targetDir, myProject) : null;
|
||||
PsiDirectory psiDirectory = targetDir != null ? PhysicalFileSystemUtilsKt.toPsiDirectory(targetDir, myProject) : null;
|
||||
if (psiDirectory == null) {
|
||||
setErrorText("No directory found for file: " + targetFile.getPath());
|
||||
return null;
|
||||
@@ -633,13 +554,8 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
|
||||
Set<FqName> sourcePackageFqNames = CollectionsKt.mapTo(
|
||||
sourceFiles,
|
||||
new LinkedHashSet<FqName>(),
|
||||
new Function1<KtFile, FqName>() {
|
||||
@Override
|
||||
public FqName invoke(KtFile file) {
|
||||
return file.getPackageFqName();
|
||||
}
|
||||
}
|
||||
new LinkedHashSet<>(),
|
||||
KtFile::getPackageFqName
|
||||
);
|
||||
FqName targetPackageFqName = CollectionsKt.singleOrNull(sourcePackageFqNames);
|
||||
if (targetPackageFqName == null) {
|
||||
@@ -651,17 +567,12 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
targetPackageFqName = new FqName(psiPackage.getQualifiedName());
|
||||
}
|
||||
|
||||
final String finalTargetPackageFqName = targetPackageFqName.asString();
|
||||
String finalTargetPackageFqName = targetPackageFqName.asString();
|
||||
return new KotlinMoveTargetForDeferredFile(
|
||||
targetPackageFqName,
|
||||
psiDirectory,
|
||||
null,
|
||||
new Function1<KtFile, KtFile>() {
|
||||
@Override
|
||||
public KtFile invoke(@NotNull KtFile originalFile) {
|
||||
return KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFile.getName(), psiDirectory, finalTargetPackageFqName);
|
||||
}
|
||||
}
|
||||
originalFile -> KotlinRefactoringUtilKt.getOrCreateKotlinFile(targetFile.getName(), psiDirectory, finalTargetPackageFqName)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -692,12 +603,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
private List<KtNamedDeclaration> getSelectedElementsToMove() {
|
||||
return CollectionsKt.map(
|
||||
memberTable.getSelectedMemberInfos(),
|
||||
new Function1<KotlinMemberInfo, KtNamedDeclaration>() {
|
||||
@Override
|
||||
public KtNamedDeclaration invoke(KotlinMemberInfo info) {
|
||||
return info.getMember();
|
||||
}
|
||||
}
|
||||
MemberInfoBase::getMember
|
||||
);
|
||||
}
|
||||
|
||||
@@ -736,7 +642,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
|
||||
List<KtNamedDeclaration> elementsToMove = getSelectedElementsToMove();
|
||||
List<KtFile> sourceFiles = getSourceFiles(elementsToMove);
|
||||
final PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
|
||||
PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
|
||||
|
||||
for (PsiElement element : elementsToMove) {
|
||||
String message = target.verify(element.getContainingFile());
|
||||
@@ -754,7 +660,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
Pair<VirtualFile, ? extends MoveDestination> sourceRootWithMoveDestination =
|
||||
selectPackageBasedTargetDirAndDestination(false);
|
||||
//noinspection ConstantConditions
|
||||
final MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
|
||||
MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
|
||||
|
||||
PsiDirectory targetDir = moveDestination.getTargetIfExists(sourceDirectory);
|
||||
String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
|
||||
@@ -762,12 +668,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
if (filesExistingInTargetDir.isEmpty()
|
||||
|| (filesExistingInTargetDir.size() == 1 && sourceFiles.contains(filesExistingInTargetDir.get(0)))) {
|
||||
PsiDirectory targetDirectory = ApplicationUtilsKt.runWriteAction(
|
||||
new Function0<PsiDirectory>() {
|
||||
@Override
|
||||
public PsiDirectory invoke() {
|
||||
return moveDestination.getTargetDirectory(sourceDirectory);
|
||||
}
|
||||
}
|
||||
() -> moveDestination.getTargetDirectory(sourceDirectory)
|
||||
);
|
||||
|
||||
for (KtFile sourceFile : sourceFiles) {
|
||||
@@ -809,7 +710,7 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
|
||||
|
||||
MoveDeclarationsDescriptor options = new MoveDeclarationsDescriptor(
|
||||
myProject,
|
||||
MoveSource(elementsToMove),
|
||||
MoveKotlinDeclarationsProcessorKt.MoveSource(elementsToMove),
|
||||
target,
|
||||
MoveDeclarationsDelegate.TopLevel.INSTANCE,
|
||||
isSearchInComments(),
|
||||
|
||||
+8
-15
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories
|
||||
@@ -33,8 +22,12 @@ class FqNameFixingMoveJavaFileHandler : MoveFileHandler() {
|
||||
override fun canProcessElement(element: PsiFile) =
|
||||
delegate.canProcessElement(element)
|
||||
|
||||
override fun findUsages(psiFile: PsiFile, newParent: PsiDirectory?, searchInComments: Boolean, searchInNonJavaFiles: Boolean) =
|
||||
delegate.findUsages(psiFile, newParent, searchInComments, searchInNonJavaFiles)
|
||||
override fun findUsages(
|
||||
psiFile: PsiFile,
|
||||
newParent: PsiDirectory?,
|
||||
searchInComments: Boolean,
|
||||
searchInNonJavaFiles: Boolean
|
||||
): MutableList<UsageInfo>? = delegate.findUsages(psiFile, newParent, searchInComments, searchInNonJavaFiles)
|
||||
|
||||
override fun prepareMovedFile(file: PsiFile, moveDestination: PsiDirectory, oldToNewMap: MutableMap<PsiElement, PsiElement>) {
|
||||
delegate.prepareMovedFile(file, moveDestination, oldToNewMap)
|
||||
|
||||
+5
-16
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories
|
||||
@@ -67,7 +56,8 @@ class KotlinMoveDirectoryWithClassesHelper : MoveDirectoryWithClassesHelper() {
|
||||
result: MutableCollection<UsageInfo>,
|
||||
searchInComments: Boolean,
|
||||
searchInNonJavaFiles: Boolean,
|
||||
project: Project) {
|
||||
project: Project
|
||||
) {
|
||||
filesToMove
|
||||
.filterIsInstance<KtFile>()
|
||||
.mapTo(result) { FileUsagesWrapper(it, fileHandler.findUsages(it, null, false), null) }
|
||||
@@ -142,8 +132,7 @@ class KotlinMoveDirectoryWithClassesHelper : MoveDirectoryWithClassesHelper() {
|
||||
usagesToProcess += FileUsagesWrapper(movedFile as KtFile, it.usages, moveDeclarationsProcessor)
|
||||
}
|
||||
usagesToProcess.forEach { fileHandler.retargetUsages(it.usages, it.moveDeclarationsProcessor!!) }
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
this.fileToMoveContext = null
|
||||
}
|
||||
}
|
||||
|
||||
+9
-14
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories
|
||||
@@ -62,7 +51,13 @@ class KotlinMoveFilesOrDirectoriesHandler : MoveFilesOrDirectoriesHandler() {
|
||||
)
|
||||
}
|
||||
|
||||
override fun tryToMove(element: PsiElement, project: Project, dataContext: DataContext?, reference: PsiReference?, editor: Editor?): Boolean {
|
||||
override fun tryToMove(
|
||||
element: PsiElement,
|
||||
project: Project,
|
||||
dataContext: DataContext?,
|
||||
reference: PsiReference?,
|
||||
editor: Editor?
|
||||
): Boolean {
|
||||
if (element is KtLightClassForFacade) {
|
||||
doMove(project, element.files.toTypedArray(), dataContext?.getData(LangDataKeys.TARGET_PSI_ELEMENT), null)
|
||||
return true
|
||||
|
||||
+9
-18
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories
|
||||
@@ -61,12 +50,15 @@ class MoveKotlinFileHandler : MoveFileHandler() {
|
||||
if (!shouldUpdatePackageDirective) return null
|
||||
|
||||
val oldPackageName = packageFqName
|
||||
val newPackage = newParent?.getPackage() ?: return ContainerChangeInfo(ContainerInfo.Package(oldPackageName),
|
||||
ContainerInfo.UnknownPackage)
|
||||
val newPackage = newParent?.getPackage() ?: return ContainerChangeInfo(
|
||||
ContainerInfo.Package(oldPackageName),
|
||||
ContainerInfo.UnknownPackage
|
||||
)
|
||||
|
||||
val newPackageName = FqNameUnsafe(newPackage.qualifiedName)
|
||||
if (oldPackageName.asString() == newPackageName.asString()
|
||||
&& ModuleUtilCore.findModuleForPsiElement(this) == ModuleUtilCore.findModuleForPsiElement(newParent)) return null
|
||||
&& ModuleUtilCore.findModuleForPsiElement(this) == ModuleUtilCore.findModuleForPsiElement(newParent)
|
||||
) return null
|
||||
if (!newPackageName.hasIdentifiersOnly()) return null
|
||||
|
||||
return ContainerChangeInfo(ContainerInfo.Package(oldPackageName), ContainerInfo.Package(newPackageName.toSafe()))
|
||||
@@ -78,8 +70,7 @@ class MoveKotlinFileHandler : MoveFileHandler() {
|
||||
|
||||
val project = psiFile.project
|
||||
|
||||
val newPackage = packageNameInfo.newContainer
|
||||
val moveTarget = when (newPackage) {
|
||||
val moveTarget = when (val newPackage = packageNameInfo.newContainer) {
|
||||
ContainerInfo.UnknownPackage -> EmptyKotlinMoveTarget
|
||||
|
||||
else -> KotlinMoveTargetForDeferredFile(newPackage.fqName!!, newParent) {
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring.move
|
||||
@@ -64,7 +53,7 @@ sealed class ContainerInfo {
|
||||
abstract fun matches(descriptor: DeclarationDescriptor): Boolean
|
||||
|
||||
object UnknownPackage : ContainerInfo() {
|
||||
override val fqName = null
|
||||
override val fqName: FqName? = null
|
||||
override fun matches(descriptor: DeclarationDescriptor) = descriptor is PackageViewDescriptor
|
||||
}
|
||||
|
||||
@@ -111,8 +100,7 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
||||
val fqName = DescriptorUtils.getFqName(descriptor).let { if (it.isSafe) it.toSafe() else return@isImported false }
|
||||
if (importPaths.any { fqName.isImported(it, false) }) return true
|
||||
|
||||
val containingDescriptor = descriptor.containingDeclaration
|
||||
return when (containingDescriptor) {
|
||||
return when (val containingDescriptor = descriptor.containingDeclaration) {
|
||||
is ClassDescriptor, is PackageViewDescriptor -> isImported(containingDescriptor)
|
||||
else -> false
|
||||
}
|
||||
@@ -138,7 +126,8 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
||||
var result = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) ?: return@lazy null
|
||||
|
||||
if (descriptor.isCompanionObject()
|
||||
&& bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, refExpr] != null) {
|
||||
&& bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, refExpr] != null
|
||||
) {
|
||||
result = (result as KtObjectDeclaration).containingClassOrObject ?: result
|
||||
}
|
||||
|
||||
@@ -150,8 +139,8 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
||||
if (isExtension && containingDescriptor is ClassDescriptor) {
|
||||
val dispatchReceiver = refExpr.getResolvedCall(bindingContext)?.dispatchReceiver
|
||||
val implicitClass = (dispatchReceiver as? ImplicitClassReceiver)?.classDescriptor
|
||||
if (implicitClass?.isCompanionObject ?: false) {
|
||||
return { ImplicitCompanionAsDispatchReceiverUsageInfo(it, implicitClass!!) }
|
||||
if (implicitClass?.isCompanionObject == true) {
|
||||
return { ImplicitCompanionAsDispatchReceiverUsageInfo(it, implicitClass) }
|
||||
}
|
||||
if (dispatchReceiver != null || containingDescriptor.kind != ClassKind.OBJECT) return null
|
||||
}
|
||||
@@ -160,7 +149,8 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
||||
if (!isExtension) {
|
||||
if (!(containingDescriptor is PackageFragmentDescriptor
|
||||
|| containingDescriptor is ClassDescriptor && containingDescriptor.kind == ClassKind.OBJECT
|
||||
|| descriptor is JavaCallableMemberDescriptor && ((declaration as? PsiMember)?.hasModifierProperty(PsiModifier.STATIC) ?: false))) return null
|
||||
|| descriptor is JavaCallableMemberDescriptor && ((declaration as? PsiMember)?.hasModifierProperty(PsiModifier.STATIC) == true))
|
||||
) return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,9 +233,23 @@ class UnqualifiableMoveRenameUsageInfo(
|
||||
val originalFile: PsiFile,
|
||||
val addImportToOriginalFile: Boolean,
|
||||
override val isInternal: Boolean
|
||||
): MoveRenameUsageInfo(element, reference, reference.rangeInElement.startOffset, reference.rangeInElement.endOffset, referencedElement, false), KotlinMoveUsage {
|
||||
) : MoveRenameUsageInfo(
|
||||
element,
|
||||
reference,
|
||||
reference.rangeInElement.startOffset,
|
||||
reference.rangeInElement.endOffset,
|
||||
referencedElement,
|
||||
false
|
||||
), KotlinMoveUsage {
|
||||
override fun refresh(refExpr: KtSimpleNameExpression, referencedElement: PsiElement): UsageInfo? {
|
||||
return UnqualifiableMoveRenameUsageInfo(refExpr, refExpr.mainReference, referencedElement, originalFile, addImportToOriginalFile, isInternal)
|
||||
return UnqualifiableMoveRenameUsageInfo(
|
||||
refExpr,
|
||||
refExpr.mainReference,
|
||||
referencedElement,
|
||||
originalFile,
|
||||
addImportToOriginalFile,
|
||||
isInternal
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,7 +258,14 @@ class QualifiableMoveRenameUsageInfo(
|
||||
reference: PsiReference,
|
||||
referencedElement: PsiElement,
|
||||
override val isInternal: Boolean
|
||||
): MoveRenameUsageInfo(element, reference, reference.rangeInElement.startOffset, reference.rangeInElement.endOffset, referencedElement, false),
|
||||
) : MoveRenameUsageInfo(
|
||||
element,
|
||||
reference,
|
||||
reference.rangeInElement.startOffset,
|
||||
reference.rangeInElement.endOffset,
|
||||
referencedElement,
|
||||
false
|
||||
),
|
||||
KotlinMoveUsage {
|
||||
override fun refresh(refExpr: KtSimpleNameExpression, referencedElement: PsiElement): UsageInfo? {
|
||||
return QualifiableMoveRenameUsageInfo(refExpr, refExpr.mainReference, referencedElement, isInternal)
|
||||
@@ -272,9 +283,23 @@ class CallableReferenceMoveRenameUsageInfo(
|
||||
val originalFile: PsiFile,
|
||||
val addImportToOriginalFile: Boolean,
|
||||
override val isInternal: Boolean
|
||||
) : MoveRenameUsageInfo(element, reference, reference.rangeInElement.startOffset, reference.rangeInElement.endOffset, referencedElement, false), DeferredKotlinMoveUsage {
|
||||
) : MoveRenameUsageInfo(
|
||||
element,
|
||||
reference,
|
||||
reference.rangeInElement.startOffset,
|
||||
reference.rangeInElement.endOffset,
|
||||
referencedElement,
|
||||
false
|
||||
), DeferredKotlinMoveUsage {
|
||||
override fun refresh(refExpr: KtSimpleNameExpression, referencedElement: PsiElement): UsageInfo? {
|
||||
return CallableReferenceMoveRenameUsageInfo(refExpr, refExpr.mainReference, referencedElement, originalFile, addImportToOriginalFile, isInternal)
|
||||
return CallableReferenceMoveRenameUsageInfo(
|
||||
refExpr,
|
||||
refExpr.mainReference,
|
||||
referencedElement,
|
||||
originalFile,
|
||||
addImportToOriginalFile,
|
||||
isInternal
|
||||
)
|
||||
}
|
||||
|
||||
override fun resolve(newElement: PsiElement): UsageInfo? {
|
||||
@@ -284,7 +309,14 @@ class CallableReferenceMoveRenameUsageInfo(
|
||||
val referencedElement = referencedElement ?: return null
|
||||
if (target != null && target.isTopLevelKtOrJavaMember()) {
|
||||
element.getStrictParentOfType<KtCallableReferenceExpression>()?.receiverExpression?.delete()
|
||||
return UnqualifiableMoveRenameUsageInfo(element, reference, referencedElement, element.containingFile!!, addImportToOriginalFile, isInternal)
|
||||
return UnqualifiableMoveRenameUsageInfo(
|
||||
element,
|
||||
reference,
|
||||
referencedElement,
|
||||
element.containingFile!!,
|
||||
addImportToOriginalFile,
|
||||
isInternal
|
||||
)
|
||||
}
|
||||
return QualifiableMoveRenameUsageInfo(element, reference, referencedElement, isInternal)
|
||||
}
|
||||
@@ -384,7 +416,8 @@ private fun updateJavaReference(reference: PsiReferenceExpression, oldElement: P
|
||||
if (newClass != null && reference.qualifierExpression != null) {
|
||||
val mockMoveMembersOptions = MockMoveMembersOptions(newClass.qualifiedName, arrayOf(newElement))
|
||||
val moveMembersUsageInfo = MoveMembersProcessor.MoveMembersUsageInfo(
|
||||
newElement, reference.element, newClass, reference.qualifierExpression, reference)
|
||||
newElement, reference.element, newClass, reference.qualifierExpression, reference
|
||||
)
|
||||
val moveMemberHandler = MoveMemberHandler.EP_NAME.forLanguage(reference.element.language)
|
||||
if (moveMemberHandler != null) {
|
||||
moveMemberHandler.changeExternalUsage(mockMoveMembersOptions, moveMembersUsageInfo)
|
||||
@@ -420,7 +453,12 @@ private fun postProcessMoveUsage(
|
||||
}
|
||||
|
||||
is UnqualifiableMoveRenameUsageInfo -> {
|
||||
val file = with(usage) { if (addImportToOriginalFile) originalFile else mapToNewOrThis(originalFile, oldToNewElementsMapping) } as KtFile
|
||||
val file = with(usage) {
|
||||
if (addImportToOriginalFile) originalFile else mapToNewOrThis(
|
||||
originalFile,
|
||||
oldToNewElementsMapping
|
||||
)
|
||||
} as KtFile
|
||||
addDelayedImportRequest(newElement, file)
|
||||
}
|
||||
|
||||
@@ -438,8 +476,7 @@ private fun processReference(reference: PsiReference?, newElement: PsiElement, s
|
||||
reference is PsiReferenceExpression && updateJavaReference(reference, oldElement, newElement) -> return
|
||||
else -> reference?.bindToElement(newElement)
|
||||
}
|
||||
}
|
||||
catch (e: IncorrectOperationException) {
|
||||
} catch (e: IncorrectOperationException) {
|
||||
// Suppress exception if bindToElement is not implemented
|
||||
}
|
||||
}
|
||||
@@ -453,7 +490,7 @@ fun postProcessMoveUsages(
|
||||
shorteningMode: ShorteningMode = ShorteningMode.DELAYED_SHORTENING
|
||||
): List<NonCodeUsageInfo> {
|
||||
val sortedUsages = usages.sortedWith(
|
||||
Comparator<UsageInfo> { o1, o2 ->
|
||||
Comparator { o1, o2 ->
|
||||
val file1 = o1.virtualFile
|
||||
val file2 = o2.virtualFile
|
||||
if (Comparing.equal(file1, file2)) {
|
||||
@@ -535,7 +572,11 @@ sealed class OuterInstanceReferenceUsageInfo(element: PsiElement, private val is
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun traverseOuterInstanceReferences(member: KtNamedDeclaration, stopAtFirst: Boolean, body: (OuterInstanceReferenceUsageInfo) -> Unit = {}): Boolean {
|
||||
fun traverseOuterInstanceReferences(
|
||||
member: KtNamedDeclaration,
|
||||
stopAtFirst: Boolean,
|
||||
body: (OuterInstanceReferenceUsageInfo) -> Unit = {}
|
||||
): Boolean {
|
||||
if (member is KtObjectDeclaration || member is KtClass && !member.isInner()) return false
|
||||
|
||||
val context = member.analyzeWithContent()
|
||||
|
||||
@@ -24,10 +24,12 @@ import org.jetbrains.kotlin.idea.core.util.toPsiDirectory
|
||||
import org.jetbrains.kotlin.idea.core.util.toPsiFile
|
||||
import org.jetbrains.kotlin.idea.jsonUtils.getNullableString
|
||||
import org.jetbrains.kotlin.idea.jsonUtils.getString
|
||||
import org.jetbrains.kotlin.idea.refactoring.*
|
||||
import org.jetbrains.kotlin.idea.refactoring.AbstractMultifileRefactoringTest
|
||||
import org.jetbrains.kotlin.idea.refactoring.createKotlinFile
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.changePackage.KotlinChangePackageRefactoring
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.moveClassesOrPackages.KotlinAwareDelegatingMoveDestination
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.*
|
||||
import org.jetbrains.kotlin.idea.refactoring.runRefactoringTest
|
||||
import org.jetbrains.kotlin.idea.search.allScope
|
||||
import org.jetbrains.kotlin.idea.search.projectScope
|
||||
import org.jetbrains.kotlin.idea.stubindex.KotlinFullClassNameIndex
|
||||
@@ -97,8 +99,7 @@ enum class MoveAction : AbstractMultifileRefactoringTest.RefactoringAction {
|
||||
targetPackage,
|
||||
targetDirectory
|
||||
)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
MultipleRootsMoveDestination(targetPackageWrapper)
|
||||
}
|
||||
|
||||
@@ -161,8 +162,7 @@ enum class MoveAction : AbstractMultifileRefactoringTest.RefactoringAction {
|
||||
ActionRunner.runInsideWriteAction { VfsUtil.createDirectoryIfMissing(rootDir, targetDirPath) }
|
||||
val newParent = if (targetPackage != null) {
|
||||
JavaPsiFacade.getInstance(project).findPackage(targetPackage)!!.directories[0]
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
rootDir.findFileByRelativePath(targetDirPath)!!.toPsiDirectory(project)!!
|
||||
}
|
||||
MoveFilesOrDirectoriesProcessor(
|
||||
@@ -174,8 +174,7 @@ enum class MoveAction : AbstractMultifileRefactoringTest.RefactoringAction {
|
||||
/* moveCallback = */ null,
|
||||
/* prepareSuccessfulCallback = */ null
|
||||
).run()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val targetFile = config.getString("targetFile")
|
||||
|
||||
MoveHandler.doMove(
|
||||
@@ -251,7 +250,7 @@ enum class MoveAction : AbstractMultifileRefactoringTest.RefactoringAction {
|
||||
val project = mainFile.project
|
||||
val sourceDir = rootDir.findFileByRelativePath(config.getString("sourceDir"))!!.toPsiDirectory(project)!!
|
||||
val targetDir = rootDir.findFileByRelativePath(config.getString("targetDir"))!!.toPsiDirectory(project)!!
|
||||
MoveDirectoryWithClassesProcessor(project, arrayOf(sourceDir), targetDir, true, true, true, {}).run()
|
||||
MoveDirectoryWithClassesProcessor(project, arrayOf(sourceDir), targetDir, true, true, true) {}.run()
|
||||
}
|
||||
},
|
||||
|
||||
@@ -263,15 +262,15 @@ enum class MoveAction : AbstractMultifileRefactoringTest.RefactoringAction {
|
||||
val targetClass =
|
||||
if (targetClassName != null) {
|
||||
KotlinFullClassNameIndex.getInstance().get(targetClassName, project, project.projectScope()).first()!!
|
||||
}
|
||||
else null
|
||||
val delegate = MoveDeclarationsDelegate.NestedClass(config.getNullableString("newName"),
|
||||
config.getNullableString("outerInstanceParameter"))
|
||||
} else null
|
||||
val delegate = MoveDeclarationsDelegate.NestedClass(
|
||||
config.getNullableString("newName"),
|
||||
config.getNullableString("outerInstanceParameter")
|
||||
)
|
||||
val moveTarget =
|
||||
if (targetClass != null) {
|
||||
KotlinMoveTargetForExistingElement(targetClass)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val fileName = (delegate.newClassName ?: elementToMove.name!!) + ".kt"
|
||||
val targetPackageFqName = (mainFile as KtFile).packageFqName
|
||||
val targetDir = mainFile.containingDirectory!!
|
||||
|
||||
Reference in New Issue
Block a user