-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the inspectors and enable them to fail silently for expected…
… exception types (#3121) * Optimize the inspectors and enable them to fail silently for expected exception types * Update sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/inspections/InspectionExt.kt Co-authored-by: Jake Wharton <github@jakewharton.com> * Fix up failing tests Co-authored-by: Jake Wharton <github@jakewharton.com>
- Loading branch information
1 parent
7036ffe
commit f9edfe0
Showing
20 changed files
with
168 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ght-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/inspections/InspectionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app.cash.sqldelight.intellij.inspections | ||
|
||
import app.cash.sqldelight.core.SqlDelightFileIndex | ||
import app.cash.sqldelight.core.lang.SqlDelightFile | ||
import com.alecstrong.sql.psi.core.psi.InvalidElementDetectedException | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.openapi.module.Module | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiInvalidElementAccessException | ||
|
||
internal inline fun LocalInspectionTool.ensureReady(file: PsiFile, block: InspectionProperties.() -> PsiElementVisitor): PsiElementVisitor { | ||
return ensureReady(file, { PsiElementVisitor.EMPTY_VISITOR }, block) | ||
} | ||
|
||
internal inline fun LocalInspectionTool.ensureFileReady(file: PsiFile, block: InspectionProperties.() -> Array<ProblemDescriptor>): Array<ProblemDescriptor> { | ||
return ensureReady(file, { emptyArray() }, block) | ||
} | ||
|
||
@Suppress("unused") // Receiver to enforce usage. | ||
internal inline fun PsiElementVisitor.ignoreInvalidElements(block: () -> Unit) { | ||
try { | ||
block() | ||
} catch (_: InvalidElementDetectedException) { | ||
} catch (_: PsiInvalidElementAccessException) { | ||
} | ||
} | ||
|
||
private inline fun <T> ensureReady( | ||
file: PsiFile, | ||
defaultValue: () -> T, | ||
block: InspectionProperties.() -> T | ||
): T { | ||
val sqlDelightFile = file as? SqlDelightFile ?: return defaultValue() | ||
val module = file.module ?: return defaultValue() | ||
val fileIndex = SqlDelightFileIndex.getInstance(module) | ||
if (!fileIndex.isConfigured) return defaultValue() | ||
|
||
try { | ||
return InspectionProperties(module, sqlDelightFile, fileIndex).block() | ||
} catch (_: InvalidElementDetectedException) { | ||
return defaultValue() | ||
} catch (_: PsiInvalidElementAccessException) { | ||
return defaultValue() | ||
} | ||
} | ||
|
||
internal data class InspectionProperties( | ||
val module: Module, | ||
val sqlDelightFile: SqlDelightFile, | ||
val fileIndex: SqlDelightFileIndex, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.