Skip to content

Commit e067428

Browse files
committed
wip
1 parent cf7c678 commit e067428

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

webui/src/main/kotlin/com/simiacryptus/diff/AddApplyDiffLinks.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ fun SocketManagerBase.addApplyDiffLinks(
3434
(isParenthesisBalanced && !isParenthesisBalancedNew) ||
3535
(isQuoteBalanced && !isQuoteBalancedNew) ||
3636
(isSingleQuoteBalanced && !isSingleQuoteBalancedNew))
37-
PatchResult(newCode, !isError)
37+
PatchResult(newCode, !isError, if(!isError) null else {
38+
val error = StringBuilder()
39+
if (!isCurlyBalanced) error.append("Curly braces are not balanced\n")
40+
if (!isSquareBalanced) error.append("Square braces are not balanced\n")
41+
if (!isParenthesisBalanced) error.append("Parenthesis are not balanced\n")
42+
if (!isQuoteBalanced) error.append("Quotes are not balanced\n")
43+
if (!isSingleQuoteBalanced) error.append("Single quotes are not balanced\n")
44+
error.toString()
45+
})
3846
}
3947

4048
val diffPattern = """(?s)(?<![^\n])```diff\n(.*?)\n```""".toRegex()

webui/src/main/kotlin/com/simiacryptus/diff/AddApplyFileDiffLinks.kt

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,20 @@ private fun SocketManagerBase.renderDiffBlock(
325325
return revertTask.placeholder
326326
}
327327

328-
if (echoDiff.isNotBlank() && newCode.isValid && shouldAutoApply(filepath ?: root.resolve(filename))) {
329-
try {
330-
filepath.toFile().writeText(newCode.newCode, Charsets.UTF_8)
331-
val originalCode = AtomicReference(prevCode)
332-
handle(mapOf(relativize to newCode.newCode))
333-
val revertButton = createRevertButton(filepath, originalCode.get(), handle)
334-
return "```diff\n$diffVal\n```\n" + """<div class="cmd-button">Diff Automatically Applied to ${filepath}</div>""" + revertButton
335-
} catch (e: Throwable) {
336-
log.error("Error auto-applying diff", e)
337-
return "```diff\n$diffVal\n```\n" + """<div class="cmd-button">Error Auto-Applying Diff to ${filepath}: ${e.message}</div>"""
328+
if (echoDiff.isNotBlank()) {
329+
if (newCode.isValid) {
330+
if (shouldAutoApply(filepath ?: root.resolve(filename))) {
331+
try {
332+
filepath.toFile().writeText(newCode.newCode, Charsets.UTF_8)
333+
val originalCode = AtomicReference(prevCode)
334+
handle(mapOf(relativize to newCode.newCode))
335+
val revertButton = createRevertButton(filepath, originalCode.get(), handle)
336+
return "```diff\n$diffVal\n```\n" + """<div class="cmd-button">Diff Automatically Applied to ${filepath}</div>""" + revertButton
337+
} catch (e: Throwable) {
338+
log.error("Error auto-applying diff", e)
339+
return "```diff\n$diffVal\n```\n" + """<div class="cmd-button">Error Auto-Applying Diff to ${filepath}: ${e.message}</div>"""
340+
}
341+
}
338342
}
339343
}
340344

@@ -400,7 +404,6 @@ private fun SocketManagerBase.renderDiffBlock(
400404

401405

402406
if (echoDiff.isNotBlank()) {
403-
404407
// Add "Fix Patch" button if the patch is not valid
405408
if (!newCode.isValid) {
406409
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.
483486
log.error("Error in fix patch", e)
484487
}
485488
}
486-
//apply1 += fixPatchLink
487489
fixTask.complete(fixPatchLink)
488490
}
489491

490492
// Create "Apply Diff (Bottom to Top)" button
491-
val apply2 = hrefLink("(Bottom to Top)", classname = "href-link cmd-button") {
493+
val applyReversed = hrefLink("(Bottom to Top)", classname = "href-link cmd-button") {
492494
try {
493495
originalCode = load(filepath)
494496
val originalLines = originalCode.reverseLines()
@@ -509,14 +511,14 @@ Please provide a fix for the diff above in the form of a diff patch.
509511
try {
510512
filepath.toFile().writeText(originalCode, Charsets.UTF_8)
511513
handle(mapOf(relativize to originalCode))
512-
hrefLink.set("""<div class="cmd-button">Reverted</div>""" + apply1 + apply2)
514+
hrefLink.set("""<div class="cmd-button">Reverted</div>""" + apply1 + applyReversed)
513515
applydiffTask.complete()
514516
} catch (e: Throwable) {
515517
hrefLink.append("""<div class="cmd-button">Error: ${e.message}</div>""")
516518
applydiffTask.error(null, e)
517519
}
518520
}
519-
hrefLink = applydiffTask.complete(apply1 + "\n" + apply2)!!
521+
hrefLink = applydiffTask.complete(apply1 + "\n" + applyReversed)!!
520522
}
521523

522524
val lang = filename.split('.').lastOrNull() ?: ""
@@ -631,7 +633,7 @@ ${prevCode}
631633
val newValue = if (newCode.isValid) {
632634
mainTabs + "\n" + applydiffTask.placeholder
633635
} else {
634-
mainTabs + """<div class="warning">Warning: The patch is not valid. Please fix the patch before applying.</div>""" + applydiffTask.placeholder
636+
mainTabs + """<div class="warning">Warning: The patch is not valid: ${newCode.error ?: "???"}</div>""" + applydiffTask.placeholder
635637
}
636638
return newValue
637639
}
@@ -655,7 +657,15 @@ private val patch = { code: String, diff: String ->
655657
(isParenthesisBalanced && !isParenthesisBalancedNew) ||
656658
(isQuoteBalanced && !isQuoteBalancedNew) ||
657659
(isSingleQuoteBalanced && !isSingleQuoteBalancedNew))
658-
PatchResult(newCode, !isError)
660+
PatchResult(newCode, !isError, if(!isError) null else {
661+
val error = StringBuilder()
662+
if (!isCurlyBalanced) error.append("Curly braces are not balanced\n")
663+
if (!isSquareBalanced) error.append("Square braces are not balanced\n")
664+
if (!isParenthesisBalanced) error.append("Parenthesis are not balanced\n")
665+
if (!isQuoteBalanced) error.append("Quotes are not balanced\n")
666+
if (!isSingleQuoteBalanced) error.append("Single quotes are not balanced\n")
667+
error.toString()
668+
})
659669
}
660670

661671

webui/src/main/kotlin/com/simiacryptus/diff/FileValidationUtils.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,5 @@ class FileValidationUtils {
171171
}
172172
return false
173173
}
174-
175174
}
176-
177175
}

webui/src/main/kotlin/com/simiacryptus/diff/PatchResult.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package com.simiacryptus.diff
33
data class PatchResult(
44
val newCode: String,
55
val isValid: Boolean,
6+
val error: String? = null,
67
)

0 commit comments

Comments
 (0)