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::Indent => {
381 editor.action(
382 font_system.raw(),
383 cosmic_text::Action::Indent,
384 );
385 }
386 Edit::Unindent => {
387 editor.action(
388 font_system.raw(),
389 cosmic_text::Action::Unindent,
390 );
391 }
392 Edit::Enter => {
393 editor.action(
394 font_system.raw(),
395 cosmic_text::Action::Enter,
396 );
397 }
398 Edit::Backspace => {
399 editor.action(
400 font_system.raw(),
401 cosmic_text::Action::Backspace,
402 );
403 }
404 Edit::Delete => {
405 editor.action(
406 font_system.raw(),
407 cosmic_text::Action::Delete,
408 );
409 }
410 }
411
412 let cursor = editor.cursor();
413 let selection_start = editor
414 .selection_bounds()
415 .map(|(start, _)| start)
416 .unwrap_or(cursor);
417
418 internal.topmost_line_changed = Some(selection_start.line);
419 }
420
421 Action::Click(position) => {
423 editor.action(
424 font_system.raw(),
425 cosmic_text::Action::Click {
426 x: position.x as i32,
427 y: position.y as i32,
428 },
429 );
430 }
431 Action::Drag(position) => {
432 editor.action(
433 font_system.raw(),
434 cosmic_text::Action::Drag {
435 x: position.x as i32,
436 y: position.y as i32,
437 },
438 );
439
440 if let Some((start, end)) = editor.selection_bounds() {
442 if start.line == end.line && start.index == end.index {
443 editor.set_selection(cosmic_text::Selection::None);
444 }
445 }
446 }
447 Action::Scroll { lines } => {
448 editor.action(
449 font_system.raw(),
450 cosmic_text::Action::Scroll { lines },
451 );
452 }
453 }
454
455 self.0 = Some(Arc::new(internal));
456 }
457
458 fn bounds(&self) -> Size {
459 self.internal().bounds
460 }
461
462 fn min_bounds(&self) -> Size {
463 let internal = self.internal();
464
465 let (bounds, _has_rtl) =
466 text::measure(buffer_from_editor(&internal.editor));
467
468 bounds
469 }
470
471 fn update(
472 &mut self,
473 new_bounds: Size,
474 new_font: Font,
475 new_size: Pixels,
476 new_line_height: LineHeight,
477 new_wrapping: Wrapping,
478 new_highlighter: &mut impl Highlighter,
479 ) {
480 let editor =
481 self.0.take().expect("Editor should always be initialized");
482
483 let mut internal = Arc::try_unwrap(editor)
484 .expect("Editor cannot have multiple strong references");
485
486 let mut font_system =
487 text::font_system().write().expect("Write font system");
488
489 let buffer = buffer_mut_from_editor(&mut internal.editor);
490
491 if font_system.version() != internal.version {
492 log::trace!("Updating `FontSystem` of `Editor`...");
493
494 for line in buffer.lines.iter_mut() {
495 line.reset();
496 }
497
498 internal.version = font_system.version();
499 internal.topmost_line_changed = Some(0);
500 }
501
502 if new_font != internal.font {
503 log::trace!("Updating font of `Editor`...");
504
505 for line in buffer.lines.iter_mut() {
506 let _ = line.set_attrs_list(cosmic_text::AttrsList::new(
507 &text::to_attributes(new_font),
508 ));
509 }
510
511 internal.font = new_font;
512 internal.topmost_line_changed = Some(0);
513 }
514
515 let metrics = buffer.metrics();
516 let new_line_height = new_line_height.to_absolute(new_size);
517
518 if new_size.0 != metrics.font_size
519 || new_line_height.0 != metrics.line_height
520 {
521 log::trace!("Updating `Metrics` of `Editor`...");
522
523 buffer.set_metrics(
524 font_system.raw(),
525 cosmic_text::Metrics::new(new_size.0, new_line_height.0),
526 );
527 }
528
529 let new_wrap = text::to_wrap(new_wrapping);
530
531 if new_wrap != buffer.wrap() {
532 log::trace!("Updating `Wrap` strategy of `Editor`...");
533
534 buffer.set_wrap(font_system.raw(), new_wrap);
535 }
536
537 if new_bounds != internal.bounds {
538 log::trace!("Updating size of `Editor`...");
539
540 buffer.set_size(
541 font_system.raw(),
542 Some(new_bounds.width),
543 Some(new_bounds.height),
544 );
545
546 internal.bounds = new_bounds;
547 }
548
549 if let Some(topmost_line_changed) = internal.topmost_line_changed.take()
550 {
551 log::trace!(
552 "Notifying highlighter of line change: {topmost_line_changed}"
553 );
554
555 new_highlighter.change_line(topmost_line_changed);
556 }
557
558 internal.editor.shape_as_needed(font_system.raw(), false);
559
560 let _ = internal
562 .cursor
563 .write()
564 .expect("Write to cursor cache")
565 .take();
566
567 self.0 = Some(Arc::new(internal));
568 }
569
570 fn highlight<H: Highlighter>(
571 &mut self,
572 font: Self::Font,
573 highlighter: &mut H,
574 format_highlight: impl Fn(&H::Highlight) -> highlighter::Format<Self::Font>,
575 ) {
576 let internal = self.internal();
577 let buffer = buffer_from_editor(&internal.editor);
578
579 let scroll = buffer.scroll();
580 let mut window = (internal.bounds.height / buffer.metrics().line_height)
581 .ceil() as i32;
582
583 let last_visible_line = buffer.lines[scroll.line..]
584 .iter()
585 .enumerate()
586 .find_map(|(i, line)| {
587 let visible_lines = line
588 .layout_opt()
589 .as_ref()
590 .expect("Line layout should be cached")
591 .len() as i32;
592
593 if window > visible_lines {
594 window -= visible_lines;
595 None
596 } else {
597 Some(scroll.line + i)
598 }
599 })
600 .unwrap_or(buffer.lines.len().saturating_sub(1));
601
602 let current_line = highlighter.current_line();
603
604 if current_line > last_visible_line {
605 return;
606 }
607
608 let editor =
609 self.0.take().expect("Editor should always be initialized");
610
611 let mut internal = Arc::try_unwrap(editor)
612 .expect("Editor cannot have multiple strong references");
613
614 let mut font_system =
615 text::font_system().write().expect("Write font system");
616
617 let attributes = text::to_attributes(font);
618
619 for line in &mut buffer_mut_from_editor(&mut internal.editor).lines
620 [current_line..=last_visible_line]
621 {
622 let mut list = cosmic_text::AttrsList::new(&attributes);
623
624 for (range, highlight) in highlighter.highlight_line(line.text()) {
625 let format = format_highlight(&highlight);
626
627 if format.color.is_some() || format.font.is_some() {
628 list.add_span(
629 range,
630 &cosmic_text::Attrs {
631 color_opt: format.color.map(text::to_color),
632 ..if let Some(font) = format.font {
633 text::to_attributes(font)
634 } else {
635 attributes.clone()
636 }
637 },
638 );
639 }
640 }
641
642 let _ = line.set_attrs_list(list);
643 }
644
645 internal.editor.shape_as_needed(font_system.raw(), false);
646
647 self.0 = Some(Arc::new(internal));
648 }
649}
650
651impl Default for Editor {
652 fn default() -> Self {
653 Self(Some(Arc::new(Internal::default())))
654 }
655}
656
657impl PartialEq for Internal {
658 fn eq(&self, other: &Self) -> bool {
659 self.font == other.font
660 && self.bounds == other.bounds
661 && buffer_from_editor(&self.editor).metrics()
662 == buffer_from_editor(&other.editor).metrics()
663 }
664}
665
666impl Default for Internal {
667 fn default() -> Self {
668 Self {
669 editor: cosmic_text::Editor::new(cosmic_text::Buffer::new_empty(
670 cosmic_text::Metrics {
671 font_size: 1.0,
672 line_height: 1.0,
673 },
674 )),
675 cursor: RwLock::new(None),
676 font: Font::default(),
677 bounds: Size::ZERO,
678 topmost_line_changed: None,
679 version: text::Version::default(),
680 }
681 }
682}
683
684impl fmt::Debug for Internal {
685 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
686 f.debug_struct("Internal")
687 .field("font", &self.font)
688 .field("bounds", &self.bounds)
689 .finish()
690 }
691}
692
693#[derive(Debug, Clone)]
695pub struct Weak {
696 raw: sync::Weak<Internal>,
697 pub bounds: Size,
699}
700
701impl Weak {
702 pub fn upgrade(&self) -> Option<Editor> {
704 self.raw.upgrade().map(Some).map(Editor)
705 }
706}
707
708impl PartialEq for Weak {
709 fn eq(&self, other: &Self) -> bool {
710 match (self.raw.upgrade(), other.raw.upgrade()) {
711 (Some(p1), Some(p2)) => p1 == p2,
712 _ => false,
713 }
714 }
715}
716
717fn highlight_line(
718 line: &cosmic_text::BufferLine,
719 from: usize,
720 to: usize,
721) -> impl Iterator<Item = (f32, f32)> + '_ {
722 let layout = line.layout_opt().map(Vec::as_slice).unwrap_or_default();
723
724 layout.iter().map(move |visual_line| {
725 let start = visual_line
726 .glyphs
727 .first()
728 .map(|glyph| glyph.start)
729 .unwrap_or(0);
730 let end = visual_line
731 .glyphs
732 .last()
733 .map(|glyph| glyph.end)
734 .unwrap_or(0);
735
736 let range = start.max(from)..end.min(to);
737
738 if range.is_empty() {
739 (0.0, 0.0)
740 } else if range.start == start && range.end == end {
741 (0.0, visual_line.w)
742 } else {
743 let first_glyph = visual_line
744 .glyphs
745 .iter()
746 .position(|glyph| range.start <= glyph.start)
747 .unwrap_or(0);
748
749 let mut glyphs = visual_line.glyphs.iter();
750
751 let x =
752 glyphs.by_ref().take(first_glyph).map(|glyph| glyph.w).sum();
753
754 let width: f32 = glyphs
755 .take_while(|glyph| range.end > glyph.start)
756 .map(|glyph| glyph.w)
757 .sum();
758
759 (x, width)
760 }
761 })
762}
763
764fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 {
765 let scroll = buffer.scroll();
766
767 let start = scroll.line.min(line);
768 let end = scroll.line.max(line);
769
770 let visual_lines_offset: usize = buffer.lines[start..]
771 .iter()
772 .take(end - start)
773 .map(|line| line.layout_opt().map(Vec::len).unwrap_or_default())
774 .sum();
775
776 visual_lines_offset as i32 * if scroll.line < line { 1 } else { -1 }
777}
778
779fn to_motion(motion: Motion) -> cosmic_text::Motion {
780 match motion {
781 Motion::Left => cosmic_text::Motion::Left,
782 Motion::Right => cosmic_text::Motion::Right,
783 Motion::Up => cosmic_text::Motion::Up,
784 Motion::Down => cosmic_text::Motion::Down,
785 Motion::WordLeft => cosmic_text::Motion::LeftWord,
786 Motion::WordRight => cosmic_text::Motion::RightWord,
787 Motion::Home => cosmic_text::Motion::Home,
788 Motion::End => cosmic_text::Motion::End,
789 Motion::PageUp => cosmic_text::Motion::PageUp,
790 Motion::PageDown => cosmic_text::Motion::PageDown,
791 Motion::DocumentStart => cosmic_text::Motion::BufferStart,
792 Motion::DocumentEnd => cosmic_text::Motion::BufferEnd,
793 }
794}
795
796fn buffer_from_editor<'a, 'b>(
797 editor: &'a impl cosmic_text::Edit<'b>,
798) -> &'a cosmic_text::Buffer
799where
800 'b: 'a,
801{
802 match editor.buffer_ref() {
803 cosmic_text::BufferRef::Owned(buffer) => buffer,
804 cosmic_text::BufferRef::Borrowed(buffer) => buffer,
805 cosmic_text::BufferRef::Arc(buffer) => buffer,
806 }
807}
808
809fn buffer_mut_from_editor<'a, 'b>(
810 editor: &'a mut impl cosmic_text::Edit<'b>,
811) -> &'a mut cosmic_text::Buffer
812where
813 'b: 'a,
814{
815 match editor.buffer_ref_mut() {
816 cosmic_text::BufferRef::Owned(buffer) => buffer,
817 cosmic_text::BufferRef::Borrowed(buffer) => buffer,
818 cosmic_text::BufferRef::Arc(_buffer) => unreachable!(),
819 }
820}