Skip to content

Commit d499727

Browse files
Alec Strongrenovate[bot]
authored andcommitted
Find the top migration file faster (#3108)
1 parent bc26eff commit d499727

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/SqlDelightCompiler.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
2424
import app.cash.sqldelight.core.lang.queriesName
2525
import app.cash.sqldelight.core.lang.util.sqFile
2626
import app.cash.sqldelight.dialect.api.SqlDelightDialect
27+
import com.alecstrong.sql.psi.core.psi.InvalidElementDetectedException
2728
import com.alecstrong.sql.psi.core.psi.NamedElement
2829
import com.alecstrong.sql.psi.core.psi.SqlCreateViewStmt
2930
import com.intellij.openapi.module.Module
@@ -176,7 +177,7 @@ object SqlDelightCompiler {
176177
}
177178

178179
private fun List<NamedQuery>.writeQueryInterfaces(file: SqlDelightFile, output: FileAppender) {
179-
return filter { tryWithElement(it.select) { it.needsInterface() } }
180+
return filter { tryWithElement(it.select) { it.needsInterface() } == true }
180181
.forEach { namedQuery ->
181182
val fileSpec = FileSpec.builder(namedQuery.interfaceType.packageName, namedQuery.name)
182183
.apply {
@@ -213,11 +214,14 @@ object SqlDelightCompiler {
213214
internal fun <T> tryWithElement(
214215
element: PsiElement,
215216
block: () -> T
216-
): T {
217+
): T? {
217218
try {
218219
return block()
219220
} catch (e: ProcessCanceledException) {
220221
throw e
222+
} catch (e: InvalidElementDetectedException) {
223+
// It's okay if compilation is cut short, we can just quit out.
224+
return null
221225
} catch (e: Throwable) {
222226
val exception = IllegalStateException("Failed to compile $element :\n${element.text}")
223227
exception.initCause(e)

sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/lang/util/TreeUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.alecstrong.sql.psi.core.psi.SqlTypeName
3939
import com.alecstrong.sql.psi.core.psi.SqlTypes
4040
import com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin
4141
import com.intellij.lang.ASTNode
42+
import com.intellij.openapi.progress.ProcessCanceledException
4243
import com.intellij.psi.PsiElement
4344
import com.intellij.psi.PsiWhiteSpace
4445
import com.intellij.psi.tree.IElementType
@@ -53,7 +54,7 @@ internal inline fun <reified R : PsiElement> PsiElement.parentOfType(): R {
5354
return PsiTreeUtil.getParentOfType(this, R::class.java)!!
5455
}
5556

56-
internal fun PsiElement.type(): IntermediateType = when (this) {
57+
internal fun PsiElement.type(): IntermediateType = if (!isValid) throw ProcessCanceledException() else when (this) {
5758
is ExposableType -> type()
5859
is SqlTypeName -> sqFile().typeResolver.definitionType(this)
5960
is AliasElement -> source().type().copy(name = name)

sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/inspections/SchemaNeedsMigrationInspection.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import app.cash.sqldelight.core.SqlDelightProjectService
55
import app.cash.sqldelight.core.lang.MigrationFile
66
import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
77
import app.cash.sqldelight.core.lang.psi.parameterValue
8-
import app.cash.sqldelight.core.lang.util.findChildrenOfType
98
import app.cash.sqldelight.intellij.refactoring.SqlDelightSignatureBuilder
109
import app.cash.sqldelight.intellij.refactoring.SqlDelightSuggestedRefactoringExecution
1110
import app.cash.sqldelight.intellij.refactoring.SqlDelightSuggestedRefactoringExecution.SuggestedMigrationData
@@ -16,6 +15,7 @@ import com.intellij.codeInspection.LocalQuickFixOnPsiElement
1615
import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR
1716
import com.intellij.codeInspection.ProblemsHolder
1817
import com.intellij.openapi.project.Project
18+
import com.intellij.psi.PsiDirectory
1919
import com.intellij.psi.PsiElement
2020
import com.intellij.psi.PsiElementVisitor
2121
import com.intellij.psi.PsiFile
@@ -34,10 +34,20 @@ internal class SchemaNeedsMigrationInspection : LocalInspectionTool() {
3434
val file = createTable.containingFile as? SqlDelightQueriesFile ?: return
3535
val module = file.module ?: return
3636

37+
fun PsiDirectory.migrationFiles(): Sequence<MigrationFile> {
38+
return children.asSequence().flatMap {
39+
when (it) {
40+
is PsiDirectory -> it.migrationFiles()
41+
is MigrationFile -> sequenceOf(it)
42+
else -> emptySequence()
43+
}
44+
}
45+
}
46+
3747
val dbFile = file.findDbFile() ?: return
3848
val fileIndex = SqlDelightFileIndex.getInstance(module)
39-
val topMigrationFile = fileIndex.sourceFolders(file)
40-
.flatMap { it.findChildrenOfType<MigrationFile>() }
49+
val topMigrationFile = fileIndex.sourceFolders(file).asSequence()
50+
.flatMap { it.migrationFiles() }
4151
.maxByOrNull { it.version }
4252

4353
val tables = (topMigrationFile ?: dbFile).tables(true)

0 commit comments

Comments
 (0)