Shorten References: Wrap elements to shorten in smart pointers
#KT-17526 Fixed
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.core
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.SmartPsiElementPointer
|
||||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
import com.intellij.psi.impl.source.PostprocessReformattingAspect
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
@@ -33,6 +34,7 @@ import org.jetbrains.kotlin.idea.util.ShadowedDeclarationsFilter
|
|||||||
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver
|
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||||
@@ -260,7 +262,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
protected val failedToImportDescriptors: Set<DeclarationDescriptor>
|
protected val failedToImportDescriptors: Set<DeclarationDescriptor>
|
||||||
) {
|
) {
|
||||||
protected val resolutionFacade = file.getResolutionFacade()
|
protected val resolutionFacade = file.getResolutionFacade()
|
||||||
private val elementsToShorten = ArrayList<TElement>()
|
private val elementsToShorten = ArrayList<SmartPsiElementPointer<TElement>>()
|
||||||
private val descriptorsToImport = LinkedHashSet<DeclarationDescriptor>()
|
private val descriptorsToImport = LinkedHashSet<DeclarationDescriptor>()
|
||||||
|
|
||||||
abstract val collectElementsVisitor: CollectElementsVisitor<TElement>
|
abstract val collectElementsVisitor: CollectElementsVisitor<TElement>
|
||||||
@@ -277,7 +279,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
val toBeShortened: Boolean
|
val toBeShortened: Boolean
|
||||||
when (result) {
|
when (result) {
|
||||||
is AnalyzeQualifiedElementResult.ShortenNow -> {
|
is AnalyzeQualifiedElementResult.ShortenNow -> {
|
||||||
elementsToShorten.add(element)
|
elementsToShorten.add(element.createSmartPointer())
|
||||||
toBeShortened = true
|
toBeShortened = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +326,8 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
protected abstract fun shortenElement(element: TElement): KtElement
|
protected abstract fun shortenElement(element: TElement): KtElement
|
||||||
|
|
||||||
fun shortenElements(elementSetToUpdate: MutableSet<KtElement>) {
|
fun shortenElements(elementSetToUpdate: MutableSet<KtElement>) {
|
||||||
for (element in elementsToShorten) {
|
for (elementPointer in elementsToShorten) {
|
||||||
|
val element = elementPointer.element ?: continue
|
||||||
if (!element.isValid) continue
|
if (!element.isValid) continue
|
||||||
|
|
||||||
var newElement: KtElement? = null
|
var newElement: KtElement? = null
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// REMOVE_COMPANION_REF
|
||||||
|
package test
|
||||||
|
|
||||||
|
class CompanionExtraPlain { companion object { val cmpVal = 1 } }
|
||||||
|
|
||||||
|
fun wrap(p: Int) = p + 1
|
||||||
|
|
||||||
|
class Dome {
|
||||||
|
fun refer() {
|
||||||
|
<selection>test.wrap(test.CompanionExtraPlain.Companion.cmpVal)</selection>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// REMOVE_COMPANION_REF
|
||||||
|
package test
|
||||||
|
|
||||||
|
class CompanionExtraPlain { companion object { val cmpVal = 1 } }
|
||||||
|
|
||||||
|
fun wrap(p: Int) = p + 1
|
||||||
|
|
||||||
|
class Dome {
|
||||||
|
fun refer() {
|
||||||
|
wrap(CompanionExtraPlain.cmpVal)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -138,6 +138,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("shortCompanionRefInsideShortenedCall.kt")
|
||||||
|
public void testShortCompanionRefInsideShortenedCall() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/shortCompanionRefInsideShortenedCall.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("UnresolvedOverload.kt")
|
@TestMetadata("UnresolvedOverload.kt")
|
||||||
public void testUnresolvedOverload() throws Exception {
|
public void testUnresolvedOverload() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/UnresolvedOverload.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/UnresolvedOverload.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user