agent_ui: Fix panic in message editor (#44493)

Bennet Bo Fenner created

Release Notes:

- N/A

Change summary

crates/agent_ui/src/acp/message_editor.rs | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

Detailed changes

crates/agent_ui/src/acp/message_editor.rs 🔗

@@ -567,6 +567,14 @@ impl MessageEditor {
             {
                 cx.stop_propagation();
 
+                let insertion_target = self
+                    .editor
+                    .read(cx)
+                    .selections
+                    .newest_anchor()
+                    .start
+                    .text_anchor;
+
                 let project = workspace.read(cx).project().clone();
                 for selection in selections {
                     if let (Some(file_path), Some(line_range)) =
@@ -587,8 +595,7 @@ impl MessageEditor {
                                 let snapshot = buffer.snapshot(cx);
                                 let (excerpt_id, _, buffer_snapshot) =
                                     snapshot.as_singleton().unwrap();
-                                let start_offset = buffer_snapshot.len();
-                                let text_anchor = buffer_snapshot.anchor_before(start_offset);
+                                let text_anchor = insertion_target.bias_left(&buffer_snapshot);
 
                                 editor.insert(&mention_text, window, cx);
                                 editor.insert(" ", window, cx);