Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/keymaps/default-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@
"ctrl-?": "agent::ToggleFocus",
"alt-save": "workspace::SaveAll",
"ctrl-alt-s": "workspace::SaveAll",
"ctrl-k n": "encoding_selector::Toggle",
"ctrl-k m": "language_selector::Toggle",
"ctrl-k ctrl-m": "toolchain::AddToolchain",
"escape": "workspace::Unfollow",
Expand Down
1 change: 1 addition & 0 deletions assets/keymaps/default-macos.json
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@
"cmd-shift-d": "debug_panel::ToggleFocus",
"cmd-?": "agent::ToggleFocus",
"cmd-alt-s": "workspace::SaveAll",
"cmd-k n": "encoding_selector::Toggle",
"cmd-k m": "language_selector::Toggle",
"cmd-k cmd-m": "toolchain::AddToolchain",
"escape": "workspace::Unfollow",
Expand Down
1 change: 1 addition & 0 deletions assets/keymaps/default-windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@
"ctrl-shift-d": "debug_panel::ToggleFocus",
"ctrl-shift-/": "agent::ToggleFocus",
"ctrl-k s": "workspace::SaveAll",
"ctrl-k n": "encoding_selector::Toggle",
"ctrl-k m": "language_selector::Toggle",
"ctrl-m ctrl-m": "toolchain::AddToolchain",
"escape": "workspace::Unfollow",
Expand Down
1 change: 1 addition & 0 deletions assets/keymaps/macos/atom.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"context": "Editor && mode == full",
"bindings": {
"ctrl-shift-l": "language_selector::Toggle",
"ctrl-shift-n": "encoding_selector::Toggle",
"cmd-|": "pane::RevealInProjectPanel",
"cmd-b": "editor::GoToDefinition",
"alt-cmd-b": "editor::GoToDefinitionSplit",
Expand Down
5 changes: 5 additions & 0 deletions crates/encoding_selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ doctest = false
[dependencies]
editor.workspace = true
encoding_rs.workspace = true
fuzzy.workspace = true
gpui.workspace = true
language.workspace = true
picker.workspace = true
project.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true
65 changes: 54 additions & 11 deletions crates/encoding_selector/src/active_buffer_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::{EncodingSelector, Toggle};

use editor::Editor;
use encoding_rs::{Encoding, UTF_8};
use gpui::{
Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription, Window, div,
Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription, WeakEntity, Window,
div,
};
use project::Project;
use ui::{Button, ButtonCommon, Clickable, LabelSize, Tooltip};
use workspace::{
StatusBarSettings, StatusItemView, Workspace,
Expand All @@ -11,30 +15,43 @@ use workspace::{

pub struct ActiveBufferEncoding {
active_encoding: Option<&'static Encoding>,
//workspace: WeakEntity<Workspace>,
workspace: WeakEntity<Workspace>,
project: Entity<Project>,
_observe_active_editor: Option<Subscription>,
has_bom: bool,
is_dirty: bool,
is_shared: bool,
is_via_remote_server: bool,
}

impl ActiveBufferEncoding {
pub fn new(_workspace: &Workspace) -> Self {
pub fn new(workspace: &Workspace) -> Self {
Self {
active_encoding: None,
//workspace: workspace.weak_handle(),
workspace: workspace.weak_handle(),
project: workspace.project().clone(),
_observe_active_editor: None,
has_bom: false,
is_dirty: false,
is_shared: false,
is_via_remote_server: false,
}
}

fn update_encoding(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
self.active_encoding = None;
self.has_bom = false;
self.is_dirty = false;

let editor = editor.read(cx);
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
let buffer = buffer.read(cx);
let project = self.project.read(cx);
self.is_shared = project.is_shared();
self.is_via_remote_server = project.is_via_remote_server();

if let Some((_, buffer, _)) = editor.read(cx).active_excerpt(cx) {
let buffer = buffer.read(cx);
self.active_encoding = Some(buffer.encoding());
self.has_bom = buffer.has_bom();
self.is_dirty = buffer.is_dirty();
}

cx.notify();
Expand All @@ -58,13 +75,36 @@ impl Render for ActiveBufferEncoding {
text.push_str(" (BOM)");
}

let (disabled, tooltip_text) = if self.is_dirty {
(true, "Save file to change encoding")
} else if self.is_shared {
(true, "Cannot change encoding during collaboration")
} else if self.is_via_remote_server {
(true, "Cannot change encoding of remote server file")
} else {
(false, "Reopen with Encoding")
};

div().child(
Button::new("change-encoding", text)
.label_size(LabelSize::Small)
.on_click(|_, _, _cx| {
// No-op
})
.tooltip(Tooltip::text("Current Encoding")),
.on_click(cx.listener(move |this, _, window, cx| {
if disabled {
return;
}
if let Some(workspace) = this.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
EncodingSelector::toggle(workspace, window, cx)
});
}
}))
.tooltip(move |_window, cx| {
if disabled {
Tooltip::text(tooltip_text)(_window, cx)
} else {
Tooltip::for_action(tooltip_text, &Toggle, cx)
}
}),
)
}
}
Expand All @@ -83,6 +123,9 @@ impl StatusItemView for ActiveBufferEncoding {
} else {
self.active_encoding = None;
self.has_bom = false;
self.is_dirty = false;
self.is_shared = false;
self.is_via_remote_server = false;
self._observe_active_editor = None;
}

Expand Down
Loading
Loading