agent_ui: Fix panic on editor changes in inline_assistant (#38303)

Lukas Wirth created

Fixes ZED-13P

Release Notes:

- N/A

Change summary

crates/agent_ui/src/inline_assistant.rs     | 31 ++++++++--------------
crates/gpui/src/platform/mac/metal_atlas.rs |  7 +++-
2 files changed, 17 insertions(+), 21 deletions(-)

Detailed changes

crates/agent_ui/src/inline_assistant.rs 🔗

@@ -744,19 +744,14 @@ impl InlineAssistant {
             .update(cx, |editor, cx| {
                 let scroll_top = editor.scroll_position(cx).y;
                 let scroll_bottom = scroll_top + editor.visible_line_count().unwrap_or(0.);
-                let prompt_row = editor
+                editor_assists.scroll_lock = editor
                     .row_for_block(decorations.prompt_block_id, cx)
-                    .unwrap()
-                    .0 as f32;
-
-                if (scroll_top..scroll_bottom).contains(&prompt_row) {
-                    editor_assists.scroll_lock = Some(InlineAssistScrollLock {
+                    .map(|row| row.0 as f32)
+                    .filter(|prompt_row| (scroll_top..scroll_bottom).contains(&prompt_row))
+                    .map(|prompt_row| InlineAssistScrollLock {
                         assist_id,
                         distance_from_top: prompt_row - scroll_top,
                     });
-                } else {
-                    editor_assists.scroll_lock = None;
-                }
             })
             .ok();
     }
@@ -917,14 +912,12 @@ impl InlineAssistant {
 
         editor.update(cx, |editor, cx| {
             let scroll_position = editor.scroll_position(cx);
-            let target_scroll_top = editor
-                .row_for_block(decorations.prompt_block_id, cx)
-                .unwrap()
-                .0 as f32
+            let target_scroll_top = editor.row_for_block(decorations.prompt_block_id, cx)?.0 as f32
                 - scroll_lock.distance_from_top;
             if target_scroll_top != scroll_position.y {
                 editor.set_scroll_position(point(scroll_position.x, target_scroll_top), window, cx);
             }
+            Some(())
         });
     }
 
@@ -968,14 +961,14 @@ impl InlineAssistant {
                     if let Some(decorations) = assist.decorations.as_ref() {
                         let distance_from_top = editor.update(cx, |editor, cx| {
                             let scroll_top = editor.scroll_position(cx).y;
-                            let prompt_row = editor
-                                .row_for_block(decorations.prompt_block_id, cx)
-                                .unwrap()
-                                .0 as f32;
-                            prompt_row - scroll_top
+                            let prompt_row =
+                                editor.row_for_block(decorations.prompt_block_id, cx)?.0 as f32;
+                            Some(prompt_row - scroll_top)
                         });
 
-                        if distance_from_top != scroll_lock.distance_from_top {
+                        if distance_from_top.is_none_or(|distance_from_top| {
+                            distance_from_top != scroll_lock.distance_from_top
+                        }) {
                             editor_assists.scroll_lock = None;
                         }
                     }

crates/gpui/src/platform/mac/metal_atlas.rs 🔗

@@ -167,11 +167,14 @@ impl MetalAtlasState {
 
         if let Some(ix) = index {
             texture_list.textures[ix] = Some(atlas_texture);
-            texture_list.textures.get_mut(ix).unwrap().as_mut().unwrap()
+            texture_list.textures.get_mut(ix)
         } else {
             texture_list.textures.push(Some(atlas_texture));
-            texture_list.textures.last_mut().unwrap().as_mut().unwrap()
+            texture_list.textures.last_mut()
         }
+        .unwrap()
+        .as_mut()
+        .unwrap()
     }
 
     fn texture(&self, id: AtlasTextureId) -> &MetalAtlasTexture {