Skip to content

Commit

Permalink
[PSI] KtCommonFile: optimize search for script
Browse files Browse the repository at this point in the history
KtScript can be declared only as a single top-level declaration,
so there is no need to search beyond the first declaration

^KT-73687
  • Loading branch information
dimonchik0036 authored and qodana-bot committed Dec 9, 2024
1 parent 505197c commit 7ab8dad
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/psi/src/org/jetbrains/kotlin/psi/KtCommonFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import org.jetbrains.kotlin.psi.stubs.KotlinFileStub
import org.jetbrains.kotlin.psi.stubs.elements.KtPlaceHolderStubElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ open class KtCommonFile(viewProvider: FileViewProvider, val isCompiled: Boolean)
isScript?.let { if (!it) return null }
greenStub?.let { if (!it.isScript()) return null }

val result = getChildOfType<KtScript>()
val result = findChildBeforeFirstDeclarationInclusiveByType<KtScript>(KtStubElementTypes.SCRIPT)
if (isScript == null) {
isScript = result != null
}
Expand Down Expand Up @@ -151,8 +151,8 @@ open class KtCommonFile(viewProvider: FileViewProvider, val isCompiled: Boolean)
* So this function will iterate as a maximum only through all non-declarations in the beginning plus one declaration.
* This one declaration processing is required to support the optimization for [KtScript] as well as it can be only in the beginning.
*/
private fun <T : KtElementImplStub<out StubElement<*>>> findChildBeforeFirstDeclarationInclusiveByType(
elementType: KtPlaceHolderStubElementType<T>,
private fun <T : KtElementImplStub<out StubElement<T>>> findChildBeforeFirstDeclarationInclusiveByType(
elementType: KtStubElementType<out StubElement<T>, T>,
): T? {
val stub = greenStub
if (stub != null) {
Expand Down

0 comments on commit 7ab8dad

Please sign in to comment.