[psi] KtFile: add hasImportAlias with caching
We have hotspots in UAST due to aliases, so we can reduce the time significantly by caching `hasImportAlias` property ^KTIJ-26688
This commit is contained in:
committed by
Space Team
parent
efb96e31fb
commit
3d52ba3bca
@@ -157,6 +157,9 @@ abstract class KtCodeFragment(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override val importLists: List<KtImportList>
|
||||||
|
get() = importsAsImportList().let(::listOfNotNull)
|
||||||
|
|
||||||
override val importDirectives: List<KtImportDirective>
|
override val importDirectives: List<KtImportDirective>
|
||||||
get() = importsAsImportList()?.imports ?: emptyList()
|
get() = importsAsImportList()?.imports ?: emptyList()
|
||||||
|
|
||||||
|
|||||||
@@ -60,8 +60,20 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) :
|
|||||||
val importList: KtImportList?
|
val importList: KtImportList?
|
||||||
get() = importLists.firstOrNull()
|
get() = importLists.firstOrNull()
|
||||||
|
|
||||||
private val importLists: Array<out KtImportList>
|
@Volatile
|
||||||
get() = findChildrenByTypeOrClass(KtStubElementTypes.IMPORT_LIST, KtImportList::class.java)
|
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<KtImportList>
|
||||||
|
get() = findChildrenByTypeOrClass(KtStubElementTypes.IMPORT_LIST, KtImportList::class.java).asList()
|
||||||
|
|
||||||
val fileAnnotationList: KtFileAnnotationList?
|
val fileAnnotationList: KtFileAnnotationList?
|
||||||
get() = findChildByTypeOrClass(KtStubElementTypes.FILE_ANNOTATION_LIST, KtFileAnnotationList::class.java)
|
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? =
|
fun findImportByAlias(name: String): KtImportDirective? {
|
||||||
importDirectives.firstOrNull { name == it.aliasName }
|
if (!hasImportAlias()) return null
|
||||||
|
|
||||||
fun findAliasByFqName(fqName: FqName): KtImportAlias? = importDirectives.firstOrNull {
|
return importDirectives.firstOrNull { name == it.aliasName }
|
||||||
it.alias != null && fqName == it.importedFqName
|
}
|
||||||
}?.alias
|
|
||||||
|
|
||||||
fun getNameForGivenImportAlias(name: Name): Name? =
|
fun findAliasByFqName(fqName: FqName): KtImportAlias? {
|
||||||
importDirectives.find { it.importedName == name }?.importedFqName?.pathSegments()?.last()
|
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
|
@Deprecated("") // getPackageFqName should be used instead
|
||||||
override fun getPackageName(): String {
|
override fun getPackageName(): String {
|
||||||
@@ -217,6 +239,7 @@ open class KtFile(viewProvider: FileViewProvider, val isCompiled: Boolean) :
|
|||||||
isScript = null
|
isScript = null
|
||||||
hasTopLevelCallables = null
|
hasTopLevelCallables = null
|
||||||
pathCached = null
|
pathCached = null
|
||||||
|
hasImportAlias = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isScript(): Boolean = isScript ?: stub?.isScript() ?: isScriptByTree
|
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))
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user