Skip to content

Commit

Permalink
fix: solved a problem with applying patch
Browse files Browse the repository at this point in the history
  • Loading branch information
acke committed Sep 16, 2024
1 parent 097875a commit 16ae0a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/main/kotlin/io/snyk/plugin/ui/jcef/ApplyFixHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ApplyFixHandler(private val project: Project) {
logger.info("[generateApplyFixCommand] calling applyPatchAndSave")

applyFixQuery.addHandler { value ->
val params = value.split(":")
val params = value.split("|@", limit = 2)
val filePath = params[0] // Path to the file that needs to be patched
val patch = params[1] // The patch we received from LS

Expand All @@ -63,6 +63,7 @@ class ApplyFixHandler(private val project: Project) {
""".trimIndent()

jbCefBrowser.cefBrowser.executeJavaScript(script, jbCefBrowser.cefBrowser.url, 0)
JBCefJSQuery.Response("success")
} catch (e: Exception) {
log("Error applying fix: ${e.message}")
}
Expand Down Expand Up @@ -100,22 +101,24 @@ class ApplyFixHandler(private val project: Project) {

val diffPatch = parseDiff(patch)
val patchedContent = applyPatch(fileContent, diffPatch)
var result = false

// Apply the patch inside a WriteCommandAction
result = try {
val result = try {
var appliedPatchSuccessfully = true
WriteCommandAction.runWriteCommandAction(project) {
val document = FileDocumentManager.getInstance().getDocument(virtualFile)
if (document != null) {
if (document != null && fileContent != patchedContent) {
document.setText(patchedContent)

//temporary logs
logger.info("[applyPatchAndSave] Content after applying patch:${document.text}")
appliedPatchSuccessfully = true
} else {
log("[applyPatchAndSave] Failed to find document for saving patched content.")
appliedPatchSuccessfully = false
}
}
true // Success
appliedPatchSuccessfully
} catch (e: Exception) {
log("[applyPatchAndSave] Error applying patch: ${e.message}")
false // Failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ class SuggestionDescriptionPanelFromLS(
const filePath = getFilePathFromFix(currentFix);
const patch = currentFix.unifiedDiffsPerFile[filePath];
window.applyFixQuery(filePath + ':' + patch);
window.applyFixQuery(filePath + '|@' + patch);
// Following VSCode logic, the steps are:
// 1. Read the current file content.
Expand Down

0 comments on commit 16ae0a1

Please sign in to comment.