Skip to content

Fix indentation when copying and pasting CodeTailor answers - #1690

Merged
bnmnetp merged 3 commits into
RunestoneInteractive:mainfrom
aspadiyath:claude/festive-archimedes-e8ednm
Nov 19, 2026
Merged

bnmnetp merged 3 commits into
RunestoneInteractive:mainfrom
aspadiyath:claude/festive-archimedes-e8ednm

Conversation

@aspadiyath

Copy link
Copy Markdown
Contributor

Problem

The CodeTailor "Copy Answer to Clipboard" button produced code with scrambled indentation once pasted into the editor. It was most visible in Java, e.g. subgoal-label comments. There were two causes:

  1. Server: extract_parsons_code called strip() on the whole solution text. That removed the first line's indentation but left the other lines alone, so every copied answer was misaligned before it reached the browser.
  2. Browser: CodeMirror pastes multi-line text verbatim. The first line lands at the cursor's column and the rest start at column 0, so nested code ends up at the wrong depth.

Changes

  • coach.py: extract_parsons_code no longer strips the whole text. It finds the ----- separator by comparing stripped lines, so each line keeps its original indentation. Combined with main's tag-stripping and < escaping.
  • activecode.js:
    • normalizeIndentation() cleans up the copied answer: tabs become 4 spaces, blank leading and trailing lines are dropped, and the shared indentation is removed. The cleaned text is saved as copiedAnswer.
    • A beforeChange handler re-indents a paste only when it matches copiedAnswer:
      • after leading whitespace, it uses that whitespace;
      • at column 0 on a blank line, it uses the mode's indentation for that line (e.g. inside main() in Java);
      • mid-line, or when the pasted text is anything else, it leaves the paste alone.

Testing

  • test_extract_parsons_code.py passes, along with all activecode vitest suites.
  • Manually tested on csawesome2 u2b-toggle6 (subgoal-labels Get Help), both in Chrome and with a Playwright script: Get Help → solve → Copy → Close Help → paste on the blank line inside main. The labels land at main's indentation with their nesting kept.

Notes

  • Only the most recently copied answer is re-indented, and only in that question's editor. Other pastes are untouched.
  • None of the existing tests exercise the paste path, so the vitest suites didn't catch a missing variable that broke pasting after the merge with main. It's fixed in this PR.

🤖 Generated with Claude Code

claude and others added 3 commits November 17, 2026 21:34
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014bZMPw1AAXTAdiAkTEKTpE
…edes-e8ednm

# Conflicts:
#	bases/rsptx/book_server_api/routers/coach.py
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 <noreply@anthropic.com>
@aspadiyath
aspadiyath requested a review from bnmnetp as a code owner November 18, 2026 18:45
@bnmnetp
bnmnetp merged commit 791fd03 into RunestoneInteractive:main Nov 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants