[kotlin] Use ShortenReferencesFacility

^KTIJ-29582

GitOrigin-RevId: fdd205560310a9babb2e41d10a87b0d2121ef1cd
This commit is contained in:
Andrey Cherkasov
2024-09-09 00:49:22 +04:00
committed by intellij-monorepo-bot
parent 2c05499f05
commit 35dd77a0ee
12 changed files with 25 additions and 52 deletions

View File

@@ -2,18 +2,15 @@
package org.jetbrains.kotlin.idea.k2.codeinsight.fixes
import org.jetbrains.kotlin.analysis.api.KaExperimentalApi
import org.jetbrains.kotlin.analysis.api.KaIdeApi
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KaTypeRendererForSource
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.idea.base.analysis.api.utils.shortenReferencesInRange
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory
import org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixBase
import org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.types.Variance
@@ -28,8 +25,8 @@ internal object ChangeAccessorTypeFixFactory {
}
context(KaSession)
@OptIn(KaExperimentalApi::class, KaIdeApi::class)
private fun getFixes(element: KtElement, expectedType: KaType): List<ChangeAccessorTypeFixBase> {
@OptIn(KaExperimentalApi::class)
private fun getFixes(element: KtElement, expectedType: KaType): List<ChangeAccessorTypeFix> {
val propertyAccessor = element.getParentOfType<KtPropertyAccessor>(
strict = false,
KtProperty::class.java,
@@ -42,11 +39,7 @@ internal object ChangeAccessorTypeFixFactory {
val typeSourceCode = expectedType.render(position = Variance.IN_VARIANCE)
return listOf(
object : ChangeAccessorTypeFixBase(propertyAccessor, typePresentation, typeSourceCode) {
override fun shortenReferences(element: KtTypeReference) {
shortenReferencesInRange(element)
}
}
ChangeAccessorTypeFix(propertyAccessor, typePresentation, typeSourceCode)
)
}
}

View File

@@ -6,13 +6,13 @@ import com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.Presentation
import com.intellij.modcommand.PsiUpdateModCommandAction
import org.jetbrains.kotlin.idea.base.codeInsight.ShortenReferencesFacility
import org.jetbrains.kotlin.idea.base.psi.replaced
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtTypeReference
abstract class ChangeAccessorTypeFixBase(
class ChangeAccessorTypeFix(
element: KtPropertyAccessor,
private val typePresentation: String,
private val typeSourceCode: String,
@@ -41,8 +41,6 @@ abstract class ChangeAccessorTypeFixBase(
val typeReference = if (element.isGetter) element.returnTypeReference else element.parameter!!.typeReference
val insertedTypeRef = typeReference!!.replaced(newTypeReference)
shortenReferences(insertedTypeRef)
ShortenReferencesFacility.getInstance().shorten(insertedTypeRef)
}
abstract fun shortenReferences(element: KtTypeReference)
}

View File

@@ -4,11 +4,9 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isError
@@ -24,14 +22,10 @@ internal object ChangeAccessorTypeFixFactory : KotlinSingleIntentionActionFactor
val typePresentation = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.renderType(type)
val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type)
return object : ChangeAccessorTypeFixBase(
return ChangeAccessorTypeFix(
propertyAccessor,
typePresentation,
typeSourceCode,
) {
override fun shortenReferences(element: KtTypeReference) {
ShortenReferences.DEFAULT.process(element)
}
}.asIntention()
).asIntention()
}
}

View File

@@ -4,5 +4,5 @@ class A {
get(): Int<caret> = {42}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -4,5 +4,5 @@ class A {
get(): (String) -> Int<caret> = {42}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -4,5 +4,5 @@ class A() {
get(): <caret>Any = 1
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -4,5 +4,5 @@ class A() {
get(): Int = 1
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -1,12 +0,0 @@
import java.util.HashSet
import java.util.LinkedHashSet
// "Change getter type to HashSet<Int>" "true"
class A() {
val i: HashSet<Int>
get(): HashSet<Int> = LinkedHashSet<Int>()
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1

View File

@@ -5,5 +5,5 @@ class A() {
get(): <caret>Any = java.util.LinkedHashSet<Int>()
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -7,5 +7,5 @@ class A() {
get(): HashSet<Int> = java.util.LinkedHashSet<Int>()
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -4,5 +4,5 @@ class A() {
set(v: <caret>Any) {}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix

View File

@@ -4,5 +4,5 @@ class A() {
set(v: Int) {}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFixFactory$createAction$1
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ChangeAccessorTypeFixFactory$getFixes$1
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeAccessorTypeFix