text_editing/selection.rs
1use std::ops::Range;
2
3use super::TextLine;
4
5impl TextLine {
6 /// Returns the ordered selection range from an anchor and cursor position.
7 ///
8 /// Returns `None` if no anchor is set.
9 ///
10 /// # Arguments
11 /// * `cursor` - The current text cursor position.
12 /// * `anchor` - The optional selection anchor position.
13 ///
14 /// # Examples
15 /// ```
16 /// use text_editing::TextLine;
17 ///
18 /// assert_eq!(TextLine::selection_range(10, Some(5)), Some(5..10));
19 /// assert_eq!(TextLine::selection_range(3, Some(8)), Some(3..8));
20 /// assert_eq!(TextLine::selection_range(5, None), None);
21 /// ```
22 #[must_use]
23 pub fn selection_range(cursor: usize, anchor: Option<usize>) -> Option<Range<usize>> {
24 let a = anchor?;
25 Some(a.min(cursor)..a.max(cursor))
26 }
27
28 /// Returns the selected text, if a selection is active.
29 ///
30 /// # Arguments
31 /// * `cursor` - The current text cursor position.
32 /// * `anchor` - The optional selection anchor position.
33 ///
34 /// # Examples
35 /// ```
36 /// use text_editing::TextLine;
37 ///
38 /// let line = TextLine::from_string("Hello, world!".into());
39 /// assert_eq!(line.selected_text(12, Some(7)), Some("world"));
40 /// assert_eq!(line.selected_text(5, None), None);
41 /// ```
42 #[must_use]
43 pub fn selected_text(&self, cursor: usize, anchor: Option<usize>) -> Option<&str> {
44 Self::selection_range(cursor, anchor)
45 .map(|range| &self.text[self.string_index(range.start)..self.string_index(range.end)])
46 }
47
48 /// Deletes the currently selected text and updates cursor and anchor.
49 ///
50 /// Returns `true` if text was deleted, `false` if no selection was active
51 /// or the selection was empty.
52 ///
53 /// # Arguments
54 /// * `cursor` - A mutable reference to the current text cursor position.
55 /// * `anchor` - A mutable reference to the optional selection anchor.
56 ///
57 /// # Examples
58 /// ```
59 /// use text_editing::TextLine;
60 ///
61 /// let mut line = TextLine::from_string("Hello, world!".into());
62 /// let mut cursor = 12;
63 /// let mut anchor = Some(7);
64 /// assert!(line.delete_selection(&mut cursor, &mut anchor));
65 /// assert_eq!(line.as_str(), "Hello, !");
66 /// assert_eq!(cursor, 7);
67 /// assert_eq!(anchor, None);
68 /// ```
69 pub fn delete_selection(&mut self, cursor: &mut usize, anchor: &mut Option<usize>) -> bool {
70 if let Some(range) = Self::selection_range(*cursor, *anchor)
71 && !range.is_empty()
72 {
73 self.remove_range(range.clone());
74 *cursor = range.start;
75 *anchor = None;
76 return true;
77 }
78 *anchor = None;
79 false
80 }
81
82 /// Replaces the current selection with the given text.
83 ///
84 /// If no selection is active, inserts the text at the cursor position.
85 ///
86 /// # Arguments
87 /// * `cursor` - A mutable reference to the current text cursor position.
88 /// * `anchor` - A mutable reference to the optional selection anchor.
89 /// * `text` - The replacement text.
90 ///
91 /// # Examples
92 /// ```
93 /// use text_editing::TextLine;
94 ///
95 /// let mut line = TextLine::from_string("Hello, world!".into());
96 /// let mut cursor = 12;
97 /// let mut anchor = Some(7);
98 /// line.replace_selection(&mut cursor, &mut anchor, "Rust");
99 /// assert_eq!(line.as_str(), "Hello, Rust!");
100 /// assert_eq!(cursor, 11);
101 /// assert_eq!(anchor, None);
102 /// ```
103 pub fn replace_selection(
104 &mut self,
105 cursor: &mut usize,
106 anchor: &mut Option<usize>,
107 text: &str,
108 ) {
109 self.delete_selection(cursor, anchor);
110 self.insert_str(cursor, text);
111 }
112
113 /// Selects all text in the line.
114 ///
115 /// Sets the anchor to the beginning and the cursor to the end.
116 ///
117 /// # Arguments
118 /// * `cursor` - A mutable reference to the current text cursor position.
119 /// * `anchor` - A mutable reference to the optional selection anchor.
120 ///
121 /// # Examples
122 /// ```
123 /// use text_editing::TextLine;
124 ///
125 /// let line = TextLine::from_string("Hello, world!".into());
126 /// let mut cursor = 5;
127 /// let mut anchor = None;
128 /// line.select_all(&mut cursor, &mut anchor);
129 /// assert_eq!(cursor, 13);
130 /// assert_eq!(anchor, Some(0));
131 /// ```
132 pub const fn select_all(&self, cursor: &mut usize, anchor: &mut Option<usize>) {
133 *anchor = Some(0);
134 *cursor = self.len();
135 }
136
137 /// Extends the selection forward by one character.
138 ///
139 /// Sets the anchor to the current cursor position if no selection is active,
140 /// then moves the cursor forward.
141 ///
142 /// # Examples
143 /// ```
144 /// use text_editing::TextLine;
145 ///
146 /// let line = TextLine::from_string("Hello, world!".into());
147 /// let mut cursor = 7;
148 /// let mut anchor = None;
149 /// assert!(line.select_forward(&mut cursor, &mut anchor));
150 /// assert_eq!(cursor, 8);
151 /// assert_eq!(anchor, Some(7));
152 /// ```
153 pub const fn select_forward(&self, cursor: &mut usize, anchor: &mut Option<usize>) -> bool {
154 if anchor.is_none() {
155 *anchor = Some(*cursor);
156 }
157 self.forward(cursor)
158 }
159
160 /// Extends the selection backward by one character.
161 ///
162 /// Sets the anchor to the current cursor position if no selection is active,
163 /// then moves the cursor backward.
164 ///
165 /// # Examples
166 /// ```
167 /// use text_editing::TextLine;
168 ///
169 /// let line = TextLine::from_string("Hello, world!".into());
170 /// let mut cursor = 7;
171 /// let mut anchor = None;
172 /// assert!(line.select_backward(&mut cursor, &mut anchor));
173 /// assert_eq!(cursor, 6);
174 /// assert_eq!(anchor, Some(7));
175 /// ```
176 pub const fn select_backward(&self, cursor: &mut usize, anchor: &mut Option<usize>) -> bool {
177 if anchor.is_none() {
178 *anchor = Some(*cursor);
179 }
180 self.backward(cursor)
181 }
182
183 /// Extends the selection forward by one word.
184 ///
185 /// Sets the anchor to the current cursor position if no selection is active,
186 /// then moves the cursor to the end of the next word.
187 ///
188 /// # Examples
189 /// ```
190 /// use text_editing::TextLine;
191 ///
192 /// let line = TextLine::from_string("Hello, world!".into());
193 /// let mut cursor = 7;
194 /// let mut anchor = None;
195 /// assert!(line.select_forward_skip(&mut cursor, &mut anchor));
196 /// assert_eq!(cursor, 12);
197 /// assert_eq!(anchor, Some(7));
198 /// ```
199 pub fn select_forward_skip(&self, cursor: &mut usize, anchor: &mut Option<usize>) -> bool {
200 if anchor.is_none() {
201 *anchor = Some(*cursor);
202 }
203 self.skip_forward(cursor)
204 }
205
206 /// Extends the selection backward by one word.
207 ///
208 /// Sets the anchor to the current cursor position if no selection is active,
209 /// then moves the cursor to the start of the previous word.
210 ///
211 /// # Examples
212 /// ```
213 /// use text_editing::TextLine;
214 ///
215 /// let line = TextLine::from_string("Hello, world!".into());
216 /// let mut cursor = 12;
217 /// let mut anchor = None;
218 /// assert!(line.select_backward_skip(&mut cursor, &mut anchor));
219 /// assert_eq!(cursor, 7);
220 /// assert_eq!(anchor, Some(12));
221 /// ```
222 pub fn select_backward_skip(&self, cursor: &mut usize, anchor: &mut Option<usize>) -> bool {
223 if anchor.is_none() {
224 *anchor = Some(*cursor);
225 }
226 self.skip_backward(cursor)
227 }
228
229 /// Clears the current selection without modifying the text.
230 ///
231 /// # Examples
232 /// ```
233 /// use text_editing::TextLine;
234 ///
235 /// let mut anchor = Some(5);
236 /// TextLine::clear_selection(&mut anchor);
237 /// assert_eq!(anchor, None);
238 /// ```
239 pub const fn clear_selection(anchor: &mut Option<usize>) {
240 *anchor = None;
241 }
242}