From eb25fe485b86d3f0663ce82ef35fe13d3bd8c443 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:34:27 +0000 Subject: [PATCH 1/2] Fix indentation when copying/pasting CodeTailor answers The static Parsons path stripped leading whitespace from only the first solution line, and CodeMirror pastes multi-line text verbatim, so pasted Java (e.g. subgoal-label comments) ended up with scrambled indents. - coach.py: keep the first line's indentation in extract_parsons_code - activecode.js: normalize the copied answer (expand tabs, dedent) and, when that answer is pasted, re-indent it to the cursor's indentation (or the mode's indent when pasting at column 0) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014bZMPw1AAXTAdiAkTEKTpE --- bases/rsptx/book_server_api/routers/coach.py | 10 ++- .../runestone/activecode/js/activecode.js | 64 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/bases/rsptx/book_server_api/routers/coach.py b/bases/rsptx/book_server_api/routers/coach.py index 4024a5c51..e2ef4c852 100644 --- a/bases/rsptx/book_server_api/routers/coach.py +++ b/bases/rsptx/book_server_api/routers/coach.py @@ -120,9 +120,13 @@ def extract_parsons_code(html_block): # Remove all HTML tags and extract the code lines text = re.sub(r"<.*?>", "", pre_content, flags=re.DOTALL) - lines = text.strip().splitlines() - if "-----" in lines: - idx = lines.index("-----") + # Don't strip() the whole text: that would remove the first code line's + # indentation but leave the others intact, so the copied solution pastes + # with mismatched indents (most visible in Java). Blank lines are dropped below. + lines = text.splitlines() + stripped = [line.strip() for line in lines] + if "-----" in stripped: + idx = stripped.index("-----") code_lines = lines[idx + 1 :] else: code_lines = lines diff --git a/bases/rsptx/interactives/runestone/activecode/js/activecode.js b/bases/rsptx/interactives/runestone/activecode/js/activecode.js index 63bbf6aa4..26d59888f 100755 --- a/bases/rsptx/interactives/runestone/activecode/js/activecode.js +++ b/bases/rsptx/interactives/runestone/activecode/js/activecode.js @@ -62,6 +62,29 @@ CodeMirror.commands.autocomplete = function (cm) { cm.showHint({ hint: CodeMirror.hint.anyword }); }; +// Expand tabs, drop blank leading/trailing lines and remove the indentation +// shared by every line, so the text starts at column 0 with relative indents intact. +export function normalizeIndentation(code, tabSize = 4) { + if (!code) return code; + let lines = code + .replace(/\r\n?/g, "\n") + .split("\n") + .map((l) => l.replace(/\t/g, " ".repeat(tabSize)).replace(/\s+$/, "")); + while (lines.length && lines[0] === "") lines.shift(); + while (lines.length && lines[lines.length - 1] === "") lines.pop(); + const indents = lines + .filter((l) => l !== "") + .map((l) => l.match(/^ */)[0].length); + const common = indents.length ? Math.min(...indents) : 0; + return lines.map((l) => l.slice(common)).join("\n"); +} + +// Prefix every line of `lines` (already normalized to column 0) with `indent`, +// except the first, which lands after the existing whitespace at the cursor. +export function reindentPastedLines(lines, indent) { + return lines.map((l, i) => (i === 0 || l === "" ? l : indent + l)); +} + const codeIndent = () => Number( window @@ -411,6 +434,42 @@ export class ActiveCode extends RunestoneBase { // capture current this for use in event handler let acElement = this; + // When the student pastes the CodeTailor answer copied with "Copy Answer + // to Clipboard", CodeMirror inserts it verbatim: the first line lands at the + // cursor's column and the rest start at column 0, which scrambles nested + // code (very visible in Java). Shift every line to the cursor's indentation. + editor.on("beforeChange", function (cm, change) { + if (change.origin !== "paste" || !change.update) return; + if (!acElement.copiedAnswer || change.text.length < 2) return; + if ( + normalizeIndentation(change.text.join("\n")) !== + acElement.copiedAnswer + ) { + return; // only adjust our own copied answer + } + const lineText = cm.getLine(change.from.line); + const before = lineText.slice(0, change.from.ch); + if (/\S/.test(before)) return; // pasting mid-line: leave it alone + let indent = before; + if (before === "" && change.from.line > 0) { + // Pasting at column 0: use the mode's indentation for this line, + // e.g. inside main() for Java. + const mode = cm.getMode(); + if (mode.indent) { + const state = cm.getStateAfter(change.from.line - 1, true); + const col = mode.indent(state, "", lineText); + if (typeof col === "number" && col > 0) { + indent = " ".repeat(col); + } + } + } + const lines = acElement.copiedAnswer.split("\n"); + let newLines = reindentPastedLines(lines, indent); + if (indent !== before) + newLines[0] = indent.slice(before.length) + newLines[0]; + change.update(change.from, change.to, newLines); + }); + // document level event handler for tab key to handle context switching // detect if tab key was used to get into the editor document.addEventListener("keydown", function (e) { @@ -1116,7 +1175,10 @@ export class ActiveCode extends RunestoneBase { this.parsonspersonalize === "movable" || this.parsonspersonalize === "partial" ) { - temptextarea.value = this.codeAnswer; + temptextarea.value = normalizeIndentation(this.codeAnswer); + // Remember what was copied so pasting it into the editor can be + // re-indented to the cursor position (see handleAnswerPaste). + this.copiedAnswer = temptextarea.value; } else { temptextarea.value = "No solution available"; } From e71a582201df6400b63b538c8127f3377975bed2 Mon Sep 17 00:00:00 2001 From: Aadarsh Padiyath Date: Wed, 18 Nov 2026 13:17:02 -0500 Subject: [PATCH 2/2] Define acElement for the CodeTailor paste handler main removed the `let acElement = this;` that the paste re-indent handler relied on, so after merging main every paste into the editor threw "acElement is not defined" and inserted nothing. Co-Authored-By: Claude Opus 5.5 --- bases/rsptx/interactives/runestone/activecode/js/activecode.js | 1 + 1 file changed, 1 insertion(+) diff --git a/bases/rsptx/interactives/runestone/activecode/js/activecode.js b/bases/rsptx/interactives/runestone/activecode/js/activecode.js index a2dc20e24..6ff2034cc 100755 --- a/bases/rsptx/interactives/runestone/activecode/js/activecode.js +++ b/bases/rsptx/interactives/runestone/activecode/js/activecode.js @@ -487,6 +487,7 @@ export class ActiveCode extends RunestoneBase { // to Clipboard", CodeMirror inserts it verbatim: the first line lands at the // cursor's column and the rest start at column 0, which scrambles nested // code (very visible in Java). Shift every line to the cursor's indentation. + const acElement = this; editor.on("beforeChange", function (cm, change) { if (change.origin !== "paste" || !change.update) return; if (!acElement.copiedAnswer || change.text.length < 2) return;