Preserve list formatting when copy/pasting list items (#19)#24
Merged
Conversation
Line-anchored rich spans (ordered/bullet lists, blockquote, code fence) live in RichSpanManager, not in the AnnotatedString the clipboard carries. A copy/paste round-trip therefore kept the paragraph indent (which is part of the AnnotatedString) but dropped the rich span, so pasted list lines were indented yet no longer rendered as numbered/bulleted items. Remember the rich spans within the copied range in an in-editor buffer and re-apply them on paste of the same text, mirroring the existing undo/redo PreservedRichSpan machinery. The text-match guard keeps stale spans off content pasted from another source. Fixes #19 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Clamp copied rich spans to the copy range so a span partially overlapping the selection no longer yields negative relative offsets and a corrupt span on paste; skip spans that collapse to empty after clamping. Invalidate the in-editor rich-span buffer on any document edit that is not the cut/paste's own edit, so a buffer captured before an intervening edit (or identical text copied from another source after the document changed) cannot apply stale spans. The text-equality match remains a secondary guard. Cut and paste arm a one-edit exemption so they keep the buffer they depend on. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Copy/pasting one or more numbered- (or bulleted-) list items dropped the list formatting: the pasted lines were oddly indented but no longer rendered as numbered/bulleted items.
Root cause
Line-anchored rich spans (
OrderedListSpanStyle,BulletListSpanStyle, blockquote, code fence) are stored inRichSpanManager, separate from theAnnotatedStringthe clipboard carries. The list's paragraph indent is baked into theAnnotatedString(viaapplyLineBlock'swithStyle(paragraphStyle)), so it survived copy→paste — but the rich span that draws the numeral/bullet did not. Result: pasted lines kept the indent yet lost the marker.Fix
Remember the rich spans within the copied range in a small in-editor buffer (
copyRichSpans) and re-apply them on paste (pasteRichSpans), mirroring the existing undo/redoPreservedRichSpanmachinery. A text-match guard ensures the remembered spans are only restored when the pasted text matches what was copied, so content pasted from another source doesn't pick up stale spans.This is a common-code solution; no platform clipboard files were touched. External (cross-app) copy/paste behavior is unchanged.
Tests
Added
RichSpanClipboardTest:Full module suite:
:ComposeTextEditor:desktopTest— 431 tests, 0 failures.Fixes #19