duat_core/text/mod.rs
1//! The primary data structure in Duat
2//!
3//! This struct is responsible for all of the text that will be
4//! printed to the screen, as well as any modifications on it.
5//!
6//! The [`Text`] is a very versatile holder for characters, below is a
7//! list of some of its capabilities:
8//!
9//! - Be cheaply* edited at any point, due to its two [gap buffers];
10//! - Be [colored] in any way, at any point;
11//! - Have any arbitrary range concealed, that is, hidden from view,
12//! but still in there;
13//! - Arbitrary [ghost text], that is, [`Text`] that shows up, but is
14//! not actually part of the [`Text`], i.e., it can be easily
15//! ignored by external modifiers (like an LSP or tree-sitter) of
16//! the file, without any special checks;
17//! - [Left]/[right]/[center] alignment of output (although that is
18//! implemented by the [`Ui`]);
19//! - [Spacers] for even more advanced alignment
20//! - The ability to [undo]/[redo] changes in the history;
21//! - In the future, button ranges that can interact with the mouse;
22//!
23//! The [`Text`] struct is created in two different ways:
24//!
25//! - By calling [`Text::new`] or one of its [`From`] implementations;
26//! - By building it with the [`txt!`] macro;
27//!
28//! The first method is recommended if you want a [`Text`] that will
29//! be modified by input. This is often the case if your [`Widget`] is
30//! some sort of text box, chief of which is the [`File`], which is
31//! the central [`Widget`] of every text editor.
32//!
33//! The second method is what should be used most of the time, as it
34//! lets you quickly create formatted [`Widget`]s/[`StatusLine`] parts
35//! in a very modular way:
36//!
37//! ```rust
38//! # use duat_core::prelude::*;
39//! fn number_of_horses(count: usize) -> Text {
40//! if count == 1 {
41//! txt!("[horses.count]1[horses] horse").build()
42//! } else {
43//! txt!("[horses.count]{}[horses] horses", count).build()
44//! }
45//! }
46//!
47//! fn inlined_number_of_horses(count: usize) -> Text {
48//! txt!(
49//! "[horses.count]{count} [horses]{}",
50//! if count == 1 { "horse" } else { "horses" }
51//! )
52//! .build()
53//! }
54//! ```
55//!
56//! You can use this whenever you need to update a widget, for
57//! example, just create a new [`Text`] to printed to the screen.
58//!
59//! However, when recreating the entire [`Text`] with a [`txt!`]
60//! macro would be too expensive, you can use [`Text`] modifying
61//! functions:
62//!
63//! ```rust
64//! # use duat_core::prelude::*;
65//! let mut prompted = txt!("[prompt]type a key: ").build();
66//! let end = prompted.len();
67//! prompted.replace_range(end..end, "a")
68//! ```
69//!
70//! A general rule of thumb for "too expensive" is this: if your
71//! [`Text`] can't scroll more than a few lines, it is not too
72//! expensive to rebuild. This way of editing the [`Text`] is mostly
73//! used on the [`File`] widget and other textbox-like [`Widget`]s.
74//!
75//! [Left]: AlignLeft
76//! [right]: AlignRight
77//! [center]: AlignCenter
78//! [Spacers]: Spacer
79//! [undo]: Text::undo
80//! [redo]: Text::redo
81//! [gap buffers]: gapbuf::GapBuffer
82//! [colored]: crate::form::Form
83//! [ghost text]: Ghost
84//! [`Ui`]: crate::ui::Ui
85//! [`File`]: crate::file::File
86//! [`Widget`]: crate::ui::Widget
87//! [`StatusLine`]: https://docs.rs/duat-utils/latest/duat_utils/widgets/struct.StatusLine.html
88//! [`Mode`]: crate::mode::Mode
89mod builder;
90mod bytes;
91mod history;
92mod iter;
93mod ops;
94mod records;
95mod search;
96mod tags;
97
98use std::{
99 path::Path,
100 rc::Rc,
101 sync::{
102 Arc,
103 atomic::{AtomicBool, Ordering},
104 },
105};
106
107pub(crate) use self::history::History;
108use self::tags::{FwdTags, RevTags, Tags};
109pub use self::{
110 builder::{Builder, BuilderPart, txt},
111 bytes::{Buffers, Bytes, Lines, Strs},
112 history::{Change, Moment},
113 iter::{FwdIter, Item, Part, RevIter},
114 ops::{Point, TextRange, TextRangeOrPoint, TwoPoints, utf8_char_width},
115 search::{Matcheable, RegexPattern, Searcher},
116 tags::{
117 AlignCenter, AlignLeft, AlignRight, Conceal, ExtraCaret, FormTag, Ghost, GhostId,
118 MainCaret, MutTags, RawTag, Spacer, Tag, Tagger, Taggers, ToggleId,
119 },
120};
121use crate::{
122 cfg::PrintCfg,
123 context, form,
124 mode::{Selection, Selections},
125 ui::RawArea,
126};
127
128/// The text of a given [`Widget`]
129///
130/// The [`Text`] is the backbone of Duat. It is the thing responsible
131/// for everything that shows up on screen.
132///
133/// You can build a [`Text`] manually, by using [`Text::new`], or with
134/// some convenience, by using the [`txt!`] macro, making use of a
135/// [`Builder`].
136///
137/// [`Widget`]: crate::ui::Widget
138pub struct Text(Box<InnerText>);
139
140struct InnerText {
141 bytes: Bytes,
142 tags: Tags,
143 selections: Option<Selections>,
144 // Specific to Files
145 history: Option<History>,
146 has_changed: bool,
147 has_unsaved_changes: AtomicBool,
148}
149
150impl Text {
151 ////////// Creation and Destruction of Text
152
153 /// Returns a new empty [`Text`]
154 pub fn new() -> Self {
155 Self::from_bytes(Bytes::default(), None, false)
156 }
157
158 /// Returns a new empty [`Text`] with [`Selections`] enabled
159 pub fn new_with_selections() -> Self {
160 Self::from_bytes(Bytes::default(), Some(Selections::default()), false)
161 }
162
163 /// Returns a new empty [`Text`] with history enabled
164 pub(crate) fn new_with_history() -> Self {
165 Self::from_bytes(Bytes::default(), Some(Selections::default()), true)
166 }
167
168 /// Creates a [`Text`] from a file's [path]
169 ///
170 /// [path]: Path
171 pub(crate) fn from_file(
172 bytes: Bytes,
173 selections: Selections,
174 path: impl AsRef<Path>,
175 has_unsaved_changes: bool,
176 ) -> Self {
177 let selections = if let Some(selection) = selections.get_main()
178 && let Some(_) = bytes.char_at(selection.caret())
179 {
180 selections
181 } else {
182 Selections::default()
183 };
184
185 let mut text = Self::from_bytes(bytes, Some(selections), true);
186 text.0
187 .has_unsaved_changes
188 .store(has_unsaved_changes, Ordering::Relaxed);
189
190 if let Some(history) = context::load_cache(path.as_ref()) {
191 text.0.history = Some(history);
192 }
193
194 text
195 }
196
197 /// Creates a [`Text`] from [`Bytes`]
198 pub(crate) fn from_bytes(
199 mut bytes: Bytes,
200 selections: Option<Selections>,
201 with_history: bool,
202 ) -> Self {
203 if bytes.buffers(..).next_back().is_none_or(|b| b != b'\n') {
204 let end = bytes.len();
205 bytes.apply_change(Change::str_insert("\n", end));
206 }
207 let tags = Tags::new(bytes.len().byte());
208
209 Self(Box::new(InnerText {
210 bytes,
211 tags,
212 selections,
213 history: with_history.then(History::new),
214 has_changed: false,
215 has_unsaved_changes: AtomicBool::new(false),
216 }))
217 }
218
219 /// Returns an empty [`Text`], only for [`Builder`]s
220 fn empty() -> Self {
221 Self(Box::new(InnerText {
222 bytes: Bytes::default(),
223 tags: Tags::new(0),
224 selections: None,
225 history: None,
226 has_changed: false,
227 has_unsaved_changes: AtomicBool::new(false),
228 }))
229 }
230
231 /// Returns a [`Builder`] for [`Text`]
232 ///
233 /// This builder can be used to iteratively create text, by
234 /// assuming that the user wants no* [`Tag`] overlap, and that
235 /// they want to construct the [`Text`] in [`Tag`]/content pairs.
236 ///
237 /// ```rust
238 /// use duat_core::prelude::*;
239 /// let mut builder = Text::builder();
240 /// ```
241 pub fn builder() -> Builder {
242 Builder::new()
243 }
244
245 /// Takes the [`Bytes`] from this [`Text`], consuming it
246 pub(crate) fn take_bytes(self) -> Bytes {
247 self.0.bytes
248 }
249
250 ////////// Querying functions
251
252 /// The [`Point`] at the end of the text
253 pub fn len(&self) -> Point {
254 self.0.bytes.len()
255 }
256
257 /// Whether or not there are any characters in the [`Text`]
258 ///
259 /// This ignores the last `'\n'` in the [`Text`], since it is
260 /// always there no matter what.
261 ///
262 /// This does not check for tags, so with a [`Ghost`],
263 /// there could actually be a "string" of characters on the
264 /// [`Text`], it just wouldn't be considered real "text". If you
265 /// want to make sure it is _indeed_ empty, see
266 /// [`is_empty_empty`].
267 ///
268 /// [`is_empty_empty`]: Self::is_empty_empty
269 pub fn is_empty(&self) -> bool {
270 self.0.bytes == "\n"
271 }
272
273 /// Whether the [`Bytes`] and `Tags` are empty
274 ///
275 /// This ignores the last `'\n'` in the [`Text`], since it is
276 /// always there no matter what.
277 ///
278 /// If you only want to check for the [`Bytes`], ignoring possible
279 /// [`Ghost`]s, see [`is_empty`].
280 ///
281 /// [`is_empty`]: Self::is_empty
282 pub fn is_empty_empty(&self) -> bool {
283 self.0.bytes == "\n" && self.0.tags.is_empty()
284 }
285
286 /// The `char` at the [`Point`]'s position
287 pub fn char_at(&self, point: Point) -> Option<char> {
288 self.0.bytes.char_at(point)
289 }
290
291 /// An [`Iterator`] over the bytes of the [`Text`]
292 pub fn buffers(&self, range: impl TextRange) -> Buffers<'_> {
293 self.0.bytes.buffers(range)
294 }
295
296 /// An [`Iterator`] over the [`&str`]s of the [`Text`]
297 ///
298 /// # Note
299 ///
300 /// The reason why this function returns two strings is that the
301 /// contents of the text are stored in a [`GapBuffer`], which
302 /// works with two strings.
303 ///
304 /// If you want to iterate over them, you can do the following:
305 ///
306 /// ```rust
307 /// # use duat_core::{text::Point, prelude::*};
308 /// # let (p1, p2) = (Point::default(), Point::default());
309 /// let text = Text::new();
310 /// text.strs(p1..p2).flat_map(str::chars);
311 /// ```
312 ///
313 /// Do note that you should avoid iterators like [`str::lines`],
314 /// as they will separate the line that is partially owned by each
315 /// [`&str`]:
316 ///
317 /// ```rust
318 /// let broken_up_line = [
319 /// "This is line 1, business as usual.\nThis is line 2, but it",
320 /// "is broken into two separate strings.\nSo 4 lines would be counted, instead of 3",
321 /// ];
322 /// ```
323 ///
324 /// # [`TextRange`] behavior:
325 ///
326 /// If you give a single [`usize`]/[`Point`], it will be
327 /// interpreted as a range from.
328 ///
329 /// [`&str`]: str
330 /// [`GapBuffer`]: gapbuf::GapBuffer
331 pub fn strs(&self, range: impl TextRange) -> Strs<'_> {
332 self.0.bytes.strs(range)
333 }
334
335 /// Returns an iterator over the lines in a given range
336 ///
337 /// The lines are inclusive, that is, it will iterate over the
338 /// whole line, not just the parts within the range.
339 pub fn lines(&self, range: impl TextRange) -> Lines<'_> {
340 self.0.bytes.lines(range)
341 }
342
343 /// The inner bytes of the [`Text`]
344 pub fn bytes(&self) -> &Bytes {
345 &self.0.bytes
346 }
347
348 /// The inner bytes of the [`Text`], mutably
349 ///
350 /// Do note that this mutability isn't actually for modifying the
351 /// [`Bytes`] themselves, but instead it is used by some methods
352 /// to read said bytes, like [`make_contiguous`] or [`lines`]
353 ///
354 /// [`make_contiguous`]: Bytes::make_contiguous
355 /// [`lines`]: Bytes::lines
356 pub fn bytes_mut(&mut self) -> &mut Bytes {
357 &mut self.0.bytes
358 }
359
360 /// The [`&mut Bytes`] and [`MutTags`]
361 ///
362 /// [`&mut Bytes`]: Bytes
363 pub fn bytes_and_tags(&mut self) -> (&mut Bytes, MutTags<'_>) {
364 (&mut self.0.bytes, MutTags(&mut self.0.tags))
365 }
366
367 /// Gets the indentation level on the current line
368 pub fn indent(&self, p: Point, area: &impl RawArea, cfg: PrintCfg) -> usize {
369 let [start, _] = self.points_of_line(p.line());
370 let t_iter = self.iter_fwd(start).no_ghosts().no_conceals();
371 area.print_iter(t_iter, cfg.new_line_as('\n'))
372 .filter_map(|(caret, item)| Some(caret).zip(item.part.as_char()))
373 .find(|(_, char)| !char.is_whitespace() || *char == '\n')
374 .map(|(caret, _)| caret.x as usize)
375 .unwrap_or(0)
376 }
377
378 ////////// Point querying functions
379
380 /// The [`Point`] corresponding to the byte position, 0 indexed
381 ///
382 /// If the byte position would fall in between two characters
383 /// (because the first one comprises more than one byte), the
384 /// first character is chosen as the [`Point`] where the byte is
385 /// located.
386 ///
387 /// # Panics
388 ///
389 /// Will panic if `b` is greater than the length of the text
390 #[inline(always)]
391 pub fn point_at(&self, b: usize) -> Point {
392 self.0.bytes.point_at(b)
393 }
394
395 /// The [`Point`] associated with a char position, 0 indexed
396 ///
397 /// # Panics
398 ///
399 /// Will panic if `c` is greater than the number of chars in the
400 /// text.
401 #[inline(always)]
402 pub fn point_at_char(&self, c: usize) -> Point {
403 self.0.bytes.point_at_char(c)
404 }
405
406 /// The [`Point`] where the `l`th line starts, 0 indexed
407 ///
408 /// If `l == number_of_lines`, returns the last point of the
409 /// text.
410 ///
411 /// # Panics
412 ///
413 /// Will panic if `l` is greater than the number of lines on the
414 /// text
415 #[inline(always)]
416 pub fn point_at_line(&self, l: usize) -> Point {
417 self.0.bytes.point_at_line(l)
418 }
419
420 /// The start and end [`Point`]s for a given `l` line
421 ///
422 /// If `l == number_of_lines`, these points will be the same.
423 ///
424 /// # Panics
425 ///
426 /// Will panic if the number `l` is greater than the number of
427 /// lines on the text
428 #[inline(always)]
429 pub fn points_of_line(&self, l: usize) -> [Point; 2] {
430 self.0.bytes.points_of_line(l)
431 }
432
433 /// The [points] at the end of the text
434 ///
435 /// This will essentially return the [last point] of the text,
436 /// alongside the last possible [`Point`] of any
437 /// [`Ghost`] at the end of the text.
438 ///
439 /// [points]: TwoPoints
440 /// [last point]: Self::len
441 pub fn len_points(&self) -> (Point, Option<Point>) {
442 self.ghost_max_points_at(self.len().byte())
443 }
444
445 /// The last [`Point`] associated with a `char`
446 ///
447 /// This will give the [`Point`] of the last `char` of the text.
448 /// The difference between this method and [`len`] is that
449 /// it will return a [`Point`] one position earlier than it. If
450 /// the text is completely empty, it will return [`None`].
451 ///
452 /// [`len`]: Self::len
453 pub fn last_point(&self) -> Option<Point> {
454 self.0.bytes.last_point()
455 }
456
457 ////////// Tag related query functions
458
459 /// The maximum [points] in the `at`th byte
460 ///
461 /// This point is essentially the [point] at that byte, plus the
462 /// last possible [`Point`] of any [`Ghost`]s in that
463 /// position.
464 ///
465 /// [points]: TwoPoints
466 /// [point]: Self::point_at
467 #[inline(always)]
468 pub fn ghost_max_points_at(&self, at: usize) -> (Point, Option<Point>) {
469 let point = self.point_at(at);
470 (point, self.0.tags.ghosts_total_at(point.byte()))
471 }
472
473 /// Points visually after the [`TwoPoints`]
474 ///
475 /// If the [`TwoPoints`] in question is concealed, treats the
476 /// next visible character as the first character, and returns
477 /// the points of the next visible character.
478 ///
479 /// This method is useful if you want to iterator reversibly
480 /// right after a certain point, thus including the character
481 /// of said point.
482 pub fn points_after(&self, tp: impl TwoPoints) -> Option<(Point, Option<Point>)> {
483 self.iter_fwd(tp)
484 .filter_map(|item| item.part.as_char().map(|_| item.points()))
485 .chain([self.len_points()])
486 .nth(1)
487 }
488
489 /// The visual start of the line
490 ///
491 /// This point is defined not by where the line actually begins,
492 /// but by where the last '\n' was located. For example, if
493 /// [`Tag`]s create ghost text or omit text from multiple
494 /// different lines, this point may differ from where in the
495 /// [`Text`] the physical line actually begins.
496 pub fn visual_line_start(&self, p: impl TwoPoints) -> (Point, Option<Point>) {
497 let (real, ghost) = p.to_points();
498
499 let mut iter = self.iter_rev((real, ghost)).peekable();
500 let mut points = (real, ghost);
501 while let Some(peek) = iter.peek() {
502 match peek.part {
503 Part::Char('\n') => {
504 return points;
505 }
506 Part::Char(_) => points = iter.next().unwrap().to_points(),
507 _ => drop(iter.next()),
508 }
509 }
510
511 points
512 }
513
514 /// Gets the [`Ghost`] of a given [`GhostId`]
515 pub fn get_ghost(&self, id: GhostId) -> Option<&Text> {
516 self.0.tags.get_ghost(id)
517 }
518
519 ////////// Modification functions
520
521 /// Replaces a [range] in the [`Text`]
522 ///
523 /// # [`TextRange`] behavior:
524 ///
525 /// If you give a single [`usize`]/[`Point`], it will be
526 /// interpreted as a range from.
527 ///
528 /// [range]: TextRange
529 pub fn replace_range(&mut self, range: impl TextRange, edit: impl ToString) {
530 let range = range.to_range(self.len().byte());
531 let (start, end) = (self.point_at(range.start), self.point_at(range.end));
532 let change = Change::new(edit, [start, end], self);
533
534 self.0.has_changed = true;
535 self.apply_change_inner(0, change.as_ref());
536 self.0
537 .history
538 .as_mut()
539 .map(|h| h.apply_change(None, change));
540 }
541
542 pub(crate) fn apply_change(
543 &mut self,
544 guess_i: Option<usize>,
545 change: Change,
546 ) -> (Option<usize>, Option<usize>) {
547 self.0.has_changed = true;
548
549 let selections_taken = self.apply_change_inner(guess_i.unwrap_or(0), change.as_ref());
550 let history = self.0.history.as_mut();
551 let insertion_i = history.map(|h| h.apply_change(guess_i, change));
552 (insertion_i, selections_taken)
553 }
554
555 /// Merges `String`s with the body of text, given a range to
556 /// replace
557 fn apply_change_inner(&mut self, guess_i: usize, change: Change<&str>) -> Option<usize> {
558 self.0.bytes.apply_change(change);
559 self.0.tags.transform(
560 change.start().byte()..change.taken_end().byte(),
561 change.added_end().byte(),
562 );
563
564 *self.0.has_unsaved_changes.get_mut() = true;
565
566 self.0
567 .selections
568 .as_mut()
569 .map(|cs| cs.apply_change(guess_i, change))
570 }
571
572 fn without_last_nl(mut self) -> Self {
573 let change = Change::remove_last_nl(self.len());
574 self.apply_change_inner(0, change);
575 self
576 }
577
578 /// Updates bounds, so that [`Tag`] ranges can visibly cross the
579 /// screen
580 ///
581 /// This is used in order to allow for very long [`Tag`] ranges
582 /// (say, a [`Form`] being applied on the range `3..999`) to show
583 /// up properly without having to lookback a bazillion [`Tag`]s
584 /// which could be in the way.
585 ///
586 /// [`Form`]: crate::form::Form
587 pub fn update_bounds(&mut self) {
588 self.0.tags.update_bounds();
589 }
590
591 /// Inserts a [`Text`] into this [`Text`], in a specific [`Point`]
592 pub fn insert_text(&mut self, p: Point, mut text: Text) {
593 let insert = if p.byte() == 1 && self.0.bytes == "\n" {
594 let change = Change::new(text.0.bytes.contiguous(..), [Point::default(), p], self);
595 self.apply_change_inner(0, change.as_ref());
596 Point::default()
597 } else {
598 let change = Change::str_insert(text.0.bytes.contiguous(..), p);
599 self.apply_change_inner(0, change);
600 p
601 };
602
603 if insert == self.len() {
604 self.0.tags.extend(text.0.tags);
605 } else {
606 self.0.tags.insert_tags(insert, text.0.tags);
607 }
608 }
609
610 ////////// History functions
611
612 /// Undoes the last moment, if there was one
613 pub fn undo(&mut self) {
614 let mut history = self.0.history.take();
615
616 if let Some(history) = history.as_mut()
617 && let Some(moment) = history.move_backwards()
618 {
619 self.apply_and_process_changes(moment);
620 self.0.has_changed = true;
621 }
622
623 self.0.history = history;
624 }
625
626 /// Redoes the last moment in the history, if there is one
627 pub fn redo(&mut self) {
628 let mut history = self.0.history.take();
629
630 if let Some(history) = history.as_mut()
631 && let Some(moment) = history.move_forward()
632 {
633 self.apply_and_process_changes(moment);
634 self.0.has_changed = true;
635 }
636
637 self.0.history = history;
638 }
639
640 /// Finishes the current moment and adds a new one to the history
641 pub fn new_moment(&mut self) {
642 if let Some(h) = self.0.history.as_mut() {
643 h.new_moment()
644 }
645 }
646
647 /// Returns a [`Moment`] containing all [`Change`]s since the last
648 /// call to this function
649 ///
650 /// This is useful if you want to figure out what has changed
651 /// after a certain period of time has passed.
652 pub fn last_unprocessed_moment(&mut self) -> Option<Vec<Moment>> {
653 self.0.history.as_mut().map(|h| h.unprocessed_moments())
654 }
655
656 fn apply_and_process_changes(&mut self, moment: Moment) {
657 if let Some(selections) = self.selections_mut() {
658 selections.clear();
659 }
660
661 for (i, change) in moment.changes().enumerate() {
662 self.apply_change_inner(0, change);
663
664 if let Some(selections) = self.0.selections.as_mut() {
665 let start = change.start();
666 let added_end = match change.added_str().chars().next_back() {
667 Some(last) => change.added_end().rev(last),
668 None => change.start(),
669 };
670
671 let selection = Selection::new(added_end, (start != added_end).then_some(start));
672 selections.insert(i, selection, i == moment.len() - 1);
673 }
674 }
675 }
676
677 ////////// Writing functions
678
679 /// Clones the inner [`Bytes`] as a [`String`]
680 ///
681 /// This function will also cut out a final '\n' from the string.
682 // NOTE: Inherent because I don't want this to implement Display
683 #[allow(clippy::inherent_to_string)]
684 pub fn to_string(&self) -> String {
685 let [s0, s1] = self.strs(..).to_array();
686 if !s1.is_empty() {
687 s0.to_string() + s1.strip_suffix('\n').unwrap_or(s1)
688 } else {
689 s0.strip_suffix('\n').unwrap_or(s0).to_string()
690 }
691 }
692
693 /// Writes the contents of this [`Text`] to a [writer]
694 ///
695 /// [writer]: std::io::Write
696 pub fn write_to(&self, mut writer: impl std::io::Write) -> std::io::Result<usize> {
697 self.0.has_unsaved_changes.store(false, Ordering::Relaxed);
698 let [s0, s1] = self.0.bytes.buffers(..).to_array();
699 Ok(writer.write(s0)? + writer.write(s1)?)
700 }
701
702 /// Wether or not the content has changed since the last [write]
703 ///
704 /// Returns `true` only if the actual bytes of the [`Text`] have
705 /// been changed, ignoring [`Tag`]s and all the other things,
706 /// since those are not written to the filesystem.
707 ///
708 /// [write]: Text::write_to
709 pub fn has_unsaved_changes(&self) -> bool {
710 self.0.has_unsaved_changes.load(Ordering::Relaxed)
711 }
712
713 ////////// Tag addition/deletion functions
714
715 /// Inserts a [`Tag`] at the given position
716 pub fn insert_tag<R>(&mut self, tagger: Tagger, r: R, tag: impl Tag<R>) {
717 self.0.tags.insert(tagger, r, tag);
718 }
719
720 /// Removes the [`Tag`]s of a [key] from a region
721 ///
722 /// # Caution
723 ///
724 /// While it is fine to do this on your own widgets, you should
725 /// refrain from using this function in a [`File`]s [`Text`], as
726 /// it must iterate over all tags in the file, so if there are a
727 /// lot of other tags, this operation may be slow.
728 ///
729 /// # [`TextRange`] behavior
730 ///
731 /// If you give it a [`Point`] or [`usize`], it will be treated as
732 /// a one byte range.
733 ///
734 /// [key]: Taggers
735 /// [`File`]: crate::file::File
736 pub fn remove_tags(&mut self, keys: impl Taggers, range: impl TextRangeOrPoint) {
737 let range = range.to_range(self.len().byte());
738 self.0.tags.remove_from(range, keys)
739 }
740
741 /// Removes all [`Tag`]s
742 ///
743 /// Refrain from using this function on [`File`]s, as there may be
744 /// other [`Tag`] providers, and you should avoid messing with
745 /// their tags.
746 ///
747 /// [`File`]: crate::file::File
748 pub fn clear_tags(&mut self) {
749 self.0.tags = Tags::new(self.0.bytes.len().byte());
750 }
751
752 /////////// Selection functions
753
754 /// Enables the usage of [`Selections`] in this [`Text`]
755 ///
756 /// This is automatically done whenever you use the cursor editing
757 /// functions of a [`Handle`].
758 ///
759 /// [`Handle`]: crate::context::Handle
760 pub fn enable_selections(&mut self) {
761 if self.0.selections.is_none() {
762 self.0.selections = Some(Selections::default())
763 }
764 }
765
766 /// Returns a [`Text`] without [`Selections`]
767 ///
768 /// You should use this if you want to send the [`Text`] across
769 /// threads.
770 pub fn no_selections(mut self) -> Selectionless {
771 self.0.selections = None;
772 Selectionless(self)
773 }
774
775 /// Removes the tags for all the selections, used before they are
776 /// expected to move
777 pub(crate) fn add_selections(&mut self, area: &impl RawArea, cfg: PrintCfg) {
778 let Some(selections) = self.0.selections.take() else {
779 return;
780 };
781
782 if selections.len() < 500 {
783 for (selection, is_main) in selections.iter() {
784 self.add_selection(selection, is_main);
785 }
786 } else {
787 let (start, _) = area.start_points(self, cfg);
788 let (end, _) = area.end_points(self, cfg);
789 for (selection, is_main) in selections.iter() {
790 let range = selection.range(self);
791 if range.end > start.byte() && range.start < end.byte() {
792 self.add_selection(selection, is_main);
793 }
794 }
795 }
796
797 self.0.selections = Some(selections);
798 }
799
800 /// Adds the tags for all the selections, used after they are
801 /// expected to have moved
802 pub(crate) fn remove_selections(&mut self, area: &impl RawArea, cfg: PrintCfg) {
803 let Some(selections) = self.0.selections.take() else {
804 return;
805 };
806
807 if selections.len() < 500 {
808 for (selection, _) in selections.iter() {
809 self.remove_selection(selection);
810 }
811 } else {
812 let (start, _) = area.start_points(self, cfg);
813 let (end, _) = area.end_points(self, cfg);
814 for (selection, _) in selections.iter() {
815 let range = selection.range(self);
816 if range.end > start.byte() && range.start < end.byte() {
817 self.remove_selection(selection);
818 }
819 }
820 }
821
822 self.0.selections = Some(selections)
823 }
824
825 /// Adds a [`Selection`] to the [`Text`]
826 fn add_selection(&mut self, selection: &Selection, is_main: bool) {
827 let (caret, selection) = selection.tag_points(self);
828
829 let key = Tagger::for_selections();
830 let form = if is_main {
831 self.0.tags.insert(key, caret.byte(), MainCaret);
832 form::M_SEL_ID
833 } else {
834 self.0.tags.insert(key, caret.byte(), ExtraCaret);
835 form::E_SEL_ID
836 };
837
838 self.0
839 .bytes
840 .add_record([caret.byte(), caret.char(), caret.line()]);
841
842 if let Some([start, end]) = selection {
843 let range = start.byte()..end.byte();
844 self.0.tags.insert(key, range, form.to_tag(250));
845 }
846 }
847
848 /// Removes a [`Selection`] from the [`Text`]
849 fn remove_selection(&mut self, selection: &Selection) {
850 let (caret, selection) = selection.tag_points(self);
851 let points = [caret]
852 .into_iter()
853 .chain(selection.into_iter().flatten().find(|p| *p != caret));
854 for p in points {
855 self.remove_tags(Tagger::for_selections(), p.byte());
856 }
857 }
858
859 /////////// Iterator methods
860
861 /// A forward iterator of the [chars and tags] of the [`Text`]
862 ///
863 /// [chars and tags]: Part
864 pub fn iter_fwd(&self, at: impl TwoPoints) -> FwdIter<'_> {
865 FwdIter::new_at(self, at)
866 }
867
868 /// A reverse iterator of the [chars and tags] of the [`Text`]
869 ///
870 /// [chars and tags]: Part
871 pub fn iter_rev(&self, at: impl TwoPoints) -> RevIter<'_> {
872 RevIter::new_at(self, at)
873 }
874
875 /// A forward iterator of the [`char`]s of the [`Text`]
876 ///
877 /// Each [`char`] will be accompanied by a [`Point`], which is the
878 /// position where said character starts, e.g.
879 /// [`Point::default()`] for the first character
880 pub fn chars_fwd(&self, p: Point) -> impl Iterator<Item = (Point, char)> + '_ {
881 self.0.bytes.chars_fwd(p)
882 }
883
884 /// A reverse iterator of the [`char`]s of the [`Text`]
885 ///
886 /// Each [`char`] will be accompanied by a [`Point`], which is the
887 /// position where said character starts, e.g.
888 /// [`Point::default()`] for the first character
889 pub fn chars_rev(&self, p: Point) -> impl Iterator<Item = (Point, char)> + '_ {
890 self.0.bytes.chars_rev(p)
891 }
892
893 /// A forward iterator over the [`Tag`]s of the [`Text`]
894 ///
895 /// This iterator will consider some [`Tag`]s before `b`, since
896 /// their ranges may overlap with `b`
897 ///
898 /// # Note
899 ///
900 /// Duat works fine with [`Tag`]s in the middle of a codepoint,
901 /// but external utilizers may not, so keep that in mind.
902 pub fn tags_fwd(&self, b: usize) -> FwdTags<'_> {
903 self.0.tags.fwd_at(b)
904 }
905
906 /// An reverse iterator over the [`Tag`]s of the [`Text`]
907 ///
908 /// This iterator will consider some [`Tag`]s ahead of `b`, since
909 /// their ranges may overlap with `b`
910 ///
911 /// # Note
912 ///
913 /// Duat works fine with [`Tag`]s in the middle of a codepoint,
914 /// but external utilizers may not, so keep that in mind.
915 pub fn tags_rev(&self, b: usize) -> RevTags<'_> {
916 self.0.tags.rev_at(b)
917 }
918
919 /// A forward [`Iterator`] over the [`RawTag`]s
920 ///
921 /// This [`Iterator`] does not take into account [`Tag`] ranges
922 /// that intersect with the starting point, unlike
923 /// [`Text::tags_fwd`]
924 pub fn raw_tags_fwd(&self, b: usize) -> impl Iterator<Item = (usize, RawTag)> {
925 self.0.tags.raw_fwd_at(b)
926 }
927
928 /// A reverse [`Iterator`] over the [`RawTag`]s
929 ///
930 /// This [`Iterator`] does not take into account [`Tag`] ranges
931 /// that intersect with the starting point, unlike
932 /// [`Text::tags_rev`]
933 pub fn raw_tags_rev(&self, b: usize) -> impl Iterator<Item = (usize, RawTag)> {
934 self.0.tags.raw_rev_at(b)
935 }
936
937 /// The [`Selections`] printed to this [`Text`], if they exist
938 pub fn selections(&self) -> Option<&Selections> {
939 self.0.selections.as_ref()
940 }
941
942 /// A mut reference to this [`Text`]'s [`Selections`] if they
943 /// exist
944 pub fn selections_mut(&mut self) -> Option<&mut Selections> {
945 self.0.selections.as_mut()
946 }
947
948 pub(crate) fn history(&self) -> Option<&History> {
949 self.0.history.as_ref()
950 }
951
952 ////////// One str functions
953
954 /// Gets a single [`&str`] from a given [range]
955 ///
956 /// This is the equivalent of calling
957 /// [`Bytes::make_contiguous`] and [`Bytes::get_contiguous`].
958 /// While this takes less space in code, calling the other two
959 /// functions means that you won't be mutably borrowing the
960 /// [`Bytes`] anymore, so if that matters to you, you should do
961 /// that.
962 ///
963 /// [`&str`]: str
964 /// [range]: TextRange
965 pub fn contiguous(&mut self, range: impl TextRange) -> &str {
966 self.make_contiguous(range.clone());
967 self.get_contiguous(range).unwrap()
968 }
969
970 /// Moves the [`GapBuffer`]'s gap, so that the `range` is whole
971 ///
972 /// The return value is the value of the gap, if the second `&str`
973 /// is the contiguous one.
974 ///
975 /// [`GapBuffer`]: gapbuf::GapBuffer
976 pub fn make_contiguous(&mut self, range: impl TextRange) {
977 self.0.bytes.make_contiguous(range);
978 }
979
980 /// Assumes that the `range` given is contiguous in `self`
981 ///
982 /// You *MUST* call [`make_contiguous`] before using this
983 /// function. The sole purpose of this function is to not keep the
984 /// [`Bytes`] mutably borrowed.
985 ///
986 /// [`make_contiguous`]: Self::make_contiguous
987 pub fn get_contiguous(&self, range: impl TextRange) -> Option<&str> {
988 self.0.bytes.get_contiguous(range)
989 }
990}
991
992impl Default for Text {
993 fn default() -> Self {
994 Self::new()
995 }
996}
997
998impl std::fmt::Debug for Text {
999 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1000 f.debug_struct("Text")
1001 .field("bytes", &self.0.bytes)
1002 .field("tags", &self.0.tags)
1003 .finish_non_exhaustive()
1004 }
1005}
1006
1007impl Clone for Text {
1008 fn clone(&self) -> Self {
1009 Self(Box::new(InnerText {
1010 bytes: self.0.bytes.clone(),
1011 tags: self.0.tags.clone(),
1012 selections: self.0.selections.clone(),
1013 history: self.0.history.clone(),
1014 has_changed: self.0.has_changed,
1015 has_unsaved_changes: AtomicBool::new(false),
1016 }))
1017 }
1018}
1019
1020impl From<std::io::Error> for Text {
1021 fn from(value: std::io::Error) -> Self {
1022 txt!("{}", value.kind().to_string()).build()
1023 }
1024}
1025
1026impl From<Box<dyn std::error::Error>> for Text {
1027 fn from(value: Box<dyn std::error::Error>) -> Self {
1028 txt!("{}", value.to_string()).build()
1029 }
1030}
1031
1032impl From<&std::path::PathBuf> for Text {
1033 fn from(value: &std::path::PathBuf) -> Self {
1034 let value = value.to_str().unwrap_or("");
1035 Self::from(value)
1036 }
1037}
1038
1039impl PartialEq for Text {
1040 fn eq(&self, other: &Self) -> bool {
1041 self.0.bytes == other.0.bytes && self.0.tags == other.0.tags
1042 }
1043}
1044
1045impl PartialEq<&str> for Text {
1046 fn eq(&self, other: &&str) -> bool {
1047 self.0.bytes == *other
1048 }
1049}
1050
1051impl PartialEq<String> for Text {
1052 fn eq(&self, other: &String) -> bool {
1053 self.0.bytes == *other
1054 }
1055}
1056
1057impl_from_to_string!(u8);
1058impl_from_to_string!(u16);
1059impl_from_to_string!(u32);
1060impl_from_to_string!(u64);
1061impl_from_to_string!(u128);
1062impl_from_to_string!(usize);
1063impl_from_to_string!(i8);
1064impl_from_to_string!(i16);
1065impl_from_to_string!(i32);
1066impl_from_to_string!(i64);
1067impl_from_to_string!(i128);
1068impl_from_to_string!(isize);
1069impl_from_to_string!(f32);
1070impl_from_to_string!(f64);
1071impl_from_to_string!(char);
1072impl_from_to_string!(&str);
1073impl_from_to_string!(String);
1074impl_from_to_string!(Box<str>);
1075impl_from_to_string!(Rc<str>);
1076impl_from_to_string!(Arc<str>);
1077
1078/// Implements [`From<$t>`] for [`Text`]
1079macro impl_from_to_string($t:ty) {
1080 impl From<$t> for Text {
1081 fn from(value: $t) -> Self {
1082 let string = <$t as ToString>::to_string(&value);
1083 let bytes = Bytes::new(&string);
1084 Self::from_bytes(bytes, None, false)
1085 }
1086 }
1087}
1088
1089/// A [`Text`] that is guaranteed not to have [`Selections`] in it
1090///
1091/// Useful for sending across threads, especially when it comes to
1092/// [`Logs`].
1093///
1094/// [`Logs`]: crate::context::Logs
1095#[derive(Clone, Debug)]
1096pub struct Selectionless(Text);
1097
1098impl Selectionless {
1099 /// Gets the [`Text`] within, allowing for mutation again
1100 pub fn get(&self) -> Text {
1101 self.0.clone()
1102 }
1103}
1104
1105impl std::ops::Deref for Selectionless {
1106 type Target = Text;
1107
1108 fn deref(&self) -> &Self::Target {
1109 &self.0
1110 }
1111}
1112
1113impl From<Selectionless> for Text {
1114 fn from(value: Selectionless) -> Self {
1115 value.0
1116 }
1117}
1118
1119// SAFETY: This struct is defined by the lack of Selections, the only
1120// non Send/Sync part of a Text
1121unsafe impl Send for Selectionless {}
1122unsafe impl Sync for Selectionless {}