Skip to content

Commit

Permalink
Always display latest version in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ng-galien committed Jul 28, 2023
1 parent 21a74ce commit 0cda14b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 49 deletions.
2 changes: 0 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

## Unreleased

### Maintenance

- Project template
- Update dependencies

### Bugfixes

- Fix does not load the latest function [#107](https://github.com/ng-galien/idea-plpgdebugger/issues/107)

## 231.0.1 - 2023-05-09

### Bugfixes
Expand Down
15 changes: 13 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ repositories {

dependencies {

// Postgres
implementation(libs.postgres)
// JDBI
implementation(libs.jdbi3Core)
implementation(libs.jdbi3Kotlin)
implementation(libs.jdbi3Postgres)
// Arrow
testImplementation(libs.arrowCore)
testImplementation(libs.arrowFxCoroutines)

// Kotlin and logging
testImplementation(kotlin("test"))
testImplementation(kotlin("reflect"))
Expand All @@ -38,8 +48,9 @@ dependencies {
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.9.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.1")
// Postgres container and driver
testImplementation("org.testcontainers:testcontainers:1.17.6")
testImplementation("org.testcontainers:junit-jupiter:1.17.6")
testImplementation(libs.testcontainers)
testImplementation(libs.testcontainersJunitJupiter)

testImplementation("org.jdbi:jdbi3-kotlin:3.34.0")
testImplementation("org.jdbi:jdbi3-kotlin-sqlobject:3.34.0")
testImplementation("org.postgresql:postgresql:42.5.1")
Expand Down
21 changes: 21 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,30 @@ gradleIntelliJPlugin = "1.15.0"
qodana = "0.1.13"
kover = "0.7.2"

arrow = "1.2.0"

testcontainers = "1.18.3"

postgres = "42.6.0"

jbdi3 = "3.39.1"

[libraries]

jdbi3Core = { module = "org.jdbi:jdbi3-core", version.ref = "jbdi3" }
jdbi3Kotlin = { module = "org.jdbi:jdbi3-kotlin", version.ref = "jbdi3" }
jdbi3Postgres = { module = "org.jdbi:jdbi3-postgres", version.ref = "jbdi3" }

annotations = { group = "org.jetbrains", name = "annotations", version.ref = "annotations" }

arrowCore = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
arrowFxCoroutines = { module = "io.arrow-kt:arrow-fx-coroutines", version.ref = "arrow" }

postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }

testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "testcontainers" }
testcontainersJunitJupiter = { module = "org.testcontainers:junit-jupiter", version.ref = "testcontainers" }

[plugins]
changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" }
gradleIntelliJPlugin = { id = "org.jetbrains.intellij", version.ref = "gradleIntelliJPlugin" }
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/net/plpgsql/ideadebugger/run/PlProcess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class PlProcess(
executor.setInfo("registerBreakpoint: ${breakpoint.fileUrl} => ${breakpoint.line}")
runReadAction {
val path = breakpoint.fileUrl.removePrefix(PlVirtualFileSystem.PROTOCOL_PREFIX)
val file = PlVirtualFileSystem.getInstance().findFileByPath(path)
val file = PlVirtualFileSystem.Util.getInstance().findFileByPath(path)
if (file != null && readyToAcceptBreakPoint()) {
addBreakpoint(file, breakpoint)
}
Expand All @@ -260,7 +260,7 @@ class PlProcess(
executor.setInfo("unregisterBreakpoint: ${breakpoint.fileUrl} => ${breakpoint.line}")
runReadAction {
val path = breakpoint.fileUrl.removePrefix(PlVirtualFileSystem.PROTOCOL_PREFIX)
val file = PlVirtualFileSystem.getInstance().findFileByPath(path)
val file = PlVirtualFileSystem.Util.getInstance().findFileByPath(path)
if (file != null && readyToAcceptBreakPoint()) {
dropBreakpoint(file, breakpoint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import net.plpgsql.ideadebugger.getPlLanguage
import net.plpgsql.ideadebugger.unquote
import java.nio.charset.Charset

class PlFunctionSource(project: Project, def: PlApiFunctionDef) : LightVirtualFile(
class PlFunctionSource(project: Project, def: PlApiFunctionDef, val md5: String) : LightVirtualFile(
"${def.schema}.${def.name}[${def.oid}]",
getPlLanguage(),
def.source
Expand Down Expand Up @@ -114,7 +114,7 @@ class PlFunctionSource(project: Project, def: PlApiFunctionDef) : LightVirtualFi

override fun getCharset(): Charset = Charsets.UTF_8

override fun getFileSystem(): VirtualFileSystem = PlVirtualFileSystem.getInstance()
override fun getFileSystem(): VirtualFileSystem = PlVirtualFileSystem.Util.getInstance()

override fun getPath(): String = "$oid"

Expand Down
66 changes: 30 additions & 36 deletions src/main/kotlin/net/plpgsql/ideadebugger/vfs/PlSourceManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,54 @@ import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiManager
import net.plpgsql.ideadebugger.command.PlApiFunctionDef
import net.plpgsql.ideadebugger.command.PlApiStackFrame
import net.plpgsql.ideadebugger.command.PlExecutor

class PlSourceManager(private val project: Project, private val executor: PlExecutor) {

private val vfs = PlVirtualFileSystem.getInstance()
private val vfs = PlVirtualFileSystem.Util.getInstance()

fun update(stack: PlApiStackFrame): PlFunctionSource? {

var virtualFile = runReadAction {
vfs.findFileByPath("${stack.oid}")
}

if (virtualFile == null) {
val def = executor.getFunctionDef(stack.oid)
if (def != null) {
virtualFile = vfs.registerNewDefinition(PlFunctionSource(project, def))
grabFromDB(stack.oid)?.let { def ->
grabFromVFS(def.oid)?.let { vfsFile ->
if (!compareMD5(def, vfsFile)) {
updateFileContent(vfsFile, def)
}
}?: run {
vfs.registerNewDefinition(PlFunctionSource(project, def, def.md5))
}.let {
runInEdt {
FileEditorManager.getInstance(project).openFile(it, true, true)
}
}
}
return vfs.findFileByPath("${stack.oid}")
}

if (virtualFile != null) {
runReadAction {
PsiManager.getInstance(project).findFile(virtualFile)
}?.let { psi ->
PsiDocumentManager.getInstance(project).getDocument(psi)?.let { doc ->
if (doc.text != virtualFile.content) {
runInEdt {
runWriteAction {
doc.setText(virtualFile.content)
}
}
private fun updateFileContent(vfsFile: PlFunctionSource, def: PlApiFunctionDef) {
runReadAction {
PsiManager.getInstance(project).findFile(vfsFile)
}?.let { psi ->
PsiDocumentManager.getInstance(project).getDocument(psi)?.let { doc ->
runInEdt {
runWriteAction {
doc.setText(def.source)
}
}
}
}

if (virtualFile != null) {
runInEdt {
FileEditorManager.getInstance(project).openFile(virtualFile, true, true)
}
}

return virtualFile
}

fun getVirtualFile(oid: Long): PlFunctionSource? {
return runReadAction {
vfs.findFileByPath("${oid}")
}
private fun grabFromDB(oid: Long): PlApiFunctionDef? {
return executor.getFunctionDef(oid)
}

fun getSourceFromDatabase(oid: Long): PlFunctionSource? = executor.getFunctionDef(oid)?.let {
vfs.registerNewDefinition(PlFunctionSource(project, it))
private fun grabFromVFS(oid: Long): PlFunctionSource? = runReadAction {
vfs.findFileByPath("$oid")
}


private fun compareMD5(inDB: PlApiFunctionDef, inVFS: PlFunctionSource): Boolean {
return inDB.md5 == inVFS.md5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ class PlVirtualFileSystem : DummyCachingFileSystem<PlFunctionSource>(PROTOCOL) {

override fun doRenameFile(vFile: VirtualFile, newName: String) {}

companion object {
const val PROTOCOL: String = "plpgsql"
const val PROTOCOL_PREFIX: String = "$PROTOCOL://"
object Util {
private val INSTANCE = Objects.requireNonNull(VirtualFileManager.getInstance().getFileSystem(PROTOCOL))
as PlVirtualFileSystem

fun getInstance(): PlVirtualFileSystem = INSTANCE
}

companion object {
const val PROTOCOL: String = "plpgsql"
const val PROTOCOL_PREFIX: String = "$PROTOCOL://"
}

fun registerNewDefinition(file: PlFunctionSource): PlFunctionSource {
runInEdt {
runWriteAction {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<applicationConfigurable parentId="tools"
instance="net.plpgsql.ideadebugger.ui.PlDebuggerSettingsDialog"
id="net.plpgsql.ideadebugger.settings.PlSettingsConfigurable"
displayName="PL/pg Debugger"/>
displayName="PL/pgSQL Debugger"/>
<applicationService serviceImplementation="net.plpgsql.ideadebugger.settings.PlDebuggerSettingsState"/>
<applicationService serviceInterface="net.plpgsql.ideadebugger.service.PlProcessWatcher"
serviceImplementation="net.plpgsql.ideadebugger.service.PlProcessWatcherImpl" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PlFunctionSourceTest(
fun `test function source`() {
val sourceCode = getFunctionSource(this, postgres, file)
val def = PlApiFunctionDef(0, "public", file, sourceCode, sourceCode.md5())
val plSource = PlFunctionSource(project, def)
val plSource = PlFunctionSource(project, def, def.md5)
Assertions.assertEquals(start, plSource.start)
Assertions.assertEquals(range, plSource.codeRange)
}
Expand Down

0 comments on commit 0cda14b

Please sign in to comment.