Skip to content

Commit

Permalink
Move system tables to baseContributorFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd committed Aug 8, 2023
1 parent 16e1cdf commit baaa137
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ private class ApplicationEnvironment {
}
}

data class PredefinedTable(val packageName: String, val simpleFileName: String, val content: String) {
val fileName = "$packageName.$simpleFileName"
}

open class SqlCoreEnvironment(
sourceFolders: List<File>,
dependencies: List<File>,
Expand Down
20 changes: 2 additions & 18 deletions core/src/main/kotlin/com/alecstrong/sql/psi/core/SqlFileBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import com.intellij.extapi.psi.PsiFileBase
import com.intellij.lang.Language
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil
import kotlin.reflect.KClass

abstract class SqlFileBase(
viewProvider: FileViewProvider,
language: Language,
predefinedTables: Collection<PredefinedTable> = emptyList(),
) : PsiFileBase(viewProvider, language) {
abstract val order: Long?

Expand All @@ -39,26 +37,12 @@ abstract class SqlFileBase(
return schema(T::class, sqlStmtElement, includeAll)
}

private val systemTables: List<SchemaContributor> by lazy {
val psiFactory = PsiFileFactory.getInstance(project)
predefinedTables.flatMap { predefinedTable ->
val sqlFile = psiFactory.createFileFromText(predefinedTable.fileName, language, predefinedTable.content) as SqlFileBase
val stmts = sqlFile.sqlStmtList!!
stmts.stmtList.mapNotNull {
it.firstChild as? SchemaContributor
}
}
}

fun <T : SchemaContributor> schema(
type: KClass<T>,
sqlStmtElement: PsiElement? = null,
includeAll: Boolean = true,
): Collection<T> {
val schema = Schema()
for (systemTable in systemTables) {
systemTable.modifySchema(schema)
}
iteratePreviousStatements(type, sqlStmtElement, includeAll) { statement ->
if (sqlStmtElement != null && PsiTreeUtil.isAncestor(sqlStmtElement, statement, false)) {
if (order == null && (statement is TableElement && statement !is SqlCreateTableStmt)) {
Expand Down Expand Up @@ -130,8 +114,8 @@ abstract class SqlFileBase(

val baseContributorFiles = baseContributorFiles()
for ((baseContributorIndex, baseContributor) in baseContributorFiles.withIndex()) {
// Put the last file to index 0, and the previous files to previous index.
val orderedIndex = -baseContributorFiles.lastIndex + baseContributorIndex.toLong()
// Put the last file to index -1, and the previous files to previous index.
val orderedIndex = -1 - baseContributorFiles.lastIndex + baseContributorIndex.toLong()
baseContributor.contributors()?.let { contributors ->
orderedContributors[orderedIndex] = linkedSetOf(elements = contributors.toTypedArray())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ class TablesExposedTest {
|ALTER TABLE test3 RENAME TO test5;
""".trimMargin(),
predefined = listOf(
PredefinedTable(
"predefined",
"1.predefined",
"""
|CREATE TABLE predefined (
| id TEXT NOT NULL
|);
""".trimMargin(),
),
"""
|CREATE TABLE predefined (
| id TEXT NOT NULL
|);
""".trimMargin(),
),
) { (_, file) ->

Expand Down Expand Up @@ -77,15 +73,11 @@ class TablesExposedTest {
|ALTER TABLE test3 RENAME TO test5;
""".trimMargin(),
predefined = listOf(
PredefinedTable(
"predefined",
"1.predefined",
"""
|CREATE TABLE predefined (
| id TEXT NOT NULL
|);
""".trimMargin(),
),
"""
|CREATE TABLE predefined (
| id TEXT NOT NULL
|);
""".trimMargin(),
),
) { (_, file) ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.alecstrong.sql.psi.sample.core
import com.alecstrong.sql.psi.core.SqlFileBase
import com.intellij.psi.FileViewProvider

class SampleFile(viewProvider: FileViewProvider) : SqlFileBase(viewProvider, SampleLanguage, predefinedTables = emptyList()) {
class SampleFile(viewProvider: FileViewProvider) : SqlFileBase(viewProvider, SampleLanguage) {
override val order = name.substringBefore(".${fileType.defaultExtension}").let { name ->
if (name.all { it in '0'..'9' }) {
name.toLong()
Expand Down
2 changes: 2 additions & 0 deletions test-fixtures/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ dependencies {
api project(":core")
api libs.junit
api libs.bundles.intelliJ

testImplementation(libs.junit)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alecstrong.sql.psi.test.fixtures

import com.alecstrong.sql.psi.core.PredefinedTable
import com.alecstrong.sql.psi.core.SqlFileBase
import com.intellij.core.CoreApplicationEnvironment
import java.io.File
Expand All @@ -10,7 +9,7 @@ fun compileFile(
// language=sql
text: String,
customInit: CoreApplicationEnvironment.() -> Unit = { },
predefined: List<PredefinedTable> = emptyList(),
predefined: List<String> = emptyList(),
action: (SqlFileBase) -> Unit,
) {
compileFiles(text, predefined = predefined, customInit = customInit) {
Expand All @@ -21,7 +20,7 @@ fun compileFile(
fun compileFiles(
vararg files: String,
customInit: CoreApplicationEnvironment.() -> Unit = { },
predefined: List<PredefinedTable> = emptyList(),
predefined: List<String> = emptyList(),
action: (List<SqlFileBase>) -> Unit,
) {
val directory = Files.createTempDirectory("sql-psi").toFile()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alecstrong.sql.psi.test.fixtures

import com.alecstrong.sql.psi.core.PredefinedTable
import com.alecstrong.sql.psi.core.SqlFileBase
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
Expand All @@ -13,7 +12,7 @@ import java.util.jar.JarFile
abstract class FixturesTest(
val name: String,
val fixtureRoot: File,
val predefinedTables: List<PredefinedTable> = emptyList(),
val predefinedTables: List<String> = emptyList(),
) {
protected open val replaceRules: Array<Pair<String, String>> = emptyArray()

Expand Down Expand Up @@ -41,13 +40,7 @@ abstract class FixturesTest(
val offsetInLine = element.textOffset - document.getLineStartOffset(lineNum)
errors.add("$name line ${lineNum + 1}:$offsetInLine - $s")
},
predefinedTables = (
newRoot.listFiles { _, name ->
name.endsWith(".predefined")
}?.map {
PredefinedTable("", it.nameWithoutExtension, it.readText())
} ?: emptyList()
) + predefinedTables,
predefinedTables = predefinedTables,
)

val sourceFiles = StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alecstrong.sql.psi.test.fixtures

import com.alecstrong.sql.psi.core.PredefinedTable
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.SqlCoreEnvironment
import com.alecstrong.sql.psi.core.SqlFileBase
Expand All @@ -9,15 +8,17 @@ 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
import java.io.File

object TestHeadlessParser {
fun build(
root: String,
annotator: SqlAnnotationHolder,
predefinedTables: List<PredefinedTable> = emptyList(),
predefinedTables: List<String> = emptyList(),
customInit: CoreApplicationEnvironment.() -> Unit = { },
): SqlCoreEnvironment {
return build(listOf(File(root)), annotator, predefinedTables, customInit)
Expand All @@ -26,7 +27,7 @@ object TestHeadlessParser {
fun build(
sourceFolders: List<File>,
annotator: SqlAnnotationHolder,
predefinedTables: List<PredefinedTable> = emptyList(),
predefinedTables: List<String> = emptyList(),
customInit: CoreApplicationEnvironment.() -> Unit = { },
): SqlCoreEnvironment {
val parserDefinition = TestParserDefinition(predefinedTables)
Expand All @@ -39,6 +40,7 @@ object TestHeadlessParser {
initializeApplication {
registerFileType(TestFileType, TestFileType.defaultExtension)
registerParserDefinition(parserDefinition)

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

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

private class TestFile(viewProvider: FileViewProvider, predefinedTables: List<PredefinedTable>) : SqlFileBase(viewProvider, TestLanguage, predefinedTables) {
private class TestFile(viewProvider: FileViewProvider, private val predefinedTables: List<String>) : 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 @@ -75,4 +77,21 @@ private class TestFile(viewProvider: FileViewProvider, predefinedTables: List<Pr
null
}
}

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")
}
}

This file was deleted.

2 changes: 0 additions & 2 deletions test-fixtures/src/main/resources/fixtures/predefined/Select.s

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alecstrong.sql.psi.test.fixtures

import org.junit.Assert.fail
import org.junit.Test
import java.io.File
import java.nio.file.Files

class PassingPredefinedTablesTest {
@Test
fun mirrorSqlDelight() {
val temp = Files.createTempDirectory("predefinedTest").toFile()
File(temp, "Test.s").writeText(
"""
SELECT * FROM dual;
SELECT name FROM dual;
""".trimIndent(),
)
val env = TestHeadlessParser.build(
sourceFolders = listOf(temp),
annotator = { _, message ->
fail(message)
},
predefinedTables = listOf(
"""
CREATE TABLE dual ( name TEXT );
""".trimIndent(),
),
)
env.close()
}
}

0 comments on commit baaa137

Please sign in to comment.