From 0e271b72c785c80d4fb6fd745771f8583fad68fb Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Fri, 25 Dec 2020 13:50:18 +0300 Subject: [PATCH] FIR IDE: Do not try to shorten type without qualifier --- .../shortenRefs/FirShortenRefsTestGenerated.java | 5 +++++ .../api/fir/components/KtFirReferenceShortener.kt | 11 +++++++++-- ...ParameterTypeNestedTypeWithoutPackageNotShorten.kt | 8 ++++++++ ...terTypeNestedTypeWithoutPackageNotShorten.kt.after | 8 ++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt create mode 100644 idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt.after diff --git a/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java index c6e8e608fb5..dfd16ca314a 100644 --- a/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java +++ b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java @@ -144,6 +144,11 @@ public class FirShortenRefsTestGenerated extends AbstractFirShortenRefsTest { runTest("idea/testData/shortenRefsFir/types/ParameterTypeNestedType.kt"); } + @TestMetadata("ParameterTypeNestedTypeWithoutPackageNotShorten.kt") + public void testParameterTypeNestedTypeWithoutPackageNotShorten() throws Exception { + runTest("idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt"); + } + @TestMetadata("ParameterTypeNonImportedClass.kt") public void testParameterTypeNonImportedClass() throws Exception { runTest("idea/testData/shortenRefsFir/types/ParameterTypeNonImportedClass.kt"); diff --git a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/components/KtFirReferenceShortener.kt b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/components/KtFirReferenceShortener.kt index 5a58e7ac80a..c1520243ac8 100644 --- a/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/components/KtFirReferenceShortener.kt +++ b/idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/components/KtFirReferenceShortener.kt @@ -185,6 +185,9 @@ internal class KtFirReferenceShortener( val positionScopes = findScopesAtPosition(wholeTypeElement, namesToImport) ?: return for ((classId, typeElement) in allClassIds.zip(allTypeElements)) { + // if qualifier is null, then this type have no package and thus cannot be shortened + if (typeElement.qualifier == null) return + val firstFoundClass = findFirstClassifierInScopesByName(positionScopes, classId.shortClassName)?.classId if (firstFoundClass == classId) { @@ -234,7 +237,7 @@ internal class KtFirReferenceShortener( val singleAvailableProperty = findSinglePropertyInScopesByName(scopes, propertyId.callableName) if (singleAvailableProperty?.callableId == propertyId) { - callsToShorten.add(qualifiedProperty) + addElementToShorten(qualifiedProperty) } } @@ -251,9 +254,13 @@ internal class KtFirReferenceShortener( val singleAvailableCallable = findSingleFunctionInScopesByName(scopes, callableId.callableName) if (singleAvailableCallable?.callableId == callableId) { - callsToShorten.add(qualifiedCallExpression) + addElementToShorten(qualifiedCallExpression) } } + + private fun addElementToShorten(element: KtDotQualifiedExpression) { + callsToShorten.add(element) + } } } diff --git a/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt b/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt new file mode 100644 index 00000000000..001e3180f67 --- /dev/null +++ b/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt @@ -0,0 +1,8 @@ +// FIR_COMPARISON +package test + +class T { + class TT +} + +fun foo(t: T.TT) {} diff --git a/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt.after b/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt.after new file mode 100644 index 00000000000..55547c490bc --- /dev/null +++ b/idea/testData/shortenRefsFir/types/ParameterTypeNestedTypeWithoutPackageNotShorten.kt.after @@ -0,0 +1,8 @@ +// FIR_COMPARISON +package test + +class T { + class TT +} + +fun foo(t: T.TT) {}