KTIJ-28080 [AA] Move selection checking to ElementsToShortenCollector.findClassifierQualifierToShorten

This way, even if the whole qualifier is not selected, but some of its
parts are, the reference shortener will correctly find and shorten
those parts

^KTIJ-28080 Fixed
This commit is contained in:
Roman Golyshev
2024-01-05 15:13:50 +01:00
committed by teamcity
parent 3b448b284c
commit 93f1555322
13 changed files with 201 additions and 5 deletions
@@ -524,8 +524,6 @@ private class ElementsToShortenCollector(
val typeElement = resolvedTypeRef.correspondingTypePsi ?: return
if (typeElement.qualifier == null) return
if (!typeElement.inSelection) return
val classifierId = resolvedTypeRef.type.lowerBoundIfFlexible().candidateClassId ?: return
findTypeQualifierToShorten(classifierId, typeElement)?.let(::addElementToShorten)
@@ -597,8 +595,6 @@ private class ElementsToShortenCollector(
else -> return
}
if (!wholeQualifierElement.inSelection) return
findTypeQualifierToShorten(wholeClassQualifier, wholeQualifierElement)?.let(::addElementToShorten)
}
@@ -829,6 +825,10 @@ private class ElementsToShortenCollector(
/**
* Finds the longest qualifier in [wholeQualifierElement] which can be safely shortened in the [positionScopes].
* [wholeQualifierClassId] is supposed to reflect the class which is referenced by the [wholeQualifierElement].
*
* N.B. Even if the [wholeQualifierElement] is not strictly in the [selection],
* some outer part of it might be, and we want to shorten that.
* So we have to check all the outer qualifiers.
*/
private fun findClassifierQualifierToShorten(
positionScopes: List<FirScope>,
@@ -839,6 +839,8 @@ private class ElementsToShortenCollector(
val allQualifiedElements = wholeQualifierElement.qualifiedElementsWithSelf
for ((classId, element) in allClassIds.zip(allQualifiedElements)) {
if (!element.inSelection) continue
val classSymbol = shorteningContext.toClassSymbol(classId) ?: return null
val option = classShortenStrategy(classSymbol)
if (option == ShortenStrategy.DO_NOT_SHORTEN) continue
@@ -885,7 +887,11 @@ private class ElementsToShortenCollector(
}
}
}
return findFakePackageToShorten(allQualifiedElements.last())
val lastQualifier = allQualifiedElements.last()
if (!lastQualifier.inSelection) return null
return findFakePackageToShorten(lastQualifier)
}
private fun createElementToShorten(
@@ -1361,6 +1367,15 @@ private class ElementsToShortenCollector(
}
}
private val KtElement.inSelection: Boolean
get() = when (this) {
is KtUserType -> inSelection
is KtDotQualifiedExpression -> inSelection
is KtThisExpression -> inSelection
else -> error("Unexpected ${this::class}")
}
/**
* Checks whether type reference of [this] type is considered to be in the [selection] text range.
*
@@ -418,6 +418,18 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType2.kt");
}
@Test
@TestMetadata("partiallySelectedType3.kt")
public void testPartiallySelectedType3() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType3.kt");
}
@Test
@TestMetadata("partiallySelectedType4.kt")
public void testPartiallySelectedType4() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType4.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier1.kt")
public void testPartiallySelectedTypeQualifier1() throws Exception {
@@ -430,6 +442,18 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier2.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier3.kt")
public void testPartiallySelectedTypeQualifier3() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier3.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier4.kt")
public void testPartiallySelectedTypeQualifier4() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier4.kt");
}
@Test
@TestMetadata("qualifierOfUnresolvedReference.kt")
public void testQualifierOfUnresolvedReference() throws Exception {
@@ -628,6 +652,12 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/variable_invokeOperator.kt");
}
@Test
@TestMetadata("_rootIdePackage_IsNotRemovedIfNotSelected.kt")
public void test_rootIdePackage_IsNotRemovedIfNotSelected() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/_rootIdePackage_IsNotRemovedIfNotSelected.kt");
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/shortenRange/nestedClasses")
@TestDataPath("$PROJECT_ROOT")
@@ -418,6 +418,18 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType2.kt");
}
@Test
@TestMetadata("partiallySelectedType3.kt")
public void testPartiallySelectedType3() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType3.kt");
}
@Test
@TestMetadata("partiallySelectedType4.kt")
public void testPartiallySelectedType4() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedType4.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier1.kt")
public void testPartiallySelectedTypeQualifier1() throws Exception {
@@ -430,6 +442,18 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier2.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier3.kt")
public void testPartiallySelectedTypeQualifier3() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier3.kt");
}
@Test
@TestMetadata("partiallySelectedTypeQualifier4.kt")
public void testPartiallySelectedTypeQualifier4() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/partiallySelectedTypeQualifier4.kt");
}
@Test
@TestMetadata("qualifierOfUnresolvedReference.kt")
public void testQualifierOfUnresolvedReference() throws Exception {
@@ -628,6 +652,12 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/variable_invokeOperator.kt");
}
@Test
@TestMetadata("_rootIdePackage_IsNotRemovedIfNotSelected.kt")
public void test_rootIdePackage_IsNotRemovedIfNotSelected() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/shortenRange/_rootIdePackage_IsNotRemovedIfNotSelected.kt");
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/referenceShortener/shortenRange/nestedClasses")
@TestDataPath("$PROJECT_ROOT")
@@ -0,0 +1,17 @@
// FILE: main.kt
package test
fun usage(
foo: _root_ide_package_.dependency.Foo
) {
<expr>"random expr"</expr>
_root_ide_package_.dependency.Foo.bar()
}
// FILE: dependency.kt
package dependency
object Foo {
fun bar() {}
}
@@ -0,0 +1,6 @@
Before shortening: "random expr"
with default settings:
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,11 @@
// FILE: main.kt
package test
fun usage(foo: <expr>dependency.Foo</expr>.Bar) {}
// FILE: dependency.kt
package dependency
class Foo {
class Bar
}
@@ -0,0 +1,9 @@
Before shortening: dependency.Foo
with default settings:
[type] dependency.Foo
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[type] dependency.Foo
with SHORTEN_AND_STAR_IMPORT:
[type] dependency.Foo
@@ -0,0 +1,15 @@
// FILE: main.kt
package test
import dependency.Foo
fun usage(foo: <expr>dependency.Foo.Bar</expr>.Baz) {}
// FILE: dependency.kt
package dependency
class Foo {
class Bar {
class Baz
}
}
@@ -0,0 +1,10 @@
Before shortening: dependency.Foo.Bar
with default settings:
[type] dependency.Foo
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[type] dependency.Foo
with SHORTEN_AND_IMPORT:
[type] dependency.Foo.Bar
with SHORTEN_AND_STAR_IMPORT:
[type] dependency.Foo.Bar
@@ -0,0 +1,15 @@
// FILE: main.kt
package test
fun usage() {
<expr>dependency.Foo</expr>.Bar.baz()
}
// FILE: dependency.kt
package dependency
object Foo {
object Bar {
fun baz() {}
}
}
@@ -0,0 +1,9 @@
Before shortening: dependency.Foo
with default settings:
[qualifier] dependency.Foo
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[qualifier] dependency.Foo
with SHORTEN_AND_STAR_IMPORT:
[qualifier] dependency.Foo
@@ -0,0 +1,19 @@
// FILE: main.kt
package test
import dependency.Foo
fun usage() {
<expr>dependency.Foo.Bar</expr>.Baz.qux()
}
// FILE: dependency.kt
package dependency
object Foo {
class Bar {
object Baz {
fun qux() {}
}
}
}
@@ -0,0 +1,10 @@
Before shortening: dependency.Foo.Bar
with default settings:
[qualifier] dependency.Foo
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] dependency.Foo
with SHORTEN_AND_IMPORT:
[qualifier] dependency.Foo.Bar
with SHORTEN_AND_STAR_IMPORT:
[qualifier] dependency.Foo.Bar