Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the android studio DB inspector #3107

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-js = { id = "org.jetbrains.kotlin.js", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
intellij = { id = "org.jetbrains.intellij", version = "1.5.3" }
intellij = { id = "org.jetbrains.intellij", version = "1.5.2" }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammarKitComposer = { id = "com.alecstrong.grammar.kit.composer", version = "0.1.9" }
publish = { id = "com.vanniktech.maven.publish", version = "0.19.0" }
spotless = { id = "com.diffplug.spotless", version = "6.4.2" } # When updating, check if we can remove base plugin from root build.gradle
Expand Down
1 change: 1 addition & 0 deletions sqldelight-idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ intellij {
"org.jetbrains.kotlin",
"com.intellij.gradle",
"com.intellij.java",
"org.jetbrains.android"
]
if (!project.version.endsWith("SNAPSHOT")) {
patchPluginXml {
Expand Down
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");
AlecKazakova marked this conversation as resolved.
Show resolved Hide resolved
| }
|}
""".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())
}
}
}
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>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<depends>com.intellij.java</depends>
<depends>org.jetbrains.kotlin</depends>
<depends>com.intellij.gradle</depends>
<depends optional="true" config-file="com.squareup.sqldelight-withAndroid.xml">org.jetbrains.android</depends>

<description><![CDATA[
Generates typesafe Kotlin APIs from SQL, and provides language
Expand Down