Move: Do not shorten references unaffected by the refactoring
This commit is contained in:
+13
@@ -27,6 +27,8 @@ import com.intellij.psi.SmartPointerManager
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences.Options
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.psi.CopyableUserDataProperty
|
||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||
import java.util.*
|
||||
|
||||
class ShorteningRequest(val pointer: SmartPsiElementPointer<KtElement>, val options: Options)
|
||||
@@ -88,3 +90,14 @@ fun prepareElementsToShorten(project: Project) {
|
||||
project.elementsToShorten = null
|
||||
}
|
||||
}
|
||||
|
||||
var KtElement.isToBeShortened: Boolean? by CopyableUserDataProperty(Key.create("IS_TO_BE_SHORTENED"))
|
||||
|
||||
fun KtElement.addToBeShortenedDescendantsToWaitingSet() {
|
||||
forEachDescendantOfType<KtElement> {
|
||||
if (it.isToBeShortened ?: false) {
|
||||
it.isToBeShortened = null
|
||||
it.addToShorteningWaitSet()
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -24,6 +24,7 @@ import com.intellij.util.IncorrectOperationException
|
||||
import com.intellij.util.SmartList
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.isToBeShortened
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
|
||||
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
|
||||
@@ -131,6 +132,7 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
|
||||
enum class ShorteningMode {
|
||||
NO_SHORTENING,
|
||||
DELAYED_SHORTENING,
|
||||
DELAYED_SHORTENING_VIA_USER_DATA,
|
||||
FORCED_SHORTENING
|
||||
}
|
||||
|
||||
@@ -157,11 +159,10 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
|
||||
val needToShorten =
|
||||
PsiTreeUtil.getParentOfType(expression, KtImportDirective::class.java, KtPackageDirective::class.java) == null
|
||||
if (needToShorten) {
|
||||
if (shorteningMode == ShorteningMode.FORCED_SHORTENING) {
|
||||
ShortenReferences.DEFAULT.process(newQualifiedElement)
|
||||
}
|
||||
else {
|
||||
newQualifiedElement.addToShorteningWaitSet()
|
||||
when (shorteningMode) {
|
||||
ShorteningMode.FORCED_SHORTENING -> ShortenReferences.DEFAULT.process(newQualifiedElement)
|
||||
ShorteningMode.DELAYED_SHORTENING_VIA_USER_DATA -> newQualifiedElement.isToBeShortened = true
|
||||
else -> newQualifiedElement.addToShorteningWaitSet()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.asJava.toLightClass
|
||||
import org.jetbrains.kotlin.asJava.unwrapped
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.isToBeShortened
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.*
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -130,7 +131,7 @@ sealed class MoveDeclarationsDelegate {
|
||||
val type = (containingClassOrObject!!.resolveToDescriptor() as ClassDescriptor).defaultType
|
||||
val parameter = KtPsiFactory(project)
|
||||
.createParameter("private val $outerInstanceParameterName: ${IdeDescriptorRenderers.SOURCE_CODE.renderType(type)}")
|
||||
createPrimaryConstructorParameterListIfAbsent().addParameter(parameter)
|
||||
createPrimaryConstructorParameterListIfAbsent().addParameter(parameter).isToBeShortened = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -42,7 +42,7 @@ import gnu.trove.THashMap
|
||||
import gnu.trove.TObjectHashingStrategy
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
||||
import org.jetbrains.kotlin.asJava.toLightElements
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToBeShortenedDescendantsToWaitingSet
|
||||
import org.jetbrains.kotlin.idea.core.deleteSingle
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.*
|
||||
@@ -221,7 +221,7 @@ class MoveKotlinDeclarationsProcessor(
|
||||
descriptor.delegate.preprocessDeclaration(descriptor, declaration)
|
||||
val newElement = mover(declaration, targetContainer)
|
||||
|
||||
newElement.addToShorteningWaitSet()
|
||||
newElement.addToBeShortenedDescendantsToWaitingSet()
|
||||
|
||||
return newElement
|
||||
}
|
||||
@@ -236,7 +236,7 @@ class MoveKotlinDeclarationsProcessor(
|
||||
descriptor.delegate.preprocessUsages(project, descriptor, usagesToProcessBeforeMove)
|
||||
descriptor.delegate.preprocessUsages(project, descriptor, usagesToProcessAfterMove)
|
||||
|
||||
postProcessMoveUsages(usagesToProcessBeforeMove, shorteningMode = ShorteningMode.NO_SHORTENING)
|
||||
postProcessMoveUsages(usagesToProcessBeforeMove, shorteningMode = ShorteningMode.DELAYED_SHORTENING_VIA_USER_DATA)
|
||||
|
||||
val oldToNewElementsMapping = THashMap<PsiElement, PsiElement>(
|
||||
object: TObjectHashingStrategy<PsiElement> {
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package source
|
||||
|
||||
class Foo {
|
||||
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package target
|
||||
|
||||
fun foo() {
|
||||
val util: util.Util = util.Util()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package util
|
||||
|
||||
class Util
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package source
|
||||
|
||||
class Foo {
|
||||
|
||||
}
|
||||
|
||||
fun <caret>foo() {
|
||||
val util: util.Util = util.Util()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package util
|
||||
|
||||
class Util
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"mainFile": "source/Foo.kt",
|
||||
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
|
||||
"targetPackage": "target"
|
||||
}
|
||||
@@ -552,6 +552,12 @@ public class MoveTestGenerated extends AbstractMoveTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlin/moveTopLevelDeclarations/misc/unaffectedQualifiedReferences/unaffectedQualifiedReferences.test")
|
||||
public void testKotlin_moveTopLevelDeclarations_misc_unaffectedQualifiedReferences_UnaffectedQualifiedReferences() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/unaffectedQualifiedReferences/unaffectedQualifiedReferences.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kotlin/moveTopLevelDeclarations/misc/visibilityConflictInImport/visibilityConflictInImport.test")
|
||||
public void testKotlin_moveTopLevelDeclarations_misc_visibilityConflictInImport_VisibilityConflictInImport() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/visibilityConflictInImport/visibilityConflictInImport.test");
|
||||
|
||||
Reference in New Issue
Block a user