Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Re-resolve anchor before applying AI inline assist edits (#43103)
Andrew Farkasd6d967f4431a
diff --git a/crates/agent_ui/src/buffer_codegen.rs b/crates/agent_ui/src/buffer_codegen.rsindex 0b7c1f61988..3877bede337 100644--- a/crates/agent_ui/src/buffer_codegen.rs+++ b/crates/agent_ui/src/buffer_codegen.rs@@ -491,6 +491,14 @@ impl CodegenAlternative {cx: &mut Context<Self>,) {let start_time = Instant::now();++ // Make a new snapshot and re-resolve anchor in case the document was modified.+ // This can happen often if the editor loses focus and is saved + reformatted,+ // as in https://github.com/zed-industries/zed/issues/39088+ self.snapshot = self.buffer.read(cx).snapshot(cx);+ self.range = self.snapshot.anchor_after(self.range.start)+ ..self.snapshot.anchor_after(self.range.end);+let snapshot = self.snapshot.clone();let selected_text = snapshot.text_for_range(self.range.start..self.range.end)diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rsindex 4f3f760ba8b..14475af5984 100644--- a/crates/language/src/buffer_tests.rs+++ b/crates/language/src/buffer_tests.rs@@ -3455,6 +3455,25 @@ fn test_contiguous_ranges() {);}+#[gpui::test]+fn test_insertion_after_deletion(cx: &mut gpui::App) {+ let buffer = cx.new(|cx| Buffer::local("struct Foo {\n \n}", cx));+ buffer.update(cx, |buffer, cx| {+ let mut anchor = buffer.anchor_after(17);+ buffer.edit([(12..18, "")], None, cx);+ let snapshot = buffer.snapshot();+ assert_eq!(snapshot.text(), "struct Foo {}");+ if !anchor.is_valid(&snapshot) {+ anchor = snapshot.anchor_after(snapshot.offset_for_anchor(&anchor));+ }+ buffer.edit([(anchor..anchor, "\n")], None, cx);+ buffer.edit([(anchor..anchor, "field1:")], None, cx);+ buffer.edit([(anchor..anchor, " i32,")], None, cx);+ let snapshot = buffer.snapshot();+ assert_eq!(snapshot.text(), "struct Foo {\nfield1: i32,}");+ })+}+#[gpui::test(iterations = 500)]fn test_trailing_whitespace_ranges(mut rng: StdRng) {// Generate a random multi-line string containing