-
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.
Add support for the android studio DB inspector
- Loading branch information
1 parent
79383bc
commit c447413
Showing
5 changed files
with
87 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
...plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/android/SqlDelightRunAnnotator.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,78 @@ | ||
package app.cash.sqldelight.intellij.run.android | ||
|
||
import app.cash.sqldelight.core.lang.util.rawSqlText | ||
import com.alecstrong.sql.psi.core.psi.SqlStmt | ||
import com.alecstrong.sql.psi.core.psi.SqlStmtList | ||
import com.android.tools.idea.sqlite.DatabaseInspectorProjectService | ||
import com.android.tools.idea.sqlite.annotator.RunSqliteStatementGutterIconAction | ||
import com.android.tools.idea.sqlite.ui.DatabaseInspectorViewsFactoryImpl | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.Annotator | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.lang.java.JavaLanguage | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.editor.markup.GutterIconRenderer | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFileFactory | ||
import com.intellij.psi.PsiLanguageInjectionHost | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.intellij.util.ui.EmptyIcon | ||
import icons.StudioIcons | ||
import javax.swing.Icon | ||
|
||
class SqlDelightRunAnnotator : Annotator { | ||
override fun annotate(element: PsiElement, holder: AnnotationHolder) { | ||
if (element.parent !is SqlStmtList || element !is SqlStmt) return | ||
|
||
val text = element.rawSqlText().trim().replace("\\s+".toRegex(), " ") | ||
val s = """ | ||
|package com.example; | ||
| | ||
|import android.database.sqlite.SQLiteDatabase; | ||
| | ||
|class Util { | ||
| void f(SQLiteDatabase db) { | ||
| db.execSQL("$text"); | ||
| } | ||
|} | ||
""".trimMargin() | ||
|
||
val psiFile = PsiFileFactory.getInstance(element.project) | ||
.createFileFromText("Util.java", JavaLanguage.INSTANCE, s) | ||
|
||
val host = PsiTreeUtil.findChildOfType(psiFile, PsiLanguageInjectionHost::class.java) ?: return | ||
|
||
if (!PsiTreeUtil.hasErrorElements(psiFile)) { | ||
holder.newAnnotation(HighlightSeverity.INFORMATION, "") | ||
.gutterIconRenderer(RunSqliteStatementGutterIconRenderer(host)) | ||
.create() | ||
} | ||
} | ||
|
||
/** | ||
* Shows an icon in the gutter when a SQLite statement is recognized. eg. Room @Query annotations. | ||
*/ | ||
private data class RunSqliteStatementGutterIconRenderer(private val element: PsiElement) : GutterIconRenderer() { | ||
private val sqliteExplorerProjectService = DatabaseInspectorProjectService.getInstance(element.project) | ||
|
||
override fun getIcon(): Icon { | ||
return if (sqliteExplorerProjectService.hasOpenDatabase()) { | ||
StudioIcons.DatabaseInspector.NEW_QUERY | ||
} else { | ||
EmptyIcon.ICON_0 | ||
} | ||
} | ||
|
||
override fun getTooltipText(): String { | ||
return "Run Sqlite statement in Database Inspector" | ||
} | ||
|
||
override fun isNavigateAction(): Boolean { | ||
return sqliteExplorerProjectService.hasOpenDatabase() | ||
} | ||
|
||
override fun getClickAction(): AnAction { | ||
return RunSqliteStatementGutterIconAction(element.project, element, DatabaseInspectorViewsFactoryImpl.getInstance()) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sqldelight-idea-plugin/src/main/resources/META-INF/com.squareup.sqldelight-withAndroid.xml
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 @@ | ||
<idea-plugin> | ||
<extensions defaultExtensionNs="com.intellij"> | ||
<annotator language="SqlDelight" | ||
implementationClass="app.cash.sqldelight.intellij.run.android.SqlDelightRunAnnotator"/> | ||
</extensions> | ||
</idea-plugin> |
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