lib.rs

  1//! A crate for handling file encodings in the text editor.
  2use editor::{Editor, EditorSettings};
  3use encoding_rs::Encoding;
  4use gpui::{ClickEvent, Entity, Subscription, WeakEntity};
  5use settings::Settings;
  6use ui::{Button, ButtonCommon, Context, LabelSize, Render, Tooltip, Window, div};
  7use ui::{Clickable, ParentElement};
  8use workspace::{ItemHandle, StatusItemView, Workspace};
  9
 10use crate::selectors::save_or_reopen::EncodingSaveOrReopenSelector;
 11
 12/// A status bar item that shows the current file encoding and allows changing it.
 13pub struct EncodingIndicator {
 14    pub encoding: Option<&'static Encoding>,
 15    pub workspace: WeakEntity<Workspace>,
 16    observe: Option<Subscription>, // Subscription to observe changes in the active editor
 17    show: bool, // Whether to show the indicator or not, based on whether an editor is active
 18}
 19
 20pub mod selectors;
 21
 22impl Render for EncodingIndicator {
 23    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
 24        let status_element = div();
 25
 26        if (!EditorSettings::get_global(cx).status_bar.encoding_indicator) || !self.show {
 27            return status_element;
 28        }
 29
 30        status_element.child(
 31            Button::new(
 32                "encoding",
 33                encoding_name(self.encoding.unwrap_or(encoding_rs::UTF_8)),
 34            )
 35            .label_size(LabelSize::Small)
 36            .tooltip(Tooltip::text("Select Encoding"))
 37            .on_click(cx.listener(|indicator, _: &ClickEvent, window, cx| {
 38                if let Some(workspace) = indicator.workspace.upgrade() {
 39                    workspace.update(cx, |workspace, cx| {
 40                        EncodingSaveOrReopenSelector::toggle(workspace, window, cx)
 41                    })
 42                } else {
 43                }
 44            })),
 45        )
 46    }
 47}
 48
 49impl EncodingIndicator {
 50    pub fn new(
 51        encoding: Option<&'static Encoding>,
 52        workspace: WeakEntity<Workspace>,
 53        observe: Option<Subscription>,
 54    ) -> EncodingIndicator {
 55        EncodingIndicator {
 56            encoding,
 57            workspace,
 58            observe,
 59            show: true,
 60        }
 61    }
 62
 63    pub fn update(
 64        &mut self,
 65        editor: Entity<Editor>,
 66        _: &mut Window,
 67        cx: &mut Context<EncodingIndicator>,
 68    ) {
 69        let editor = editor.read(cx);
 70        if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
 71            let encoding = buffer.read(cx).encoding;
 72            self.encoding = Some(encoding);
 73        }
 74
 75        cx.notify();
 76    }
 77}
 78
 79impl StatusItemView for EncodingIndicator {
 80    fn set_active_pane_item(
 81        &mut self,
 82        active_pane_item: Option<&dyn ItemHandle>,
 83        window: &mut Window,
 84        cx: &mut Context<Self>,
 85    ) {
 86        match active_pane_item.and_then(|item| item.downcast::<Editor>()) {
 87            Some(editor) => {
 88                self.observe = Some(cx.observe_in(&editor, window, Self::update));
 89                self.update(editor, window, cx);
 90                self.show = true;
 91            }
 92            None => {
 93                self.encoding = None;
 94                self.observe = None;
 95                self.show = false;
 96            }
 97        }
 98    }
 99}
