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
134 /// Moves the text cursor to the start of the line.
135 ///
136 /// # Arguments
137 /// * `text_cursor` - A mutable reference to the current text cursor position.
138 ///
139 /// # Examples
140 /// ```
141 /// use text_editing::TextLine;
142 ///
143 /// let line = TextLine::from_string("Hello, world!".into());
144 /// let mut text_cursor = 7;
145 /// line.cursor_to_start(&mut text_cursor);
146 /// assert_eq!(text_cursor, 0);
147 /// ```
148 pub const fn cursor_to_start(&self, text_cursor: &mut usize) {
149 *text_cursor = 0;
150 }
151
152 /// Moves the text cursor to the end of the line.
153 ///
154 /// # Arguments
155 /// * `text_cursor` - A mutable reference to the current text cursor position.
156 ///
157 /// # Examples
158 /// ```
159 /// use text_editing::TextLine;
160 ///
161 /// let line = TextLine::from_string("Hello, world!".into());
162 /// let mut text_cursor = 7;
163 /// line.cursor_to_end(&mut text_cursor);
164 /// assert_eq!(text_cursor, 13);
165 /// ```
166 pub const fn cursor_to_end(&self, text_cursor: &mut usize) {
167 *text_cursor = self.len();
168 }
169
170 /// Inserts a string at the current text cursor position and advances the cursor.
171 ///
172 /// # Arguments
173 /// * `text_cursor` - A mutable reference to the current text cursor position.
174 /// * `text` - The string to insert.
175 ///
176 /// # Examples
177 /// ```
178 /// use text_editing::TextLine;
179 ///
180 /// let mut line = TextLine::from_string("Hello, !".into());
181 /// let mut text_cursor = 7;
182 /// line.insert_str(&mut text_cursor, "world");
183 /// assert_eq!(line.as_str(), "Hello, world!");
184 /// assert_eq!(text_cursor, 12);
185 /// ```
186 pub fn insert_str(&mut self, text_cursor: &mut usize, text: &str) {
187 let pos = *text_cursor;
188 let byte_index = self.string_index(pos);
189 let mut count = 0;
190 let mut total_extra = 0;
191 for c in text.chars() {
192 count += 1;
193 total_extra += c.len_utf8() - 1;
194 }
195 self.text.insert_str(byte_index, text);
196 *text_cursor = pos + count;
197 if !self.indices.is_empty() {
198 let base = if pos > 0 { self.indices[pos - 1] } else { 0 };
199 self.indices.splice(
200 pos..pos,
201 text.chars().scan(base, |running, c| {
202 *running += c.len_utf8() - 1;
203 Some(*running)
204 }),
205 );
206 for v in &mut self.indices[pos + count..] {
207 *v += total_extra;
208 }
209 } else if total_extra > 0 {
210 self.enable_indices();
211 }
212 }
213}