1use crate::core::text::editor::{
3 self, Action, Cursor, Direction, Edit, Motion,
4};
5use crate::core::text::highlighter::{self, Highlighter};
6use crate::core::text::{LineHeight, Wrapping};
7use crate::core::{Font, Pixels, Point, Rectangle, Size};
8use crate::text;
9
10use cosmic_text::Edit as _;
11
12use std::borrow::Cow;
13use std::fmt;
14use std::sync::{self, Arc, RwLock};
15
16#[derive(Debug, PartialEq)]
18pub struct Editor(Option<Arc<Internal>>);
19
20struct Internal {
21 editor: cosmic_text::Editor<'static>,
22 cursor: RwLock<Option<Cursor>>,
23 font: Font,
24 bounds: Size,
25 topmost_line_changed: Option<usize>,
26 version: text::Version,
27}
28
29impl Editor {
30 pub fn new() -> Self {
32 Self::default()
33 }
34
35 pub fn buffer(&self) -> &cosmic_text::Buffer {
37 buffer_from_editor(&self.internal().editor)
38 }
39
40 pub fn downgrade(&self) -> Weak {
46 let editor = self.internal();
47
48 Weak {
49 raw: Arc::downgrade(editor),
50 bounds: editor.bounds,
51 }
52 }
53
54 fn internal(&self) -> &Arc<Internal> {
55 self.0
56 .as_ref()
57 .expect("Editor should always be initialized")
58 }
59}
60
61impl editor::Editor for Editor {
62 type Font = Font;
63
64 fn with_text(text: &str) -> Self {
65 let mut buffer = cosmic_text::Buffer::new_empty(cosmic_text::Metrics {
66 font_size: 1.0,
67 line_height: 1.0,
68 });
69
70 let mut font_system =
71 text::font_system().write().expect("Write font system");
72
73 buffer.set_text(
74 font_system.raw(),
75 text,
76 cosmic_text::Attrs::new(),
77 cosmic_text::Shaping::Advanced,
78 );
79
80 Editor(Some(Arc::new(Internal {
81 editor: cosmic_text::Editor::new(buffer),
82 version: font_system.version(),
83 ..Default::default()
84 })))
85 }
86
87 fn is_empty(&self) -> bool {
88 let buffer = self.buffer();
89
90 buffer.lines.is_empty()
91 || (buffer.lines.len() == 1 && buffer.lines[0].text().is_empty())
92 }
93
94 fn line(&self, index: usize) -> Option<editor::Line<'_>> {
95 self.buffer().lines.get(index).map(|line| editor::Line {
96 text: Cow::Borrowed(line.text()),
97 ending: match line.ending() {
98 cosmic_text::LineEnding::Lf => editor::LineEnding::Lf,
99 cosmic_text::LineEnding::CrLf => editor::LineEnding::CrLf,
100 cosmic_text::LineEnding::Cr => editor::LineEnding::Cr,
101 cosmic_text::LineEnding::LfCr => editor::LineEnding::LfCr,
102 cosmic_text::LineEnding::None => editor::LineEnding::None,
103 },
104 })
105 }
106
107 fn line_count(&self) -> usize {
108 self.buffer().lines.len()
109 }
110
111 fn selection(&self) -> Option<String> {
112 self.internal().editor.copy_selection()
113 }
114
115 fn cursor(&self) -> editor::Cursor {
116 let internal = self.internal();
117
118 if let Ok(Some(cursor)) = internal.cursor.read().as_deref() {
119 return cursor.clone();
120 }
121
122 let cursor = internal.editor.cursor();
123 let buffer = buffer_from_editor(&internal.editor);
124
125 let cursor = match internal.editor.selection_bounds() {
126 Some((start, end)) => {
127 let line_height = buffer.metrics().line_height;
128 let selected_lines = end.line - start.line + 1;
129
130 let visual_lines_offset =
131 visual_lines_offset(start.line, buffer);
132
133 let regions = buffer
134 .lines
135 .iter()
136 .skip(start.line)
137 .take(selected_lines)
138 .enumerate()
139 .flat_map(|(i, line)| {
140 highlight_line(
141 line,
142 if i == 0 { start.index } else { 0 },
143 if i == selected_lines - 1 {
144 end.index
145 } else {
146 line.text().len()
147 },
148 )
149 })
150 .enumerate()
151 .filter_map(|(visual_line, (x, width))| {
152 if width > 0.0 {
153 Some(Rectangle {
154 x,
155 width,
156 y: (visual_line as i32 + visual_lines_offset)
157 as f32
158 * line_height
159 - buffer.scroll().vertical,
160 height: line_height,
161 })
162 } else {
163 None
164 }
165 })
166 .collect();
167
168 Cursor::Selection(regions)
169 }
170 _ => {
171 let line_height = buffer.metrics().line_height;
172
173 let visual_lines_offset =
174 visual_lines_offset(cursor.line, buffer);
175
176 let line = buffer
177 .lines
178 .get(cursor.line)
179 .expect("Cursor line should be present");
180
181 let layout =
182 line.layout_opt().expect("Line layout should be cached");
183
184 let mut lines = layout.iter().enumerate();
185
186 let (visual_line, offset) = lines
187 .find_map(|(i, line)| {
188 let start = line
189 .glyphs
190 .first()
191 .map(|glyph| glyph.start)
192 .unwrap_or(0);
193 let end = line
194 .glyphs
195 .last()
196 .map(|glyph| glyph.end)
197 .unwrap_or(0);
198
199 let is_cursor_before_start = start > cursor.index;
200
201 let is_cursor_before_end = match cursor.affinity {
202 cosmic_text::Affinity::Before => {
203 cursor.index <= end
204 }
205 cosmic_text::Affinity::After => cursor.index < end,
206 };
207
208 if is_cursor_before_start {
209 Some((i - 1, layout[i - 1].w))
218 } else if is_cursor_before_end {
219 let offset = line
220 .glyphs
221 .iter()
222 .take_while(|glyph| cursor.index > glyph.start)
223 .map(|glyph| glyph.w)
224 .sum();
225
226 Some((i, offset))
227 } else {
228 None
229 }
230 })
231 .unwrap_or((
232 layout.len().saturating_sub(1),
233 layout.last().map(|line| line.w).unwrap_or(0.0),
234 ));
235
236 Cursor::Caret(Point::new(
237 offset,
238 (visual_lines_offset + visual_line as i32) as f32
239 * line_height
240 - buffer.scroll().vertical,
241 ))
242 }
243 };
244
245 *internal.cursor.write().expect("Write to cursor cache") =
246 Some(cursor.clone());
247
248 cursor
249 }
250
251 fn cursor_position(&self) -> (usize, usize) {
252 let cursor = self.internal().editor.cursor();
253
254 (cursor.line, cursor.index)
255 }
256
257 fn perform(&mut self, action: Action) {
258 let mut font_system =
259 text::font_system().write().expect("Write font system");
260
261 let editor =
262 self.0.take().expect("Editor should always be initialized");
263
264 let mut internal = Arc::try_unwrap(editor)
266 .expect("Editor cannot have multiple strong references");
267
268 let editor = &mut internal.editor;
269
270 let _ = internal
272 .cursor
273 .write()
274 .expect("Write to cursor cache")
275 .take();
276
277 match action {
278 Action::Move(motion) => {
280 if let Some((start, end)) = editor.selection_bounds() {
281 editor.set_selection(cosmic_text::Selection::None);
282
283 match motion {
284 Motion::Home
287 | Motion::End
288 | Motion::DocumentStart
289 | Motion::DocumentEnd => {
290 editor.action(
291 font_system.raw(),
292 cosmic_text::Action::Motion(to_motion(motion)),
293 );
294 }
295 _ => editor.set_cursor(match motion.direction() {
297 Direction::Left => start,
298 Direction::Right => end,
299 }),
300 }
301 } else {
302 editor.action(
303 font_system.raw(),
304 cosmic_text::Action::Motion(to_motion(motion)),
305 );
306 }
307 }
308
309 Action::Select(motion) => {
311 let cursor = editor.cursor();
312
313 if editor.selection_bounds().is_none() {
314 editor
315 .set_selection(cosmic_text::Selection::Normal(cursor));
316 }
317
318 editor.action(
319 font_system.raw(),
320 cosmic_text::Action::Motion(to_motion(motion)),
321 );
322
323 if let Some((start, end)) = editor.selection_bounds() {
325 if start.line == end.line && start.index == end.index {
326 editor.set_selection(cosmic_text::Selection::None);
327 }
328 }
329 }
330 Action::SelectWord => {
331 let cursor = editor.cursor();
332
333 editor.set_selection(cosmic_text::Selection::Word(cursor));
334 }
335 Action::SelectLine => {
336 let cursor = editor.cursor();
337
338 editor.set_selection(cosmic_text::Selection::Line(cursor));
339 }
340 Action::SelectAll => {
341 let buffer = buffer_from_editor(editor);
342
343 if buffer.lines.len() > 1
344 || buffer
345 .lines
346 .first()
347 .is_some_and(|line| !line.text().is_empty())
348 {
349 let cursor = editor.cursor();
350
351 editor.set_selection(cosmic_text::Selection::Normal(
352 cosmic_text::Cursor {
353 line: 0,
354 index: 0,
355 ..cursor
356 },
357 ));
358
359 editor.action(
360 font_system.raw(),
361 cosmic_text::Action::Motion(
362 cosmic_text::Motion::BufferEnd,
363 ),
364 );
365 }
366 }
367
368 Action::Edit(edit) => {
370 match edit {
371 Edit::Insert(c) => {
372 editor.action(
373 font_system.raw(),
374 cosmic_text::Action::Insert(c),
375 );
376 }
377 Edit::Paste(text) => {
378 editor.insert_string(&text, None);
379 }
380 Edit::Enter => {
381 editor.action(
382 font_system.raw(),
383 cosmic_text::Action::Enter,
384 );
385 }
386 Edit::Backspace => {
387 editor.action(
388 font_system.raw(),
389 cosmic_text::Action::Backspace,
390 );
391 }
392 Edit::Delete => {
393 editor.action(
394 font_system.raw(),
395 cosmic_text::Action::Delete,
396 );
397 }
398 }
399
400 let cursor = editor.cursor();
401 let selection_start = editor
402 .selection_bounds()
403 .map(|(start, _)| start)
404 .unwrap_or(cursor);
405
406 internal.topmost_line_changed = Some(selection_start.line);
407 }
408
409 Action::Click(position) => {
411 editor.action(
412 font_system.raw(),
413 cosmic_text::Action::Click {
414 x: position.x as i32,
415 y: position.y as i32,
416 },
417 );
418 }
419 Action::Drag(position) => {
420 editor.action(
421 font_system.raw(),
422 cosmic_text::Action::Drag {
423 x: position.x as i32,
424 y: position.y as i32,
425 },
426 );
427
428 if let Some((start, end)) = editor.selection_bounds() {
430 if start.line == end.line && start.index == end.index {
431 editor.set_selection(cosmic_text::Selection::None);
432 }
433 }
434 }
435 Action::Scroll { lines } => {
436 editor.action(
437 font_system.raw(),
438 cosmic_text::Action::Scroll { lines },
439 );
440 }
441 }
442
443 self.0 = Some(Arc::new(internal));
444 }
445
446 fn bounds(&self) -> Size {
447 self.internal().bounds
448 }
449
450 fn min_bounds(&self) -> Size {
451 let internal = self.internal();
452
453 let (bounds, _has_rtl) =
454 text::measure(buffer_from_editor(&internal.editor));
455
456 bounds
457 }
458
459 fn update(
460 &mut self,
461 new_bounds: Size,
462 new_font: Font,
463 new_size: Pixels,
464 new_line_height: LineHeight,
465 new_wrapping: Wrapping,
466 new_highlighter: &mut impl Highlighter,
467 ) {
468 let editor =
469 self.0.take().expect("Editor should always be initialized");
470
471 let mut internal = Arc::try_unwrap(editor)
472 .expect("Editor cannot have multiple strong references");
473
474 let mut font_system =
475 text::font_system().write().expect("Write font system");
476
477 let buffer = buffer_mut_from_editor(&mut internal.editor);
478
479 if font_system.version() != internal.version {
480 log::trace!("Updating `FontSystem` of `Editor`...");
481
482 for line in buffer.lines.iter_mut() {
483 line.reset();
484 }
485
486 internal.version = font_system.version();
487 internal.topmost_line_changed = Some(0);
488 }
489
490 if new_font != internal.font {
491 log::trace!("Updating font of `Editor`...");
492
493 for line in buffer.lines.iter_mut() {
494 let _ = line.set_attrs_list(cosmic_text::AttrsList::new(
495 text::to_attributes(new_font),
496 ));
497 }
498
499 internal.font = new_font;
500 internal.topmost_line_changed = Some(0);
501 }
502
503 let metrics = buffer.metrics();
504 let new_line_height = new_line_height.to_absolute(new_size);
505
506 if new_size.0 != metrics.font_size
507 || new_line_height.0 != metrics.line_height
508 {
509 log::trace!("Updating `Metrics` of `Editor`...");
510
511 buffer.set_metrics(
512 font_system.raw(),
513 cosmic_text::Metrics::new(new_size.0, new_line_height.0),
514 );
515 }
516
517 let new_wrap = text::to_wrap(new_wrapping);
518
519 if new_wrap != buffer.wrap() {
520 log::trace!("Updating `Wrap` strategy of `Editor`...");
521
522 buffer.set_wrap(font_system.raw(), new_wrap);
523 }
524
525 if new_bounds != internal.bounds {
526 log::trace!("Updating size of `Editor`...");
527
528 buffer.set_size(
529 font_system.raw(),
530 Some(new_bounds.width),
531 Some(new_bounds.height),
532 );
533
534 internal.bounds = new_bounds;
535 }
536
537 if let Some(topmost_line_changed) = internal.topmost_line_changed.take()
538 {
539 log::trace!(
540 "Notifying highlighter of line change: {topmost_line_changed}"
541 );
542
543 new_highlighter.change_line(topmost_line_changed);
544 }
545
546 internal.editor.shape_as_needed(font_system.raw(), false);
547
548 let _ = internal
550 .cursor
551 .write()
552 .expect("Write to cursor cache")
553 .take();
554
555 self.0 = Some(Arc::new(internal));
556 }
557
558 fn highlight<H: Highlighter>(
559 &mut self,
560 font: Self::Font,
561 highlighter: &mut H,
562 format_highlight: impl Fn(&H::Highlight) -> highlighter::Format<Self::Font>,
563 ) {
564 let internal = self.internal();
565 let buffer = buffer_from_editor(&internal.editor);
566
567 let scroll = buffer.scroll();
568 let mut window = (internal.bounds.height / buffer.metrics().line_height)
569 .ceil() as i32;
570
571 let last_visible_line = buffer.lines[scroll.line..]
572 .iter()
573 .enumerate()
574 .find_map(|(i, line)| {
575 let visible_lines = line
576 .layout_opt()
577 .as_ref()
578 .expect("Line layout should be cached")
579 .len() as i32;
580
581 if window > visible_lines {
582 window -= visible_lines;
583 None
584 } else {
585 Some(scroll.line + i)
586 }
587 })
588 .unwrap_or(buffer.lines.len().saturating_sub(1));
589
590 let current_line = highlighter.current_line();
591
592 if current_line > last_visible_line {
593 return;
594 }
595
596 let editor =
597 self.0.take().expect("Editor should always be initialized");
598
599 let mut internal = Arc::try_unwrap(editor)
600 .expect("Editor cannot have multiple strong references");
601
602 let mut font_system =
603 text::font_system().write().expect("Write font system");
604
605 let attributes = text::to_attributes(font);
606
607 for line in &mut buffer_mut_from_editor(&mut internal.editor).lines
608 [current_line..=last_visible_line]
609 {
610 let mut list = cosmic_text::AttrsList::new(attributes);
611
612 for (range, highlight) in highlighter.highlight_line(line.text()) {
613 let format = format_highlight(&highlight);
614
615 if format.color.is_some() || format.font.is_some() {
616 list.add_span(
617 range,
618 cosmic_text::Attrs {
619 color_opt: format.color.map(text::to_color),
620 ..if let Some(font) = format.font {
621 text::to_attributes(font)
622 } else {
623 attributes
624 }
625 },
626 );
627 }
628 }
629
630 let _ = line.set_attrs_list(list);
631 }
632
633 internal.editor.shape_as_needed(font_system.raw(), false);
634
635 self.0 = Some(Arc::new(internal));
636 }
637}
638
639impl Default for Editor {
640 fn default() -> Self {
641 Self(Some(Arc::new(Internal::default())))
642 }
643}
644
645impl PartialEq for Internal {
646 fn eq(&self, other: &Self) -> bool {
647 self.font == other.font
648 && self.bounds == other.bounds
649 && buffer_from_editor(&self.editor).metrics()
650 == buffer_from_editor(&other.editor).metrics()
651 }
652}
653
654impl Default for Internal {
655 fn default() -> Self {
656 Self {
657 editor: cosmic_text::Editor::new(cosmic_text::Buffer::new_empty(
658 cosmic_text::Metrics {
659 font_size: 1.0,
660 line_height: 1.0,
661 },
662 )),
663 cursor: RwLock::new(None),
664 font: Font::default(),
665 bounds: Size::ZERO,
666 topmost_line_changed: None,
667 version: text::Version::default(),
668 }
669 }
670}
671
672impl fmt::Debug for Internal {
673 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
674 f.debug_struct("Internal")
675 .field("font", &self.font)
676 .field("bounds", &self.bounds)
677 .finish()
678 }
679}
680
681#[derive(Debug, Clone)]
683pub struct Weak {
684 raw: sync::Weak<Internal>,
685 pub bounds: Size,
687}
688
689impl Weak {
690 pub fn upgrade(&self) -> Option<Editor> {
692 self.raw.upgrade().map(Some).map(Editor)
693 }
694}
695
696impl PartialEq for Weak {
697 fn eq(&self, other: &Self) -> bool {
698 match (self.raw.upgrade(), other.raw.upgrade()) {
699 (Some(p1), Some(p2)) => p1 == p2,
700 _ => false,
701 }
702 }
703}
704
705fn highlight_line(
706 line: &cosmic_text::BufferLine,
707 from: usize,
708 to: usize,
709) -> impl Iterator<Item = (f32, f32)> + '_ {
710 let layout = line.layout_opt().map(Vec::as_slice).unwrap_or_default();
711
712 layout.iter().map(move |visual_line| {
713 let start = visual_line
714 .glyphs
715 .first()
716 .map(|glyph| glyph.start)
717 .unwrap_or(0);
718 let end = visual_line
719 .glyphs
720 .last()
721 .map(|glyph| glyph.end)
722 .unwrap_or(0);
723
724 let range = start.max(from)..end.min(to);
725
726 if range.is_empty() {
727 (0.0, 0.0)
728 } else if range.start == start && range.end == end {
729 (0.0, visual_line.w)
730 } else {
731 let first_glyph = visual_line
732 .glyphs
733 .iter()
734 .position(|glyph| range.start <= glyph.start)
735 .unwrap_or(0);
736
737 let mut glyphs = visual_line.glyphs.iter();
738
739 let x =
740 glyphs.by_ref().take(first_glyph).map(|glyph| glyph.w).sum();
741
742 let width: f32 = glyphs
743 .take_while(|glyph| range.end > glyph.start)
744 .map(|glyph| glyph.w)
745 .sum();
746
747 (x, width)
748 }
749 })
750}
751
752fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 {
753 let scroll = buffer.scroll();
754
755 let start = scroll.line.min(line);
756 let end = scroll.line.max(line);
757
758 let visual_lines_offset: usize = buffer.lines[start..]
759 .iter()
760 .take(end - start)
761 .map(|line| line.layout_opt().map(Vec::len).unwrap_or_default())
762 .sum();
763
764 visual_lines_offset as i32 * if scroll.line < line { 1 } else { -1 }
765}
766
767fn to_motion(motion: Motion) -> cosmic_text::Motion {
768 match motion {
769 Motion::Left => cosmic_text::Motion::Left,
770 Motion::Right => cosmic_text::Motion::Right,
771 Motion::Up => cosmic_text::Motion::Up,
772 Motion::Down => cosmic_text::Motion::Down,
773 Motion::WordLeft => cosmic_text::Motion::LeftWord,
774 Motion::WordRight => cosmic_text::Motion::RightWord,
775 Motion::Home => cosmic_text::Motion::Home,
776 Motion::End => cosmic_text::Motion::End,
777 Motion::PageUp => cosmic_text::Motion::PageUp,
778 Motion::PageDown => cosmic_text::Motion::PageDown,
779 Motion::DocumentStart => cosmic_text::Motion::BufferStart,
780 Motion::DocumentEnd => cosmic_text::Motion::BufferEnd,
781 }
782}
783
784fn buffer_from_editor<'a, 'b>(
785 editor: &'a impl cosmic_text::Edit<'b>,
786) -> &'a cosmic_text::Buffer
787where
788 'b: 'a,
789{
790 match editor.buffer_ref() {
791 cosmic_text::BufferRef::Owned(buffer) => buffer,
792 cosmic_text::BufferRef::Borrowed(buffer) => buffer,
793 cosmic_text::BufferRef::Arc(buffer) => buffer,
794 }
795}
796
797fn buffer_mut_from_editor<'a, 'b>(
798 editor: &'a mut impl cosmic_text::Edit<'b>,
799) -> &'a mut cosmic_text::Buffer
800where
801 'b: 'a,
802{
803 match editor.buffer_ref_mut() {
804 cosmic_text::BufferRef::Owned(buffer) => buffer,
805 cosmic_text::BufferRef::Borrowed(buffer) => buffer,
806 cosmic_text::BufferRef::Arc(_buffer) => unreachable!(),
807 }
808}