16
16
17
17
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations
18
18
19
+ import com.intellij.ide.highlighter.JavaFileType
19
20
import com.intellij.ide.util.EditorHelper
20
21
import com.intellij.openapi.project.Project
21
22
import com.intellij.openapi.util.Ref
22
23
import com.intellij.openapi.util.text.StringUtil
23
24
import com.intellij.psi.PsiElement
24
25
import com.intellij.psi.PsiNamedElement
25
26
import com.intellij.psi.PsiReference
27
+ import com.intellij.psi.search.GlobalSearchScope
28
+ import com.intellij.psi.search.SearchScope
26
29
import com.intellij.psi.search.searches.ReferencesSearch
27
30
import com.intellij.refactoring.BaseRefactoringProcessor
28
31
import com.intellij.refactoring.move.MoveCallback
@@ -41,6 +44,7 @@ import com.intellij.util.containers.MultiMap
41
44
import gnu.trove.THashMap
42
45
import gnu.trove.TObjectHashingStrategy
43
46
import org.jetbrains.kotlin.asJava.elements.KtLightDeclaration
47
+ import org.jetbrains.kotlin.asJava.findFacadeClass
44
48
import org.jetbrains.kotlin.asJava.namedUnwrappedElement
45
49
import org.jetbrains.kotlin.asJava.toLightElements
46
50
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToBeShortenedDescendantsToWaitingSet
@@ -49,6 +53,7 @@ import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
49
53
import org.jetbrains.kotlin.idea.refactoring.move.*
50
54
import org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories.MoveKotlinClassHandler
51
55
import org.jetbrains.kotlin.idea.search.projectScope
56
+ import org.jetbrains.kotlin.idea.search.restrictByFileType
52
57
import org.jetbrains.kotlin.idea.util.projectStructure.module
53
58
import org.jetbrains.kotlin.lexer.KtTokens
54
59
import org.jetbrains.kotlin.psi.*
@@ -153,24 +158,39 @@ class MoveKotlinDeclarationsProcessor(
153
158
154
159
val newContainerName = descriptor.moveTarget.targetContainerFqName?.asString() ? : " "
155
160
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
162
183
}
163
184
164
185
fun collectUsages (kotlinToLightElements : Map <KtNamedDeclaration , List <PsiNamedElement >>, result : MutableCollection <UsageInfo >) {
165
186
kotlinToLightElements.values.flatten().flatMapTo(result) { lightElement ->
166
- if (canSkipUsages( lightElement)) return @flatMapTo emptyList()
187
+ val searchScope = getSearchScope( lightElement) ? : return @flatMapTo emptyList()
167
188
168
189
val newFqName = StringUtil .getQualifiedName(newContainerName, lightElement.name)
169
190
170
191
val foundReferences = HashSet <PsiReference >()
171
- val projectScope = project.projectScope()
172
192
val results = ReferencesSearch
173
- .search(lightElement, projectScope )
193
+ .search(lightElement, searchScope )
174
194
.mapNotNullTo(ArrayList ()) { ref ->
175
195
if (foundReferences.add(ref) && elementsToMove.none { it.isAncestor(ref.element)}) {
176
196
createMoveUsageInfoIfPossible(ref, lightElement, true , false )
0 commit comments