diff --git a/crates/encodings/src/lib.rs b/crates/encodings/src/lib.rs index 318f4660eeb854bf701113c28db04e9e0bc30aa1..dc8bfe56ba48eb5f44288d630ea17ad001019ded 100644 --- a/crates/encodings/src/lib.rs +++ b/crates/encodings/src/lib.rs @@ -1,5 +1,6 @@ //! A crate for handling file encodings in the text editor. +use crate::selectors::encoding::Action; use editor::{Editor, EditorSettings}; use encoding_rs::Encoding; use gpui::{ClickEvent, Entity, Subscription, WeakEntity}; @@ -9,6 +10,7 @@ use ui::{Button, ButtonCommon, Context, LabelSize, Render, Tooltip, Window, div} use ui::{Clickable, ParentElement}; use workspace::{ItemHandle, StatusItemView, Workspace}; +use crate::selectors::encoding::EncodingSelector; use crate::selectors::save_or_reopen::EncodingSaveOrReopenSelector; /// A status bar item that shows the current file encoding and allows changing it. @@ -22,7 +24,12 @@ pub struct EncodingIndicator { /// Subscription to observe changes in the `encoding` field of the `Buffer` struct observe_buffer_encoding: Option, - show: bool, // Whether to show the indicator or not, based on whether an editor is active + /// Whether to show the indicator or not, based on whether an editor is active + show: bool, + + /// Whether to show `EncodingSaveOrReopenSelector`. It will be shown only when + /// the current buffer is associated with a file. + show_save_or_reopen_selector: bool, } pub mod selectors; @@ -30,6 +37,7 @@ pub mod selectors; impl Render for EncodingIndicator { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl ui::IntoElement { let status_element = div(); + let show_save_or_reopen_selector = self.show_save_or_reopen_selector; if (!EditorSettings::get_global(cx).status_bar.encoding_indicator) || !self.show { return status_element; @@ -42,12 +50,38 @@ impl Render for EncodingIndicator { ) .label_size(LabelSize::Small) .tooltip(Tooltip::text("Select Encoding")) - .on_click(cx.listener(|indicator, _: &ClickEvent, window, cx| { + .on_click(cx.listener(move |indicator, _: &ClickEvent, window, cx| { if let Some(workspace) = indicator.workspace.upgrade() { - workspace.update(cx, |workspace, cx| { - EncodingSaveOrReopenSelector::toggle(workspace, window, cx) + workspace.update(cx, move |workspace, cx| { + // Open the `EncodingSaveOrReopenSelector` if the buffer is associated with a file, + if show_save_or_reopen_selector { + EncodingSaveOrReopenSelector::toggle(workspace, window, cx) + } + // otherwise, open the `EncodingSelector` directly. + else { + let (_, buffer, _) = workspace + .active_item(cx) + .unwrap() + .act_as::(cx) + .unwrap() + .read(cx) + .active_excerpt(cx) + .unwrap(); + + let weak_workspace = workspace.weak_handle(); + + workspace.toggle_modal(window, cx, |window, cx| { + let selector = EncodingSelector::new( + window, + cx, + Action::Save, + buffer.downgrade(), + weak_workspace, + ); + selector + }) + } }) - } else { } })), ) @@ -67,6 +101,7 @@ impl EncodingIndicator { observe_editor, show: true, observe_buffer_encoding, + show_save_or_reopen_selector: false, } } @@ -81,6 +116,12 @@ impl EncodingIndicator { if let Some((_, buffer, _)) = editor.active_excerpt(cx) { let encoding = buffer.read(cx).encoding.clone(); self.encoding = Some(&*encoding.lock().unwrap()); + + if let Some(_) = buffer.read(cx).file() { + self.show_save_or_reopen_selector = true; + } else { + self.show_save_or_reopen_selector = false; + } } cx.notify(); diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs index 3f0be2fa07898815031e44415cc446d41e22dc07..6b47d507bb3d899e103b986cb3b6ebf356e51f6e 100644 --- a/crates/fs/src/fs.rs +++ b/crates/fs/src/fs.rs @@ -633,7 +633,7 @@ impl Fs for RealFs { let path = path.to_path_buf(); let encoding = EncodingWrapper::new(encoding_rs::UTF_8); let text = smol::unblock(async || { - Ok(encodings::to_utf8(std::fs::read(path)?, encoding, false, None).await?) + encodings::to_utf8(std::fs::read(path)?, encoding, false, None).await }) .await .await; diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index a90fa3411d4b3224ca17791d0bd488e42afd2ed9..ea28f37ab1aaa0a7402276c7216cf0b3f35943f6 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1364,7 +1364,7 @@ impl Buffer { let Some((new_mtime, new_text)) = this.update(cx, |this, cx| { let file = this.file.as_ref()?.as_local()?; Some((file.disk_state().mtime(), { - file.load_with_encoding(cx, &*encoding.lock().unwrap(), force, encoding.clone()) + file.load_with_encoding(cx, &encoding.lock().unwrap(), force, encoding.clone()) })) })? else { diff --git a/crates/project/src/buffer_store.rs b/crates/project/src/buffer_store.rs index 9ae1ed0310e53dc20cc95a03a7c55cf887af0697..a1fe441abd89a428e52d58bfe5869512f494f5e7 100644 --- a/crates/project/src/buffer_store.rs +++ b/crates/project/src/buffer_store.rs @@ -402,7 +402,7 @@ impl LocalBufferStore { text, line_ending, cx, - &*encoding.lock().unwrap(), + &encoding.lock().unwrap(), ) });