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