Skip to content

Commit

Permalink
Find the top migration file faster
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova committed Apr 18, 2022
1 parent 849b2ab commit 8fdb275
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
import app.cash.sqldelight.core.lang.queriesName
import app.cash.sqldelight.core.lang.util.sqFile
import app.cash.sqldelight.dialect.api.SqlDelightDialect
import com.alecstrong.sql.psi.core.psi.InvalidElementDetectedException
import com.alecstrong.sql.psi.core.psi.NamedElement
import com.alecstrong.sql.psi.core.psi.SqlCreateViewStmt
import com.intellij.openapi.module.Module
Expand Down Expand Up @@ -175,7 +176,7 @@ object SqlDelightCompiler {
}

private fun List<NamedQuery>.writeQueryInterfaces(file: SqlDelightFile, output: FileAppender) {
return filter { tryWithElement(it.select) { it.needsInterface() } }
return filter { tryWithElement(it.select) { it.needsInterface() } == true }
.forEach { namedQuery ->
val fileSpec = FileSpec.builder(namedQuery.interfaceType.packageName, namedQuery.name)
.apply {
Expand Down Expand Up @@ -212,9 +213,12 @@ object SqlDelightCompiler {
internal fun <T> tryWithElement(
element: PsiElement,
block: () -> T
): T {
): T? {
try {
return block()
} catch (e: InvalidElementDetectedException) {
// It's okay if compilation is cut short, we can just quit out.
return null
} catch (e: Throwable) {
val exception = IllegalStateException("Failed to compile $element :\n${element.text}")
exception.initCause(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.alecstrong.sql.psi.core.psi.SqlTypeName
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin
import com.intellij.lang.ASTNode
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.tree.IElementType
Expand All @@ -52,7 +53,7 @@ internal inline fun <reified R : PsiElement> PsiElement.parentOfType(): R {
return PsiTreeUtil.getParentOfType(this, R::class.java)!!
}

internal fun PsiElement.type(): IntermediateType = when (this) {
internal fun PsiElement.type(): IntermediateType = if (!isValid) throw ProcessCanceledException() else when (this) {
is SqlTypeName -> sqFile().typeResolver.definitionType(this)
is AliasElement -> source().type().copy(name = name)
is ColumnDefMixin -> (columnType as ColumnTypeMixin).type()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app.cash.sqldelight.core.SqlDelightProjectService
import app.cash.sqldelight.core.lang.MigrationFile
import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
import app.cash.sqldelight.core.lang.psi.parameterValue
import app.cash.sqldelight.core.lang.util.findChildrenOfType
import app.cash.sqldelight.intellij.refactoring.SqlDelightSignatureBuilder
import app.cash.sqldelight.intellij.refactoring.SqlDelightSuggestedRefactoringExecution
import app.cash.sqldelight.intellij.refactoring.SqlDelightSuggestedRefactoringExecution.SuggestedMigrationData
Expand All @@ -16,6 +15,7 @@ import com.intellij.codeInspection.LocalQuickFixOnPsiElement
import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
Expand All @@ -34,10 +34,20 @@ internal class SchemaNeedsMigrationInspection : LocalInspectionTool() {
val file = createTable.containingFile as? SqlDelightQueriesFile ?: return
val module = file.module ?: return

fun PsiDirectory.migrationFiles(): Sequence<MigrationFile> {
return children.asSequence().flatMap {
when (it) {
is PsiDirectory -> it.migrationFiles()
is MigrationFile -> sequenceOf(it)
else -> emptySequence()
}
}
}

val dbFile = file.findDbFile() ?: return
val fileIndex = SqlDelightFileIndex.getInstance(module)
val topMigrationFile = fileIndex.sourceFolders(file)
.flatMap { it.findChildrenOfType<MigrationFile>() }
val topMigrationFile = fileIndex.sourceFolders(file).asSequence()
.flatMap { it.migrationFiles() }
.maxByOrNull { it.version }

val tables = (topMigrationFile ?: dbFile).tables(true)
Expand Down

0 comments on commit 8fdb275

Please sign in to comment.