From 5d6d457187a39e764bf8f97f6766453532e0e2ad Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Tue, 1 Dec 2026 11:14:33 +0100 Subject: [PATCH] Splice formatted text and its character styles in one edit (#4277) --- crates/keyframe/src/text_doc.rs | 52 ++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/crates/keyframe/src/text_doc.rs b/crates/keyframe/src/text_doc.rs index e8fcb52be..1d120964c 100644 --- a/crates/keyframe/src/text_doc.rs +++ b/crates/keyframe/src/text_doc.rs @@ -828,18 +828,28 @@ impl TextDoc { /// Replace characters `r` with `s`. The new text takes `style`, or the style of the first /// replaced character, or (inserting) the style of the character before the caret. pub fn replace_range(&mut self, r: Range, s: &str, style: Option<&CharStyle>) { - let n = self.char_len(); - let (a, b) = (r.start.min(n), r.end.min(n).max(r.start.min(n))); + let range = self.clamped_range(r); let st = match style { Some(s) => s.clone(), - None if b > a => self.style_at(a), - None => self.insertion_style(a), + None if !range.is_empty() => self.style_at(range.start), + None => self.insertion_style(range.start), }; + let ins = s.chars().count(); + self.replace_with_runs(range, s, ins, std::iter::once(StyleRun { len: ins, style: st })); + } + + fn clamped_range(&self, r: Range) -> Range { + let n = self.char_len(); + let start = r.start.min(n); + start..r.end.min(n).max(start) + } + + fn replace_with_runs(&mut self, range: Range, s: &str, ins: usize, inserted: impl IntoIterator) { + let Range { start: a, end: b } = range; let mut runs = self.runs(); let i = split_at(&mut runs, a); let j = split_at(&mut runs, b); - let ins = s.chars().count(); - runs.splice(i..j, std::iter::once(StyleRun { len: ins, style: st })); + runs.splice(i..j, inserted); // Map paragraph starts back to the old text; inserted paragraphs inherit the edited // paragraph. Counting breaks in `s` alone loses styles when CR/LF join or split at an // edit boundary (including deleting just one half of a CRLF). @@ -908,14 +918,9 @@ impl TextDoc { /// Replace characters `r` with another document's text and character formatting. pub fn insert_doc(&mut self, r: Range, frag: &TextDoc) { - let a = r.start.min(self.char_len()); - self.replace_range(r, &frag.text, None); - let mut pos = a; - for run in frag.runs() { - let st = run.style.clone(); - self.apply_style(pos..pos + run.len, |s| *s = st.clone()); - pos += run.len; - } + let runs = frag.runs(); + let len = runs.iter().map(|run| run.len).sum(); + self.replace_with_runs(self.clamped_range(r), &frag.text, len, runs); } /// Re-establish the invariants after direct field edits (runs clamped, lists collapsed). @@ -1545,6 +1550,25 @@ mod tests { assert_eq!(e.style_at(2).fill, [1.0; 4]); } + #[test] + fn paste_merges_boundary_styles_and_keeps_paragraphs_and_empty_typing_style() { + let mut dest = TextDoc::plain("A🙂\r\nZ"); + dest.apply_style(0..2, |s| s.size = 20.0); + dest.apply_para(4..5, |p| p.space_after = 5.0); + let mut fragment = TextDoc::plain("ß中"); + fragment.apply_style(0..1, |s| s.size = 20.0); + dest.insert_doc(1..2, &fragment); + assert_eq!(dest.text, "Aß中\r\nZ"); + assert_eq!(dest.runs().iter().map(|r| (r.len, r.style.size)).collect::>(), [(2, 20.0), (4, TextDoc::default().size)]); + assert_eq!(dest.para(1).space_after, 5.0); + + let mut empty = TextDoc::plain(""); + empty.size = 37.0; + dest.insert_doc(0..usize::MAX, &empty); + assert!(dest.text.is_empty()); + assert_eq!(dest.insertion_style(0).size, 37.0); + } + #[test] fn attrs_by_key() { let mut d = TextDoc::plain("abc\ndef");