From b630dfad5518d0be15fd1b2f60822696802dc9cf Mon Sep 17 00:00:00 2001 From: Qslia <49663251+qslia@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:41:09 +0800 Subject: [PATCH] fix(notebook): keep source lines whole when typing in a cell (#7502) Rewriting a cell's source kept every old source element that still prefixed or suffixed the new text, including partial lines, so code typed character by character was saved one character per element and showed one character per line in VS Code and Cursor. Only whole lines (an element ending in a newline, starting at a line start) are reused now; the line being edited is written as one element, the notebook's own JSON layout and string-valued sources stay as they were, and a source already split into characters is joined into lines where it is edited. Co-authored-by: qslia <49663251+qslia@users.noreply.github.com> --- crates/ide-notebook/src/document.rs | 56 ++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/crates/ide-notebook/src/document.rs b/crates/ide-notebook/src/document.rs index c845a52825..c87bcdac29 100644 --- a/crates/ide-notebook/src/document.rs +++ b/crates/ide-notebook/src/document.rs @@ -905,7 +905,9 @@ fn rewritten_source(old: Option<&Node>, text: String) -> Node { let mut start = 0; while first < parts.len() { let part = parts[first].string().expect("validated source text"); - if !text[start..].starts_with(part) { + // Only whole lines are kept: a part without its newline (typing at the end of a line, or a + // source saved one character per element) is rewritten with the rest of its line. + if !part.ends_with('\n') || !text[start..].starts_with(part) { break; } start += part.len(); @@ -918,6 +920,11 @@ fn rewritten_source(old: Option<&Node>, text: String) -> Node { if end - start < part.len() || !text[start..end].ends_with(part) { break; } + // ...and only from the start of a line. + let at = end - part.len(); + if at > 0 && !text[..at].ends_with('\n') { + break; + } end -= part.len(); last -= 1; } @@ -993,3 +1000,50 @@ fn validate_output_node(node: &Node) -> IdeResult<()> { } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn typing_characters_preserves_source_lines() { + let mut source = Node::new(Value::Array(Vec::new())); + + for text in ["a", "ab", "abc"] { + source = rewritten_source(Some(&source), text.to_string()); + } + + let mut actual = String::new(); + source.write(&mut actual); + + assert_eq!(actual, r#"["abc"]"#); + } + + fn written(node: &Node) -> String { + let mut out = String::new(); + node.write(&mut out); + out + } + + #[test] + fn typing_keeps_one_element_per_line_and_the_untouched_lines() { + let mut source = Node::new(Value::Array(Vec::new())); + let code = "if True:\n print('hello')\nx = 1"; + for end in 1..=code.len() { + source = rewritten_source(Some(&source), code[..end].to_string()); + } + assert_eq!(written(&source), r#"["if True:\n"," print('hello')\n","x = 1"]"#); + + // Typing at the end of the middle line leaves the other lines' elements as they were. + let edited = "if True:\n print('hello!')\nx = 1"; + let source = rewritten_source(Some(&source), edited.to_string()); + assert_eq!(written(&source), r#"["if True:\n"," print('hello!')\n","x = 1"]"#); + } + + #[test] + fn a_source_saved_one_character_per_element_is_joined_into_lines_where_it_is_edited() { + let parts = "ab\ncd".chars().map(|c| Value::String(c.to_string())).collect(); + let source = rewritten_source(Some(&Node::new(Value::Array(parts))), "ab\ncde".to_string()); + assert_eq!(written(&source), r#"["ab\n","cde"]"#); + } +}