Skip to content

Commit a341392

Browse files
committed
Move: Search Java usages when top-level declaration moves to new facade
#KT-22747 Fixed
1 parent 91fdc0e commit a341392

File tree

9 files changed

+64
-9
lines changed

9 files changed

+64
-9
lines changed

idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsProcessor.kt

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
1818

19+
import com.intellij.ide.highlighter.JavaFileType
1920
import com.intellij.ide.util.EditorHelper
2021
import com.intellij.openapi.project.Project
2122
import com.intellij.openapi.util.Ref
2223
import com.intellij.openapi.util.text.StringUtil
2324
import com.intellij.psi.PsiElement
2425
import com.intellij.psi.PsiNamedElement
2526
import com.intellij.psi.PsiReference
27+
import com.intellij.psi.search.GlobalSearchScope
28+
import com.intellij.psi.search.SearchScope
2629
import com.intellij.psi.search.searches.ReferencesSearch
2730
import com.intellij.refactoring.BaseRefactoringProcessor
2831
import com.intellij.refactoring.move.MoveCallback
@@ -41,6 +44,7 @@ import com.intellij.util.containers.MultiMap
4144
import gnu.trove.THashMap
4245
import gnu.trove.TObjectHashingStrategy
4346
import org.jetbrains.kotlin.asJava.elements.KtLightDeclaration
47+
import org.jetbrains.kotlin.asJava.findFacadeClass
4448
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
4549
import org.jetbrains.kotlin.asJava.toLightElements
4650
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToBeShortenedDescendantsToWaitingSet
@@ -49,6 +53,7 @@ import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
4953
import org.jetbrains.kotlin.idea.refactoring.move.*
5054
import org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories.MoveKotlinClassHandler
5155
import org.jetbrains.kotlin.idea.search.projectScope
56+
import org.jetbrains.kotlin.idea.search.restrictByFileType
5257
import org.jetbrains.kotlin.idea.util.projectStructure.module
5358
import org.jetbrains.kotlin.lexer.KtTokens
5459
import org.jetbrains.kotlin.psi.*
@@ -153,24 +158,39 @@ class MoveKotlinDeclarationsProcessor(
153158

154159
val newContainerName = descriptor.moveTarget.targetContainerFqName?.asString() ?: ""
155160

156-
fun canSkipUsages(element: PsiElement): Boolean {
157-
val ktDeclaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return false
158-
if (ktDeclaration.hasModifier(KtTokens.PRIVATE_KEYWORD)) return false
159-
val (oldContainer, newContainer) = descriptor.delegate.getContainerChangeInfo(ktDeclaration, descriptor.moveTarget)
160-
val targetModule = descriptor.moveTarget.getTargetModule(project) ?: return false
161-
return oldContainer == newContainer && ktDeclaration.module == targetModule
161+
fun getSearchScope(element: PsiElement): GlobalSearchScope? {
162+
val projectScope = project.projectScope()
163+
val ktDeclaration = element.namedUnwrappedElement as? KtNamedDeclaration ?: return projectScope
164+
if (ktDeclaration.hasModifier(KtTokens.PRIVATE_KEYWORD)) return projectScope
165+
val moveTarget = descriptor.moveTarget
166+
val (oldContainer, newContainer) = descriptor.delegate.getContainerChangeInfo(ktDeclaration, moveTarget)
167+
val targetModule = moveTarget.getTargetModule(project) ?: return projectScope
168+
if (oldContainer != newContainer || ktDeclaration.module != targetModule) return projectScope
169+
// Check if facade class may change
170+
if (newContainer is ContainerInfo.Package) {
171+
val javaScope = projectScope.restrictByFileType(JavaFileType.INSTANCE)
172+
val currentFile = ktDeclaration.containingKtFile
173+
val newFile = when (moveTarget) {
174+
is KotlinMoveTargetForExistingElement -> moveTarget.targetElement as? KtFile ?: return null
175+
is KotlinMoveTargetForDeferredFile -> return javaScope
176+
else -> return null
177+
}
178+
val currentFacade = currentFile.findFacadeClass()
179+
val newFacade = newFile.findFacadeClass()
180+
return if (currentFacade?.qualifiedName != newFacade?.qualifiedName) javaScope else null
181+
}
182+
return null
162183
}
163184

164185
fun collectUsages(kotlinToLightElements: Map<KtNamedDeclaration, List<PsiNamedElement>>, result: MutableCollection<UsageInfo>) {
165186
kotlinToLightElements.values.flatten().flatMapTo(result) { lightElement ->
166-
if (canSkipUsages(lightElement)) return@flatMapTo emptyList()
187+
val searchScope = getSearchScope(lightElement) ?: return@flatMapTo emptyList()
167188

168189
val newFqName = StringUtil.getQualifiedName(newContainerName, lightElement.name)
169190

170191
val foundReferences = HashSet<PsiReference>()
171-
val projectScope = project.projectScope()
172192
val results = ReferencesSearch
173-
.search(lightElement, projectScope)
193+
.search(lightElement, searchScope)
174194
.mapNotNullTo(ArrayList()) { ref ->
175195
if (foundReferences.add(ref) && elementsToMove.none { it.isAncestor(ref.element)}) {
176196
createMoveUsageInfoIfPossible(ref, lightElement, true, false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
BarKt.foo();
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package test
2+
3+
fun foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package test
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
FooKt.foo();
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package test
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package test
2+
3+
fun <caret>foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"mainFile": "source/foo.kt",
3+
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
4+
"targetFile": "source/bar.kt"
5+
}

idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)