status_bar: Add encoding indicator (#45476)
## Context / Related PRs This PR is the third part of the encoding support improvements, following: - #44819: Introduced initial legacy encoding support (Shift-JIS, etc.). - #45243: Fixed UTF-16 saving behavior and improved binary detection. ## Summary This PR implements a status bar item that displays the character encoding of the active buffer (e.g., `UTF-8`, `Shift_JIS`). It provides visibility into the file's encoding and indicates the presence of a Byte Order Mark (BOM). ## Features - **Encoding Indicator**: Displays the encoding name in the status bar. - **BOM Support**: Appends `(BOM)` to the encoding name if a BOM is detected (e.g., `UTF-8 (BOM)`). - **Configuration**: The active_encoding_button setting in status_bar accepts "enabled", "disabled", or "non_utf8". The default is "non_utf8", which displays the indicator for all encodings except standard UTF-8 (without BOM). - **Settings UI**: Provides a dropdown menu in the Settings UI to control this behavior. - **Documentation**: Updated `configuring-zed.md` and `visual-customization.md`. ## Implementation Details - Created `ActiveBufferEncoding` component in `crates/encoding_selector`. - The click handler for the button is currently a **no-op**. Implementing the functionality to reopen files with a specific encoding has potential implications for real-time collaboration (e.g., syncing buffer interpretation across peers). Therefore, this PR focuses strictly on the visualization and configuration aspects to keep the scope simple and focused. - Updated schema and default settings to include `active_encoding_button`. ## Screenshots <img width="487" height="104" alt="image" src="https://github.com/user-attachments/assets/041f096d-ac69-4bad-ac53-20cdcb41f733" /> <img width="454" height="99" alt="image" src="https://github.com/user-attachments/assets/ed76daa2-2733-484f-bb1f-4688357c035a" /> ## Configuration To hide the button, add the following to `settings.json`: ```json "status_bar": { "active_encoding_button": "disabled" } ``` - **enabled**: Always show the encoding. - **disabled**: Never show the encoding. - **non_utf8**: Shows for non-UTF-8 encodings and UTF-8 with BOM. Only hides for standard UTF-8 (Default). <img width="1347" height="415" alt="image" src="https://github.com/user-attachments/assets/7f4f4938-3320-4d21-852c-53ee886d9a44" /> ## Heuristic Limitations: The underlying detection logic (implemented in #44819 and #45243) prioritizes UTF-8 opening performance and does not guarantee perfect detection for all encodings. We consider this margin of error acceptable, similar to the behavior seen in VS Code. A future "Reopen with Encoding" feature would serve as the primary fallback for any misdetections. Release Notes: - Added a status bar item to display the active file's character encoding (e.g. `UTF-16`). This shows for non-utf8 files by default and can be configured with `{"status_bar":{"active_encoding_button":"disabled|enabled|non_utf8"}}`
This commit is contained in:
1 parent
7274a96154
commit
6b90aaa1bd
16 files changed
+210
-4
No files matched your search
@@ -0,0 +1,91 @@
|
||||
use editor::Editor;
|
||||
use encoding_rs::{Encoding, UTF_8};
|
||||
use gpui::{
|
||||
Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription, Window, div,
|
||||
};
|
||||
use ui::{Button, ButtonCommon, Clickable, LabelSize, Tooltip};
|
||||
use workspace::{
|
||||
StatusBarSettings, StatusItemView, Workspace,
|
||||
item::{ItemHandle, Settings},
|
||||
};
|
||||
|
||||
pub struct ActiveBufferEncoding {
|
||||
active_encoding: Option<&'static Encoding>,
|
||||
//workspace: WeakEntity<Workspace>,
|
||||
_observe_active_editor: Option<Subscription>,
|
||||
has_bom: bool,
|
||||
}
|
||||
|
||||
impl ActiveBufferEncoding {
|
||||
pub fn new(_workspace: &Workspace) -> Self {
|
||||
Self {
|
||||
active_encoding: None,
|
||||
//workspace: workspace.weak_handle(),
|
||||
_observe_active_editor: None,
|
||||
has_bom: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn update_encoding(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
|
||||
self.active_encoding = None;
|
||||
|
||||
let editor = editor.read(cx);
|
||||
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
|
||||
let buffer = buffer.read(cx);
|
||||
|
||||
self.active_encoding = Some(buffer.encoding());
|
||||
self.has_bom = buffer.has_bom();
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ActiveBufferEncoding {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let Some(active_encoding) = self.active_encoding else {
|
||||
return div().hidden();
|
||||
};
|
||||
|
||||
let display_option = StatusBarSettings::get_global(cx).active_encoding_button;
|
||||
let is_utf8 = active_encoding == UTF_8;
|
||||
if !display_option.should_show(is_utf8, self.has_bom) {
|
||||
return div().hidden();
|
||||
}
|
||||
|
||||
let mut text = active_encoding.name().to_string();
|
||||
if self.has_bom {
|
||||
text.push_str(" (BOM)");
|
||||
}
|
||||
|
||||
div().child(
|
||||
Button::new("change-encoding", text)
|
||||
.label_size(LabelSize::Small)
|
||||
.on_click(|_, _, _cx| {
|
||||
// No-op
|
||||
})
|
||||
.tooltip(Tooltip::text("Current Encoding")),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl StatusItemView for ActiveBufferEncoding {
|
||||
fn set_active_pane_item(
|
||||
&mut self,
|
||||
active_pane_item: Option<&dyn ItemHandle>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
|
||||
self._observe_active_editor =
|
||||
Some(cx.observe_in(&editor, window, Self::update_encoding));
|
||||
self.update_encoding(editor, window, cx);
|
||||
} else {
|
||||
self.active_encoding = None;
|
||||
self.has_bom = false;
|
||||
self._observe_active_editor = None;
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user