salvation_cosmic_text/edit/
mod.rs1use alloc::sync::Arc;
2use core::ops::Range;
3
4#[cfg(not(feature = "std"))]
5use alloc::{string::String, vec::Vec};
6use core::cmp;
7use unicode_segmentation::UnicodeSegmentation;
8
9use crate::{AttrsList, AttrsOwned, BorrowedWithFontSystem, Buffer, Cursor, FontSystem, Motion};
10
11pub use self::editor::*;
12mod editor;
13
14#[cfg(feature = "syntect")]
15pub use self::syntect::*;
16#[cfg(feature = "syntect")]
17mod syntect;
18
19#[cfg(feature = "vi")]
20pub use self::vi::*;
21#[cfg(feature = "vi")]
22mod vi;
23
24#[derive(Clone, Debug, Eq, PartialEq)]
26pub enum Action {
27 Motion {
29 motion: Motion,
30 select: bool,
31 },
32 Escape,
34 SelectAll,
36 Insert(char),
38 Enter,
40 Backspace,
42 DeleteStartOfWord,
44 Delete,
46 DeleteEndOfWord,
48 Indent,
50 Unindent,
52 Click {
54 x: i32,
55 y: i32,
56 select: bool,
57 },
58 DoubleClick {
60 x: i32,
61 y: i32,
62 },
63 TripleClick {
65 x: i32,
66 y: i32,
67 },
68 Drag {
70 x: i32,
71 y: i32,
72 },
73 Scroll {
75 lines: i32,
76 },
77 SetPreedit {
87 preedit: String,
88 cursor: Option<(usize, usize)>,
89 attrs: Option<AttrsOwned>,
90 },
91}
92
93#[derive(Debug)]
94pub enum BufferRef<'buffer> {
95 Owned(Buffer),
96 Borrowed(&'buffer mut Buffer),
97 Arc(Arc<Buffer>),
98}
99
100impl<'buffer> From<Buffer> for BufferRef<'buffer> {
101 fn from(buffer: Buffer) -> Self {
102 Self::Owned(buffer)
103 }
104}
105
106impl<'buffer> From<&'buffer mut Buffer> for BufferRef<'buffer> {
107 fn from(buffer: &'buffer mut Buffer) -> Self {
108 Self::Borrowed(buffer)
109 }
110}
111
112impl<'buffer> From<Arc<Buffer>> for BufferRef<'buffer> {
113 fn from(arc: Arc<Buffer>) -> Self {
114 Self::Arc(arc)
115 }
116}
117
118#[derive(Clone, Debug)]
120pub struct ChangeItem {
121 pub start: Cursor,
123 pub end: Cursor,
125 pub text: String,
127 pub insert: bool,
129}
130
131impl ChangeItem {
132 pub fn reverse(&mut self) {
134 self.insert = !self.insert;
135 }
136}
137
138#[derive(Clone, Debug, Default)]
140pub struct Change {
141 pub items: Vec<ChangeItem>,
143}
144
145impl Change {
146 pub fn reverse(&mut self) {
148 self.items.reverse();
149 for item in self.items.iter_mut() {
150 item.reverse();
151 }
152 }
153}
154
155#[derive(Clone, Copy, Debug, Eq, PartialEq)]
157pub enum Selection {
158 None,
160 Normal(Cursor),
162 Line(Cursor),
164 Word(Cursor),
166 }
168
169pub trait Edit<'buffer> {
171 fn borrow_with<'font_system>(
173 &'font_system mut self,
174 font_system: &'font_system mut FontSystem,
175 ) -> BorrowedWithFontSystem<'font_system, Self>
176 where
177 Self: Sized,
178 {
179 BorrowedWithFontSystem {
180 inner: self,
181 font_system,
182 }
183 }
184
185 fn buffer_ref(&self) -> &BufferRef<'buffer>;
187
188 fn buffer_ref_mut(&mut self) -> &mut BufferRef<'buffer>;
190
191 fn with_buffer<F: FnOnce(&Buffer) -> T, T>(&self, f: F) -> T {
193 match self.buffer_ref() {
194 BufferRef::Owned(buffer) => f(buffer),
195 BufferRef::Borrowed(buffer) => f(buffer),
196 BufferRef::Arc(buffer) => f(buffer),
197 }
198 }
199
200 fn with_buffer_mut<F: FnOnce(&mut Buffer) -> T, T>(&mut self, f: F) -> T {
202 match self.buffer_ref_mut() {
203 BufferRef::Owned(buffer) => f(buffer),
204 BufferRef::Borrowed(buffer) => f(buffer),
205 BufferRef::Arc(buffer) => f(Arc::make_mut(buffer)),
206 }
207 }
208
209 fn redraw(&self) -> bool {
211 self.with_buffer(|buffer| buffer.redraw())
212 }
213
214 fn set_redraw(&mut self, redraw: bool) {
216 self.with_buffer_mut(|buffer| buffer.set_redraw(redraw))
217 }
218
219 fn cursor(&self) -> Cursor;
221
222 fn set_cursor_hidden(&mut self, hidden: bool);
232
233 fn set_cursor(&mut self, cursor: Cursor);
235
236 fn has_selection(&self) -> bool {
238 match self.selection() {
239 Selection::None => false,
240 Selection::Normal(selection) => {
241 let cursor = self.cursor();
242 selection.line != cursor.line || selection.index != cursor.index
243 }
244 Selection::Line(selection) => selection.line != self.cursor().line,
245 Selection::Word(_) => true,
246 }
247 }
248
249 fn selection(&self) -> Selection;
251
252 fn set_selection(&mut self, selection: Selection);
254
255 fn selection_bounds(&self) -> Option<(Cursor, Cursor)> {
258 self.with_buffer(|buffer| {
259 let cursor = self.cursor();
260 match self.selection() {
261 Selection::None => None,
262 Selection::Normal(select) => match select.line.cmp(&cursor.line) {
263 cmp::Ordering::Greater => Some((cursor, select)),
264 cmp::Ordering::Less => Some((select, cursor)),
265 cmp::Ordering::Equal => {
266 if select.index < cursor.index {
268 Some((select, cursor))
269 } else {
270 Some((cursor, select))
272 }
273 }
274 },
275 Selection::Line(select) => {
276 let start_line = cmp::min(select.line, cursor.line);
277 let end_line = cmp::max(select.line, cursor.line);
278 let end_index = buffer.lines[end_line].text().len();
279 Some((Cursor::new(start_line, 0), Cursor::new(end_line, end_index)))
280 }
281 Selection::Word(select) => {
282 let (mut start, mut end) = match select.line.cmp(&cursor.line) {
283 cmp::Ordering::Greater => (cursor, select),
284 cmp::Ordering::Less => (select, cursor),
285 cmp::Ordering::Equal => {
286 if select.index < cursor.index {
288 (select, cursor)
289 } else {
290 (cursor, select)
292 }
293 }
294 };
295
296 {
298 let line = &buffer.lines[start.line];
299 start.index = line
300 .text()
301 .unicode_word_indices()
302 .rev()
303 .map(|(i, _)| i)
304 .find(|&i| i < start.index)
305 .unwrap_or(0);
306 }
307
308 {
310 let line = &buffer.lines[end.line];
311 end.index = line
312 .text()
313 .unicode_word_indices()
314 .map(|(i, word)| i + word.len())
315 .find(|&i| i > end.index)
316 .unwrap_or(line.text().len());
317 }
318
319 Some((start, end))
320 }
321 }
322 })
323 }
324
325 fn auto_indent(&self) -> bool;
327
328 fn set_auto_indent(&mut self, auto_indent: bool);
330
331 fn tab_width(&self) -> u16;
333
334 fn set_tab_width(&mut self, tab_width: u16);
336
337 fn shape_as_needed(&mut self, font_system: &mut FontSystem, prune: bool);
339
340 fn delete_range(&mut self, start: Cursor, end: Cursor);
342
343 fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option<AttrsList>) -> Cursor;
345
346 fn copy_selection(&self) -> Option<String>;
348
349 fn delete_selection(&mut self) -> bool;
352
353 fn insert_string(&mut self, data: &str, attrs_list: Option<AttrsList>) {
356 self.delete_selection();
357 let new_cursor = self.insert_at(self.cursor(), data, attrs_list);
358 self.set_cursor(new_cursor);
359 }
360
361 fn apply_change(&mut self, change: &Change) -> bool;
363
364 fn start_change(&mut self);
366
367 fn finish_change(&mut self) -> Option<Change>;
369
370 fn preedit_range(&self) -> Option<Range<usize>>;
373
374 fn preedit_text(&self) -> Option<String>;
376
377 fn action(&mut self, font_system: &mut FontSystem, action: Action);
379
380 fn cursor_position(&self) -> Option<(i32, i32)>;
382}
383
384impl<'font_system, 'buffer, E: Edit<'buffer>> BorrowedWithFontSystem<'font_system, E> {
385 pub fn with_buffer_mut<F: FnOnce(&mut BorrowedWithFontSystem<Buffer>) -> T, T>(
387 &mut self,
388 f: F,
389 ) -> T {
390 self.inner.with_buffer_mut(|buffer| {
391 let mut borrowed = BorrowedWithFontSystem {
392 inner: buffer,
393 font_system: self.font_system,
394 };
395 f(&mut borrowed)
396 })
397 }
398
399 pub fn shape_as_needed(&mut self, prune: bool) {
401 self.inner.shape_as_needed(self.font_system, prune);
402 }
403
404 pub fn action(&mut self, action: Action) {
406 self.inner.action(self.font_system, action);
407 }
408}