100
101/// Get a human-readable name for the given encoding.
102pub fn encoding_name(encoding: &'static Encoding) -> String {
103    let name = encoding.name();
104
105    match name {
106        "UTF-8" => "UTF-8",
107        "windows-1252" => "Windows-1252",
108        "windows-1251" => "Windows-1251",
109        "windows-1250" => "Windows-1250",
110        "ISO-8859-2" => "ISO 8859-2",
111        "ISO-8859-3" => "ISO 8859-3",
112        "ISO-8859-4" => "ISO 8859-4",
113        "ISO-8859-5" => "ISO 8859-5",
114        "ISO-8859-6" => "ISO 8859-6",
115        "ISO-8859-7" => "ISO 8859-7",
116        "ISO-8859-8" => "ISO 8859-8",
117        "ISO-8859-13" => "ISO 8859-13",
118        "ISO-8859-15" => "ISO 8859-15",
119        "KOI8-R" => "KOI8-R",
120        "KOI8-U" => "KOI8-U",
121        "macintosh" => "MacRoman",
122        "x-mac-cyrillic" => "Mac Cyrillic",
123        "windows-874" => "Windows-874",
124        "windows-1253" => "Windows-1253",
125        "windows-1254" => "Windows-1254",
126        "windows-1255" => "Windows-1255",
127        "windows-1256" => "Windows-1256",
128        "windows-1257" => "Windows-1257",
129        "windows-1258" => "Windows-1258",
130        "EUC-KR" => "Windows-949",
131        "EUC-JP" => "EUC-JP",
132        "ISO-2022-JP" => "ISO 2022-JP",
133        "GBK" => "GBK",
134        "gb18030" => "GB18030",
135        "Big5" => "Big5",
136        _ => name,
137    }
138    .to_string()
139}
140
141/// Get an encoding from its index in the predefined list.
142/// If the index is out of range, UTF-8 is returned as a default.
143pub fn encoding_from_index(index: usize) -> &'static Encoding {
144    match index {
145        0 => encoding_rs::UTF_8,
146        1 => encoding_rs::WINDOWS_1252,
147        2 => encoding_rs::WINDOWS_1251,
148        3 => encoding_rs::WINDOWS_1250,
149        4 => encoding_rs::ISO_8859_2,
150        5 => encoding_rs::ISO_8859_3,
151        6 => encoding_rs::ISO_8859_4,
152        7 => encoding_rs::ISO_8859_5,
153        8 => encoding_rs::ISO_8859_6,
154        9 => encoding_rs::ISO_8859_7,
155        10 => encoding_rs::ISO_8859_8,
156        11 => encoding_rs::ISO_8859_13,
157        12 => encoding_rs::ISO_8859_15,
158        13 => encoding_rs::KOI8_R,
159        14 => encoding_rs::KOI8_U,
160        15 => encoding_rs::MACINTOSH,
161        16 => encoding_rs::X_MAC_CYRILLIC,
162        17 => encoding_rs::WINDOWS_874,
163        18 => encoding_rs::WINDOWS_1253,
164        19 => encoding_rs::WINDOWS_1254,
165        20 => encoding_rs::WINDOWS_1255,
166        21 => encoding_rs::WINDOWS_1256,
167        22 => encoding_rs::WINDOWS_1257,
168        23 => encoding_rs::WINDOWS_1258,
169        24 => encoding_rs::EUC_KR,
170        25 => encoding_rs::EUC_JP,
171        26 => encoding_rs::ISO_2022_JP,
172        27 => encoding_rs::GBK,
173        28 => encoding_rs::GB18030,
174        29 => encoding_rs::BIG5,
175        _ => encoding_rs::UTF_8,
176    }
177}
178
179/// Get an encoding from its name.
180pub fn encoding_from_name(name: &str) -> &'static Encoding {
181    match name {
182        "UTF-8" => encoding_rs::UTF_8,
183        "Windows-1252" => encoding_rs::WINDOWS_1252,
184        "Windows-1251" => encoding_rs::WINDOWS_1251,
185        "Windows-1250" => encoding_rs::WINDOWS_1250,
186        "ISO 8859-2" => encoding_rs::ISO_8859_2,
187        "ISO 8859-3" => encoding_rs::ISO_8859_3,
188        "ISO 8859-4" => encoding_rs::ISO_8859_4,
189        "ISO 8859-5" => encoding_rs::ISO_8859_5,
190        "ISO 8859-6" => encoding_rs::ISO_8859_6,
191        "ISO 8859-7" => encoding_rs::ISO_8859_7,
192        "ISO 8859-8" => encoding_rs::ISO_8859_8,
193        "ISO 8859-13" => encoding_rs::ISO_8859_13,
194        "ISO 8859-15" => encoding_rs::ISO_8859_15,
195        "KOI8-R" => encoding_rs::KOI8_R,
196        "KOI8-U" => encoding_rs::KOI8_U,
197        "MacRoman" => encoding_rs::MACINTOSH,
198        "Mac Cyrillic" => encoding_rs::X_MAC_CYRILLIC,
199        "Windows-874" => encoding_rs::WINDOWS_874,
200        "Windows-1253" => encoding_rs::WINDOWS_1253,
201        "Windows-1254" => encoding_rs::WINDOWS_1254,
202        "Windows-1255" => encoding_rs::WINDOWS_1255,
203        "Windows-1256" => encoding_rs::WINDOWS_1256,
204        "Windows-1257" => encoding_rs::WINDOWS_1257,
205        "Windows-1258" => encoding_rs::WINDOWS_1258,
206        "Windows-949" => encoding_rs::EUC_KR,
207        "EUC-JP" => encoding_rs::EUC_JP,
208        "ISO 2022-JP" => encoding_rs::ISO_2022_JP,
209        "GBK" => encoding_rs::GBK,
210        "GB18030" => encoding_rs::GB18030,
211        "Big5" => encoding_rs::BIG5,
212        "HZ-GB-2312" => encoding_rs::UTF_8, // encoding_rs doesn't support HZ, fallback to UTF-8
213        _ => encoding_rs::UTF_8,            // Default to UTF-8 for unknown names
214    }
215}