Diff a proposed script against the open one and take it a hunk at a time

This commit is contained in:
Gmer4Lfe
2026-08-10 22:41:07 -04:00
parent 66f427dd2d
commit aa3d92360e
2 changed files with 252 additions and 0 deletions
+33
View File
@@ -2445,6 +2445,9 @@ if (document.getElementById('vv-sched-ai-chat')) {
// Per-block Insert buttons render only where a page can receive them.
onInsertCode: vvAiInsertCode,
onOpenSource: vvAiOpenSource,
// Diff and per-hunk apply against whatever the editor holds at the moment of the click.
getCompareText: vvAiCompareText,
onReplaceCode: vvAiReplaceCode,
});
}
@@ -2530,6 +2533,36 @@ function vvAiEditorOpen() {
return !!ed && ed.style.display !== 'none';
}
// What a proposed block is diffed against: the editor exactly as it stands right now, not as it
// was when the answer arrived. Read fresh on every draw, so applying one hunk and redrawing shows
// the remaining differences rather than a stale picture.
function vvAiCompareText() {
const ta = document.getElementById('vv-editor-body');
return (ta && vvAiEditorOpen()) ? ta.value : '';
}
// Takes the whole rewritten file back from a hunk apply. Whole-file rather than a splice because
// the component computed the result against this exact text — handing back a range would make
// both sides responsible for the arithmetic, and only one of them can be right.
function vvAiReplaceCode(text) {
const ta = document.getElementById('vv-editor-body');
if (!ta || !vvAiEditorOpen()) {
if (vvSchedChat) vvSchedChat.note('The editor is not open — nothing was applied.');
return;
}
// Bracketed immediately, so one Ctrl+Z steps back over the whole hunk rather than unpicking it
// a keystroke at a time. Cursor position is kept where it was where that still makes sense.
vvUndoCapture(true);
const caret = Math.min(ta.selectionStart, text.length);
ta.value = text;
ta.selectionStart = ta.selectionEnd = caret;
vvUndoCapture(true);
vvSyncHlOverlay();
vvEditorCursorMoved();
}
// Called by the chat component's per-block Insert button, with that block's exact text. This
// replaced a whole-answer offer: the offer had to guess which block was meant when an answer
// carried several, and it asked after every code answer whether or not anything was wanted.