From 9a464ae9c40d7287279ae4885a18817245fe141c Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Tue, 9 Feb 2021 16:22:12 +0300 Subject: [PATCH] FIR IDE: Merge all collectors visitors to a single visitor --- .../fir/components/KtFirReferenceShortener.kt | 121 ++++++++---------- 1 file changed, 54 insertions(+), 67 deletions(-) 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 1c198f076ab..d058cedab7a 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 @@ -64,19 +64,14 @@ internal class KtFirReferenceShortener( resolveFileToBodyResolve(file) val firFile = file.getOrBuildFirOfType(firResolveState) - val namesToImport = mutableListOf() - - val typesToShorten = mutableListOf() - val callsToShorten = mutableListOf() - - firFile.acceptChildren(TypesCollectingVisitor(namesToImport, typesToShorten)) - firFile.acceptChildren(CallsCollectingVisitor(namesToImport, callsToShorten)) + val collector = ElementsToShortenCollector() + firFile.acceptChildren(collector) return ShortenCommandImpl( file, - namesToImport.distinct(), - typesToShorten.distinct().map { it.createSmartPointer() }, - callsToShorten.distinct().map { it.createSmartPointer() } + collector.namesToImport.distinct(), + collector.typesToShorten.distinct().map { it.createSmartPointer() }, + collector.qualifiersToShorten.distinct().map { it.createSmartPointer() } ) } @@ -164,10 +159,11 @@ internal class KtFirReferenceShortener( } } - private inner class TypesCollectingVisitor( - private val namesToImport: MutableList, - private val typesToShorten: MutableList, - ) : FirVisitorVoid() { + private inner class ElementsToShortenCollector : FirVisitorVoid() { + val namesToImport: MutableList = mutableListOf() + val typesToShorten: MutableList = mutableListOf() + val qualifiersToShorten: MutableList = mutableListOf() + override fun visitElement(element: FirElement) { element.acceptChildren(this) } @@ -240,14 +236,47 @@ internal class KtFirReferenceShortener( namesToImport.add(classFqName) typesToShorten.add(mostTopLevelTypeElement) } - } - private inner class CallsCollectingVisitor( - private val namesToImport: MutableList, - private val callsToShorten: MutableList - ) : FirVisitorVoid() { - override fun visitElement(element: FirElement) { - element.acceptChildren(this) + override fun visitResolvedQualifier(resolvedQualifier: FirResolvedQualifier) { + super.visitResolvedQualifier(resolvedQualifier) + + val wholeClassQualifier = resolvedQualifier.classId ?: return + val wholeQualifierElement = when (val qualifierPsi = resolvedQualifier.psi) { + is KtDotQualifiedExpression -> qualifierPsi + is KtNameReferenceExpression -> qualifierPsi.getDotQualifiedExpressionForSelector() ?: return + else -> return + } + + collectQualifierIfNeedsToBeShortened(wholeClassQualifier, wholeQualifierElement) + } + + private fun collectQualifierIfNeedsToBeShortened(wholeClassQualifier: ClassId, wholeQualifierElement: KtDotQualifiedExpression) { + val positionScopes = findScopesAtPosition(wholeQualifierElement, namesToImport) ?: return + + val allClassIds = wholeClassQualifier.outerClassesWithSelf + val allQualifiers = wholeQualifierElement.qualifiersWithSelf + + for ((classId, qualifier) in allClassIds.zip(allQualifiers)) { + val firstFoundClass = findFirstClassifierInScopesByName(positionScopes, classId.shortClassName)?.classId + + if (firstFoundClass == classId) { + addElementToShorten(qualifier) + return + } + } + + val (mostTopLevelClassId, mostTopLevelQualifier) = allClassIds.zip(allQualifiers).last() + val availableClassifier = findFirstClassifierInScopesByName(positionScopes, mostTopLevelClassId.shortClassName) + + check(availableClassifier?.classId != mostTopLevelClassId) { + "If this condition were true, we would have exited from the loop on the last iteration. ClassId = $mostTopLevelClassId" + } + + if (availableClassifier == null || availableClassifier.isFromStarOrPackageImport) { + addElementToImportAndShorten(mostTopLevelClassId.asSingleFqName(), mostTopLevelQualifier) + } else { + addFakePackagePrefixToShortenIfPresent(mostTopLevelQualifier) + } } override fun visitResolvedNamedReference(resolvedNamedReference: FirResolvedNamedReference) { @@ -337,48 +366,6 @@ internal class KtFirReferenceShortener( ?: error("Expected all candidates to have same callableId, but got: ${distinctCandidates.map { it.callableId }}") } - override fun visitResolvedQualifier(resolvedQualifier: FirResolvedQualifier) { - super.visitResolvedQualifier(resolvedQualifier) - - val wholeClassQualifier = resolvedQualifier.classId ?: return - val wholeQualifierElement = when (val qualifierPsi = resolvedQualifier.psi) { - is KtDotQualifiedExpression -> qualifierPsi - is KtNameReferenceExpression -> qualifierPsi.getDotQualifiedExpressionForSelector() ?: return - else -> return - } - - collectQualifierIfNeedsToBeShortened(wholeClassQualifier, wholeQualifierElement) - } - - private fun collectQualifierIfNeedsToBeShortened(wholeClassQualifier: ClassId, wholeQualifierElement: KtDotQualifiedExpression) { - val positionScopes = findScopesAtPosition(wholeQualifierElement, namesToImport) ?: return - - val allClassIds = wholeClassQualifier.outerClassesWithSelf - val allQualifiers = wholeQualifierElement.qualifiersWithSelf - - for ((classId, qualifier) in allClassIds.zip(allQualifiers)) { - val firstFoundClass = findFirstClassifierInScopesByName(positionScopes, classId.shortClassName)?.classId - - if (firstFoundClass == classId) { - addElementToShorten(qualifier) - return - } - } - - val (mostTopLevelClassId, mostTopLevelQualifier) = allClassIds.zip(allQualifiers).last() - val availableClassifier = findFirstClassifierInScopesByName(positionScopes, mostTopLevelClassId.shortClassName) - - check(availableClassifier?.classId != mostTopLevelClassId) { - "If this condition were true, we would have exited from the loop on the last iteration. ClassId = $mostTopLevelClassId" - } - - if (availableClassifier == null || availableClassifier.isFromStarOrPackageImport) { - addElementToImportAndShorten(mostTopLevelClassId.asSingleFqName(), mostTopLevelQualifier) - } else { - addFakePackagePrefixToShortenIfPresent(mostTopLevelQualifier) - } - } - private fun addFakePackagePrefixToShortenIfPresent(wholeQualifiedExpression: KtDotQualifiedExpression) { val deepestQualifier = wholeQualifiedExpression.qualifiersWithSelf.last() if (deepestQualifier.hasFakeRootPrefix()) { @@ -387,12 +374,12 @@ internal class KtFirReferenceShortener( } private fun addElementToShorten(element: KtDotQualifiedExpression) { - callsToShorten.add(element) + qualifiersToShorten.add(element) } private fun addElementToImportAndShorten(nameToImport: FqName, element: KtDotQualifiedExpression) { namesToImport.add(nameToImport) - callsToShorten.add(element) + qualifiersToShorten.add(element) } } @@ -410,7 +397,7 @@ private class ShortenCommandImpl( val targetFile: KtFile, val importsToAdd: List, val typesToShorten: List>, - val callsToShorten: List>, + val qualifiersToShorten: List>, ) : ShortenCommand { override fun invokeShortening() { @@ -425,7 +412,7 @@ private class ShortenCommandImpl( type.deleteQualifier() } - for (callPointer in callsToShorten) { + for (callPointer in qualifiersToShorten) { val call = callPointer.element ?: continue call.deleteQualifier() }