Skip to content

Commit

Permalink
Lazy create the schema files once
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd committed Oct 18, 2023
1 parent 71eef38 commit 1eaa07a
Showing 1 changed file with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.intellij.core.CoreApplicationEnvironment
import com.intellij.icons.AllIcons
import com.intellij.lang.Language
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.util.Key
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.tree.IFileElementType
Expand All @@ -30,15 +29,21 @@ object TestHeadlessParser {
predefinedTables: List<String> = emptyList(),
customInit: CoreApplicationEnvironment.() -> Unit = { },
): SqlCoreEnvironment {
val parserDefinition = TestParserDefinition(predefinedTables)

val environment = object : SqlCoreEnvironment(
sourceFolders = sourceFolders,
dependencies = emptyList(),
) {
init {
initializeApplication {
registerFileType(TestFileType, TestFileType.defaultExtension)
val parserDefinition = TestParserDefinition(
lazy {
val factory = PsiFileFactory.getInstance(projectEnvironment.project)
predefinedTables.map {
factory.createFileFromText(TestLanguage, it) as SqlFileBase
}
},
)
registerParserDefinition(parserDefinition)

customInit()
Expand All @@ -58,7 +63,7 @@ private object TestFileType : LanguageFileType(TestLanguage) {
override fun getDescription() = "Test SQLite Language File"
}

private class TestParserDefinition(private val predefinedTables: List<String>) : SqlParserDefinition() {
private class TestParserDefinition(private val predefinedTables: Lazy<List<SqlFileBase>>) : SqlParserDefinition() {
override fun createFile(viewProvider: FileViewProvider) = TestFile(viewProvider, predefinedTables)
override fun getFileNodeType() = FILE
override fun getLanguage() = TestLanguage
Expand All @@ -68,7 +73,7 @@ private class TestParserDefinition(private val predefinedTables: List<String>) :
}
}

private class TestFile(viewProvider: FileViewProvider, private val predefinedTables: List<String>) : SqlFileBase(viewProvider, TestLanguage) {
private class TestFile(viewProvider: FileViewProvider, private val predefinedTables: Lazy<List<SqlFileBase>>) : SqlFileBase(viewProvider, TestLanguage) {
override fun getFileType() = TestFileType
override val order = name.substringBefore(".${fileType.defaultExtension}").let { name ->
if (name.all { it in '0'..'9' }) {
Expand All @@ -80,18 +85,6 @@ private class TestFile(viewProvider: FileViewProvider, private val predefinedTab

override fun baseContributorFiles(): List<SqlFileBase> {
val base = super.baseContributorFiles()
if (getUserData(isPredefined) == Unit) {
return base
}
val factory = PsiFileFactory.getInstance(project)
return base + predefinedTables.map {
val file = factory.createFileFromText(TestLanguage, it) as SqlFileBase
file.putUserData(isPredefined, Unit)
file
}
}

companion object {
val isPredefined = Key.create<Unit>("isPredefined")
return base + predefinedTables.value
}
}

0 comments on commit 1eaa07a

Please sign in to comment.