@@ -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
0 commit comments