forked from cashapp/sqldelight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
149 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
...in/src/main/kotlin/com/squareup/sqldelight/intellij/inspections/UnusedImportInspection.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,54 @@ | ||
package com.squareup.sqldelight.intellij.inspections | ||
|
||
import com.intellij.codeInsight.actions.OptimizeImportsProcessor | ||
import com.intellij.codeInspection.InspectionManager | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.LocalQuickFixOnPsiElement | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.squareup.sqldelight.core.lang.psi.ImportStmtMixin | ||
import com.squareup.sqldelight.core.lang.util.findChildrenOfType | ||
import com.squareup.sqldelight.core.psi.SqlDelightJavaTypeName | ||
|
||
class UnusedImportInspection : LocalInspectionTool() { | ||
|
||
override fun runForWholeFile(): Boolean = true | ||
|
||
override fun checkFile( | ||
file: PsiFile, | ||
manager: InspectionManager, | ||
isOnTheFly: Boolean | ||
): Array<ProblemDescriptor> { | ||
val columnTypes = file.findChildrenOfType<SqlDelightJavaTypeName>() | ||
.map { javaTypeName -> javaTypeName.text } | ||
|
||
return file.findChildrenOfType<ImportStmtMixin>() | ||
.filter { importStmtMixin -> importStmtMixin.text.findAnyOf(columnTypes) == null } | ||
.map { importStmtMixin -> | ||
manager.createProblemDescriptor( | ||
importStmtMixin, "Unused import", isOnTheFly, | ||
arrayOf(RemoveUnusedImportQuickFix(file)), ProblemHighlightType.LIKE_UNUSED_SYMBOL | ||
) | ||
} | ||
.toTypedArray() | ||
} | ||
|
||
class RemoveUnusedImportQuickFix(file: PsiFile) : LocalQuickFixOnPsiElement(file) { | ||
|
||
override fun getFamilyName(): String = name | ||
|
||
override fun getText(): String = "Optimize imports" | ||
|
||
override fun invoke( | ||
project: Project, | ||
file: PsiFile, | ||
startElement: PsiElement, | ||
endElement: PsiElement | ||
) { | ||
OptimizeImportsProcessor(project, file).run() | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...plugin/src/main/kotlin/com/squareup/sqldelight/intellij/lang/SqlDelightImportOptimizer.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,44 @@ | ||
package com.squareup.sqldelight.intellij.lang | ||
|
||
import com.intellij.lang.ImportOptimizer | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.psi.PsiFileFactory | ||
import com.squareup.sqldelight.core.lang.MigrationFile | ||
import com.squareup.sqldelight.core.lang.SqlDelightFile | ||
import com.squareup.sqldelight.core.lang.SqlDelightFileType | ||
import com.squareup.sqldelight.core.lang.psi.ImportStmtMixin | ||
import com.squareup.sqldelight.core.lang.util.findChildOfType | ||
import com.squareup.sqldelight.core.lang.util.findChildrenOfType | ||
import com.squareup.sqldelight.core.psi.SqlDelightImportStmtList | ||
import com.squareup.sqldelight.core.psi.SqlDelightJavaTypeName | ||
|
||
class SqlDelightImportOptimizer : ImportOptimizer { | ||
|
||
override fun supports(file: PsiFile): Boolean = file is SqlDelightFile || file is MigrationFile | ||
|
||
override fun processFile(file: PsiFile): Runnable = Runnable { | ||
val manager = PsiDocumentManager.getInstance(file.project) | ||
val document = manager.getDocument(file) ?: return@Runnable | ||
manager.commitDocument(document) | ||
|
||
val columnTypes = file.findChildrenOfType<SqlDelightJavaTypeName>() | ||
.map { javaTypeName -> javaTypeName.text } | ||
|
||
val remainingImports = file.findChildrenOfType<ImportStmtMixin>() | ||
.asSequence() | ||
.map { import -> import.text } | ||
.filter { import -> import.findAnyOf(columnTypes) != null } | ||
.sorted() | ||
.joinToString("\n") | ||
val factory = PsiFileFactory.getInstance(file.project) | ||
val dummyFile = factory.createFileFromText( | ||
"_Dummy_.${SqlDelightFileType.EXTENSION}", | ||
SqlDelightFileType, remainingImports | ||
) | ||
val newImportList = dummyFile.findChildOfType<SqlDelightImportStmtList>() | ||
if (newImportList != null) { | ||
file.findChildOfType<SqlDelightImportStmtList>()?.replace(newImportList) | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
...rc/test/kotlin/com/squareup/sqldelight/intellij/inspections/UnusedImportInspectionTest.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,13 @@ | ||
package com.squareup.sqldelight.intellij.inspections | ||
|
||
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper | ||
import com.squareup.sqldelight.intellij.SqlDelightFixtureTestCase | ||
|
||
class UnusedImportInspectionTest : SqlDelightFixtureTestCase() { | ||
|
||
override val fixtureDirectory: String = "inspections" | ||
|
||
fun testUnusedImportInspection() { | ||
myFixture.testInspection("", LocalInspectionToolWrapper(UnusedImportInspection())) | ||
} | ||
} |
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<problems> | ||
<problem> | ||
<file>CreateTable.sq</file> | ||
<line>1</line> | ||
<description>Unused import</description> | ||
</problem> | ||
<problem> | ||
<file>AlterTable.sqm</file> | ||
<line>1</line> | ||
<description>Unused import</description> | ||
</problem> | ||
</problems> |
3 changes: 3 additions & 0 deletions
3
sqldelight-idea-plugin/testData/inspections/src/AlterTable.sqm
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,3 @@ | ||
import java.time.OffsetDateTime; | ||
|
||
ALTER TABLE hockeyPlayer ADD COLUMN draft_year TEXT; |
6 changes: 6 additions & 0 deletions
6
sqldelight-idea-plugin/testData/inspections/src/CreateTable.sq
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,6 @@ | ||
import java.time.OffsetDateTime; | ||
|
||
CREATE TABLE hockeyPlayer ( | ||
player_number INTEGER NOT NULL, | ||
full_name TEXT NOT NULL | ||
); |