text_editing/
editing.rs

1use super::TextLine;
2
3impl TextLine {
4    /// Adds a char at the current text cursor position.
5    ///
6    /// # Arguments
7    /// * `text_cursor` - A mutable reference to the current text cursor position.
8    /// * `c` - The char to add.
9    ///
10    /// # Examples
11    /// ```
12    /// use text_editing::TextLine;
13    ///
14    /// let mut line = TextLine::from_string("Hello, ".into());
15    /// let mut text_cursor = 7;
16    /// line.add_char(&mut text_cursor, 'w');
17    /// assert_eq!(line.as_str(), "Hello, w");
18    /// assert_eq!(text_cursor, 8);
19    /// ```
20    pub fn add_char(&mut self, text_cursor: &mut usize, c: char) {
21        self.insert(*text_cursor, c);
22        *text_cursor += 1;
23    }
24
25    /// Removes the next char at the current text cursor position, if it exists.
26    ///
27    /// # Arguments
28    /// * `text_cursor` - The current text cursor position.
29    ///
30    /// # Returns
31    /// `true` if a char was removed, `false` otherwise.
32    ///
33    /// # Examples
34    /// ```
35    /// use text_editing::TextLine;
36    ///
37    /// let mut line = TextLine::from_string("Hello, world!".into());
38    /// let text_cursor = 7;
39    /// assert!(line.remove_forward(text_cursor));
40    /// assert_eq!(line.as_str(), "Hello, orld!");
41    /// ```
42    pub fn remove_forward(&mut self, text_cursor: usize) -> bool {
43        let remove = text_cursor < self.len();
44        if remove {
45            self.remove(text_cursor);
46        }
47        remove
48    }
49
50    /// Removes the next word at the current text cursor position, if it exists.
51    ///
52    /// # Arguments
53    /// * `text_cursor` - The current text cursor position.
54    ///
55    /// # Returns
56    /// `true` if a word was removed, `false` otherwise.
57    ///
58    /// # Examples
59    /// ```
60    /// use text_editing::TextLine;
61    ///
62    /// let mut line = TextLine::from_string("Hello, world!".into());
63    /// let text_cursor = 7;
64    /// assert!(line.remove_forward_skip(text_cursor));
65    /// assert_eq!(line.as_str(), "Hello, !");
66    /// ```
67    pub fn remove_forward_skip(&mut self, text_cursor: usize) -> bool {
68        let mut end_cursor = text_cursor;
69        self.skip_forward(&mut end_cursor);
70        let range = text_cursor..end_cursor;
71        let moved = !range.is_empty();
72        if moved {
73            self.remove_range(range);
74        }
75        moved
76    }
77
78    /// Removes the previous char at the current text cursor position, if it exists, and updates the text cursor.
79    ///
80    /// # Arguments
81    /// * `text_cursor` - A mutable reference to the current text cursor position.
82    ///
83    /// # Returns
84    /// `true` if a char was removed, `false` otherwise.
85    ///
86    /// # Examples
87    /// ```
88    /// use text_editing::TextLine;
89    ///
90    /// let mut line = TextLine::from_string("Hello, world!".into());
91    /// let mut text_cursor = 7;
92    /// assert!(line.remove_backward(&mut text_cursor));
93    /// assert_eq!(line.as_str(), "Hello,world!");
94    /// assert_eq!(text_cursor, 6);
95    /// ```
96    pub fn remove_backward(&mut self, text_cursor: &mut usize) -> bool {
97        let remove = *text_cursor > 0;
98        if remove {
99            *text_cursor -= 1;
100            self.remove(*text_cursor);
101        }
102        remove
103    }
104
105    /// Removes the previous word at the current text cursor position, if it exists, and updates the text cursor.
106    ///
107    /// # Arguments
108    /// * `text_cursor` - A mutable reference to the current text cursor position.
109    ///
110    /// # Returns
111    /// `true` if a word was removed, `false` otherwise.
112    ///
113    /// # Examples
114    /// ```
115    /// use text_editing::TextLine;
116    ///
117    /// let mut line = TextLine::from_string("Hello, world!".into());
118    /// let mut text_cursor = 12;
119    /// assert!(line.remove_backward_skip(&mut text_cursor));
120    /// assert_eq!(line.as_str(), "Hello, !");
121    /// assert_eq!(text_cursor, 7);
122    /// ```
123    pub fn remove_backward_skip(&mut self, text_cursor: &mut usize) -> bool {
124        let end_cursor = *text_cursor;
125        self.skip_backward(text_cursor);
126        let range = *text_cursor..end_cursor;
127        let moved = !range.is_empty();
128        if moved {
129            self.remove_range(range);
130        }
131        moved
132    }
133}