diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt index 45f9ab5d41f..24e07a5924e 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt @@ -157,6 +157,9 @@ abstract class KtCodeFragment( return null } + override val importLists: List + get() = importsAsImportList().let(::listOfNotNull) + override val importDirectives: List get() = importsAsImportList()?.imports ?: emptyList() diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt index be1f23ed0fb..b8f08a27d0a 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt @@ -60,8 +60,20 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) : val importList: KtImportList? get() = importLists.firstOrNull() - private val importLists: Array - get() = findChildrenByTypeOrClass(KtStubElementTypes.IMPORT_LIST, KtImportList::class.java) + @Volatile + private var hasImportAlias: Boolean? = null + + fun hasImportAlias(): Boolean { + val hasImportAlias = hasImportAlias + if (hasImportAlias != null) return hasImportAlias + + val newValue = importLists.any(KtImportList::computeHasImportAlias) + this.hasImportAlias = newValue + return newValue + } + + protected open val importLists: List + get() = findChildrenByTypeOrClass(KtStubElementTypes.IMPORT_LIST, KtImportList::class.java).asList() val fileAnnotationList: KtFileAnnotationList? get() = findChildByTypeOrClass(KtStubElementTypes.FILE_ANNOTATION_LIST, KtFileAnnotationList::class.java) @@ -179,15 +191,25 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) : } - fun findImportByAlias(name: String): KtImportDirective? = - importDirectives.firstOrNull { name == it.aliasName } + fun findImportByAlias(name: String): KtImportDirective? { + if (!hasImportAlias()) return null - fun findAliasByFqName(fqName: FqName): KtImportAlias? = importDirectives.firstOrNull { - it.alias != null && fqName == it.importedFqName - }?.alias + return importDirectives.firstOrNull { name == it.aliasName } + } - fun getNameForGivenImportAlias(name: Name): Name? = - importDirectives.find { it.importedName == name }?.importedFqName?.pathSegments()?.last() + fun findAliasByFqName(fqName: FqName): KtImportAlias? { + if (!hasImportAlias()) return null + + return importDirectives.firstOrNull { + it.alias != null && fqName == it.importedFqName + }?.alias + } + + fun getNameForGivenImportAlias(name: Name): Name? { + if (!hasImportAlias()) return null + + return importDirectives.find { it.importedName == name }?.importedFqName?.pathSegments()?.last() + } @Deprecated("") // getPackageFqName should be used instead override fun getPackageName(): String { @@ -217,6 +239,7 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) : isScript = null hasTopLevelCallables = null pathCached = null + hasImportAlias = null } fun isScript(): Boolean = isScript ?: stub?.isScript() ?: isScriptByTree @@ -281,3 +304,16 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) : val FILE_DECLARATION_TYPES = TokenSet.orSet(KtTokenSets.DECLARATION_TYPES, TokenSet.create(KtStubElementTypes.SCRIPT)) } } + +private fun KtImportList.computeHasImportAlias(): Boolean { + var child: PsiElement? = firstChild + while (child != null) { + if (child is KtImportDirective && child.alias != null) { + return true + } + + child = child.nextSibling + } + + return false +}