Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework system tables #548

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,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
@@ -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 @@ -10,14 +9,15 @@ import com.intellij.icons.AllIcons
import com.intellij.lang.Language
import com.intellij.openapi.fileTypes.LanguageFileType
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,19 +26,26 @@ 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)

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 @@ -56,7 +63,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: Lazy<List<SqlFileBase>>) : SqlParserDefinition() {
override fun createFile(viewProvider: FileViewProvider) = TestFile(viewProvider, predefinedTables)
override fun getFileNodeType() = FILE
override fun getLanguage() = TestLanguage
Expand All @@ -66,7 +73,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: 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 @@ -75,4 +82,9 @@ private class TestFile(viewProvider: FileViewProvider, predefinedTables: List<Pr
null
}
}

override fun baseContributorFiles(): List<SqlFileBase> {
val base = super.baseContributorFiles()
return base + predefinedTables.value
}
}

This file was deleted.

2 changes: 0 additions & 2 deletions core/src/testFixtures/resources/fixtures/predefined/Select.s

This file was deleted.

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