From e0674287ad1376dc29951dbd1694afc841be53dd Mon Sep 17 00:00:00 2001 From: Andrew Charneski Date: Sun, 22 Dec 2024 17:26:42 -0500 Subject: [PATCH] wip --- .../simiacryptus/diff/AddApplyDiffLinks.kt | 10 ++++- .../diff/AddApplyFileDiffLinks.kt | 44 ++++++++++++------- .../simiacryptus/diff/FileValidationUtils.kt | 2 - .../com/simiacryptus/diff/PatchResult.kt | 1 + 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/webui/src/main/kotlin/com/simiacryptus/diff/AddApplyDiffLinks.kt b/webui/src/main/kotlin/com/simiacryptus/diff/AddApplyDiffLinks.kt index 3e7f550b..09535c64 100644 --- a/webui/src/main/kotlin/com/simiacryptus/diff/AddApplyDiffLinks.kt +++ b/webui/src/main/kotlin/com/simiacryptus/diff/AddApplyDiffLinks.kt @@ -34,7 +34,15 @@ fun SocketManagerBase.addApplyDiffLinks( (isParenthesisBalanced && !isParenthesisBalancedNew) || (isQuoteBalanced && !isQuoteBalancedNew) || (isSingleQuoteBalanced && !isSingleQuoteBalancedNew)) - PatchResult(newCode, !isError) + PatchResult(newCode, !isError, if(!isError) null else { + val error = StringBuilder() + if (!isCurlyBalanced) error.append("Curly braces are not balanced\n") + if (!isSquareBalanced) error.append("Square braces are not balanced\n") + if (!isParenthesisBalanced) error.append("Parenthesis are not balanced\n") + if (!isQuoteBalanced) error.append("Quotes are not balanced\n") + if (!isSingleQuoteBalanced) error.append("Single quotes are not balanced\n") + error.toString() + }) } val diffPattern = """(?s)(?Diff Automatically Applied to ${filepath}""" + revertButton - } catch (e: Throwable) { - log.error("Error auto-applying diff", e) - return "```diff\n$diffVal\n```\n" + """
Error Auto-Applying Diff to ${filepath}: ${e.message}
""" + if (echoDiff.isNotBlank()) { + if (newCode.isValid) { + if (shouldAutoApply(filepath ?: root.resolve(filename))) { + try { + filepath.toFile().writeText(newCode.newCode, Charsets.UTF_8) + val originalCode = AtomicReference(prevCode) + handle(mapOf(relativize to newCode.newCode)) + val revertButton = createRevertButton(filepath, originalCode.get(), handle) + return "```diff\n$diffVal\n```\n" + """
Diff Automatically Applied to ${filepath}
""" + revertButton + } catch (e: Throwable) { + log.error("Error auto-applying diff", e) + return "```diff\n$diffVal\n```\n" + """
Error Auto-Applying Diff to ${filepath}: ${e.message}
""" + } + } } } @@ -400,7 +404,6 @@ private fun SocketManagerBase.renderDiffBlock( if (echoDiff.isNotBlank()) { - // Add "Fix Patch" button if the patch is not valid if (!newCode.isValid) { val fixPatchLink = hrefLink("Fix Patch", classname = "href-link cmd-button") { @@ -483,12 +486,11 @@ Please provide a fix for the diff above in the form of a diff patch. log.error("Error in fix patch", e) } } - //apply1 += fixPatchLink fixTask.complete(fixPatchLink) } // Create "Apply Diff (Bottom to Top)" button - val apply2 = hrefLink("(Bottom to Top)", classname = "href-link cmd-button") { + val applyReversed = hrefLink("(Bottom to Top)", classname = "href-link cmd-button") { try { originalCode = load(filepath) val originalLines = originalCode.reverseLines() @@ -509,14 +511,14 @@ Please provide a fix for the diff above in the form of a diff patch. try { filepath.toFile().writeText(originalCode, Charsets.UTF_8) handle(mapOf(relativize to originalCode)) - hrefLink.set("""
Reverted
""" + apply1 + apply2) + hrefLink.set("""
Reverted
""" + apply1 + applyReversed) applydiffTask.complete() } catch (e: Throwable) { hrefLink.append("""
Error: ${e.message}
""") applydiffTask.error(null, e) } } - hrefLink = applydiffTask.complete(apply1 + "\n" + apply2)!! + hrefLink = applydiffTask.complete(apply1 + "\n" + applyReversed)!! } val lang = filename.split('.').lastOrNull() ?: "" @@ -631,7 +633,7 @@ ${prevCode} val newValue = if (newCode.isValid) { mainTabs + "\n" + applydiffTask.placeholder } else { - mainTabs + """
Warning: The patch is not valid. Please fix the patch before applying.
""" + applydiffTask.placeholder + mainTabs + """
Warning: The patch is not valid: ${newCode.error ?: "???"}
""" + applydiffTask.placeholder } return newValue } @@ -655,7 +657,15 @@ private val patch = { code: String, diff: String -> (isParenthesisBalanced && !isParenthesisBalancedNew) || (isQuoteBalanced && !isQuoteBalancedNew) || (isSingleQuoteBalanced && !isSingleQuoteBalancedNew)) - PatchResult(newCode, !isError) + PatchResult(newCode, !isError, if(!isError) null else { + val error = StringBuilder() + if (!isCurlyBalanced) error.append("Curly braces are not balanced\n") + if (!isSquareBalanced) error.append("Square braces are not balanced\n") + if (!isParenthesisBalanced) error.append("Parenthesis are not balanced\n") + if (!isQuoteBalanced) error.append("Quotes are not balanced\n") + if (!isSingleQuoteBalanced) error.append("Single quotes are not balanced\n") + error.toString() + }) } diff --git a/webui/src/main/kotlin/com/simiacryptus/diff/FileValidationUtils.kt b/webui/src/main/kotlin/com/simiacryptus/diff/FileValidationUtils.kt index c3ed3897..64b1c94e 100644 --- a/webui/src/main/kotlin/com/simiacryptus/diff/FileValidationUtils.kt +++ b/webui/src/main/kotlin/com/simiacryptus/diff/FileValidationUtils.kt @@ -171,7 +171,5 @@ class FileValidationUtils { } return false } - } - } \ No newline at end of file diff --git a/webui/src/main/kotlin/com/simiacryptus/diff/PatchResult.kt b/webui/src/main/kotlin/com/simiacryptus/diff/PatchResult.kt index ebb1ddb3..0cb4f101 100644 --- a/webui/src/main/kotlin/com/simiacryptus/diff/PatchResult.kt +++ b/webui/src/main/kotlin/com/simiacryptus/diff/PatchResult.kt @@ -3,4 +3,5 @@ package com.simiacryptus.diff data class PatchResult( val newCode: String, val isValid: Boolean, + val error: String? = null, ) \ No newline at end of file