Pass file path to `EncodingSelector` via `Toggle` action, if there is

R Aadarsh created

one.

Change summary

crates/encodings/src/lib.rs             |  9 +++++++--
crates/encodings/src/selectors.rs       | 12 ++++++++++--
crates/project/src/invalid_item_view.rs |  8 ++++++--
crates/zed_actions/src/lib.rs           |  8 ++++++--
4 files changed, 29 insertions(+), 8 deletions(-)

Detailed changes

crates/encodings/src/lib.rs 🔗

@@ -8,6 +8,7 @@ use language::Buffer;
 use ui::{App, Button, ButtonCommon, Context, LabelSize, Render, Tooltip, Window, div};
 use ui::{Clickable, ParentElement};
 use workspace::{ItemHandle, StatusItemView, Workspace, with_active_or_new_workspace};
+use zed_actions::encodings::Toggle;
 
 use crate::selectors::encoding::EncodingSelector;
 use crate::selectors::save_or_reopen::EncodingSaveOrReopenSelector;
@@ -76,6 +77,7 @@ impl Render for EncodingIndicator {
                                     Action::Save,
                                     Some(buffer.downgrade()),
                                     weak_workspace,
+                                    None,
                                 );
                                 selector
                             })
@@ -291,11 +293,14 @@ pub fn encoding_from_name(name: &str) -> &'static Encoding {
 }
 
 pub fn init(cx: &mut App) {
-    cx.on_action(|_: &zed_actions::encodings::Toggle, cx: &mut App| {
+    cx.on_action(|action: &Toggle, cx: &mut App| {
+        let Toggle(path) = action.clone();
+        let path = path.to_path_buf();
+
         with_active_or_new_workspace(cx, |workspace, window, cx| {
             let weak_workspace = workspace.weak_handle();
             workspace.toggle_modal(window, cx, |window, cx| {
-                EncodingSelector::new(window, cx, Action::Reopen, None, weak_workspace)
+                EncodingSelector::new(window, cx, Action::Reopen, None, weak_workspace, Some(path))
             });
         });
     });

crates/encodings/src/selectors.rs 🔗

@@ -125,6 +125,7 @@ pub mod save_or_reopen {
                                 Action::Save,
                                 Some(buffer.downgrade()),
                                 weak_workspace,
+                                None,
                             );
                             selector
                         })
@@ -149,6 +150,7 @@ pub mod save_or_reopen {
                                 Action::Reopen,
                                 Some(buffer.downgrade()),
                                 weak_workspace,
+                                None,
                             );
                             selector
                         });
@@ -275,7 +277,7 @@ pub mod save_or_reopen {
 
 /// This module contains the encoding selector for choosing an encoding to save or reopen a file with.
 pub mod encoding {
-    use std::sync::atomic::AtomicBool;
+    use std::{path::PathBuf, sync::atomic::AtomicBool};
 
     use fuzzy::{StringMatch, StringMatchCandidate};
     use gpui::{AppContext, DismissEvent, Entity, EventEmitter, Focusable, WeakEntity};
@@ -294,6 +296,7 @@ pub mod encoding {
     pub struct EncodingSelector {
         picker: Entity<Picker<EncodingSelectorDelegate>>,
         workspace: WeakEntity<Workspace>,
+        path: Option<PathBuf>,
     }
 
     pub struct EncodingSelectorDelegate {
@@ -497,11 +500,16 @@ pub mod encoding {
             action: Action,
             buffer: Option<WeakEntity<Buffer>>,
             workspace: WeakEntity<Workspace>,
+            path: Option<PathBuf>,
         ) -> EncodingSelector {
             let delegate = EncodingSelectorDelegate::new(cx.entity().downgrade(), buffer, action);
             let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
 
-            EncodingSelector { picker, workspace }
+            EncodingSelector {
+                picker,
+                workspace,
+                path,
+            }
         }
     }
 

crates/project/src/invalid_item_view.rs 🔗

@@ -78,6 +78,8 @@ impl Focusable for InvalidItemView {
 impl Render for InvalidItemView {
     fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl gpui::IntoElement {
         let abs_path = self.abs_path.clone();
+        let path = self.abs_path.clone();
+
         v_flex()
             .size_full()
             .track_focus(&self.focus_handle(cx))
@@ -120,9 +122,11 @@ impl Render for InvalidItemView {
                                         )
                                         .style(ButtonStyle::Outlined)
                                         .on_click(
-                                            |_, window, cx| {
+                                            move |_, window, cx| {
                                                 window.dispatch_action(
-                                                    Box::new(zed_actions::encodings::Toggle),
+                                                    Box::new(zed_actions::encodings::Toggle(
+                                                        path.clone(),
+                                                    )),
                                                     cx,
                                                 )
                                             },

crates/zed_actions/src/lib.rs 🔗

@@ -300,10 +300,14 @@ pub mod settings_profile_selector {
 }
 
 pub mod encodings {
+    use std::sync::Arc;
+
     use gpui::Action;
+    use schemars::JsonSchema;
+    use serde::Deserialize;
 
-    #[derive(PartialEq, Debug, Clone, Action)]
-    pub struct Toggle;
+    #[derive(PartialEq, Debug, Clone, Action, JsonSchema, Deserialize)]
+    pub struct Toggle(pub Arc<std::path::Path>);
 }
 
 pub mod agent {