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