text_editing/cursor.rs
1use super::TextLine;
2
3#[derive(Copy, Clone, PartialEq, Eq)]
4enum CharKind {
5 Whitespace,
6 Alphanumeric,
7 Other,
8}
9
10impl CharKind {
11 fn of(c: char) -> Self {
12 if c.is_whitespace() {
13 Self::Whitespace
14 } else if c.is_alphanumeric() {
15 Self::Alphanumeric
16 } else {
17 Self::Other
18 }
19 }
20}
21
22/// Represents the direction of movement for the text cursor.
23#[derive(Copy, Clone, PartialEq, Eq)]
24pub enum Direction {
25 /// Moves the text cursor forward.
26 Forward,
27 /// Moves the text cursor backward.
28 Backward,
29}
30
31impl TextLine {
32 /// Returns the appropriate movement function based on parameters `movement` and `skip`.
33 ///
34 /// # Arguments
35 /// * `movement` - The direction of movement (`Forward` or `Backward`).
36 /// * `skip` - Whether to skip over whitespace characters.
37 ///
38 /// # Returns
39 /// A function pointer to the appropriate movement function.
40 ///
41 /// # Examples
42 /// ```
43 /// use text_editing::{TextLine, Direction};
44 ///
45 /// let line = TextLine::from_string("Hello, world!".into());
46 /// let mut text_cursor = 7;
47 /// let movement_fn = TextLine::cursor_movement(Direction::Forward, false);
48 /// movement_fn(&line, &mut text_cursor);
49 /// assert_eq!(text_cursor, 8);
50 /// ```
51 #[must_use]
52 pub fn cursor_movement(movement: Direction, skip: bool) -> fn(&Self, &mut usize) -> bool {
53 match (movement, skip) {
54 (Direction::Backward, false) => Self::backward,
55 (Direction::Backward, true) => Self::skip_backward,
56 (Direction::Forward, false) => Self::forward,
57 (Direction::Forward, true) => Self::skip_forward,
58 }
59 }
60
61 /// Moves the text cursor forward by one.
62 ///
63 /// # Arguments
64 /// * `text_cursor` - A mutable reference to the current text cursor position.
65 ///
66 /// # Returns
67 /// `true` if the cursor was successfully moved forward, `false` otherwise.
68 ///
69 /// # Examples
70 /// ```
71 /// use text_editing::TextLine;
72 ///
73 /// let line = TextLine::from_string("Hello, world!".into());
74 /// let mut text_cursor = 6;
75 /// assert!(line.forward(&mut text_cursor));
76 /// assert_eq!(text_cursor, 7);
77 /// ```
78 pub const fn forward(&self, text_cursor: &mut usize) -> bool {
79 if *text_cursor < self.len() {
80 *text_cursor += 1;
81 true
82 } else {
83 false
84 }
85 }
86
87 /// Moves the text cursor backward by one.
88 ///
89 /// # Arguments
90 /// * `text_cursor` - A mutable reference to the current text cursor position.
91 ///
92 /// # Returns
93 /// `true` if the cursor was successfully moved backward, `false` otherwise.
94 ///
95 /// # Examples
96 /// ```
97 /// use text_editing::TextLine;
98 ///
99 /// let line = TextLine::from_string("Hello, world!".into());
100 /// let mut text_cursor = 7;
101 /// assert!(line.backward(&mut text_cursor));
102 /// assert_eq!(text_cursor, 6);
103 /// ```
104 pub const fn backward(&self, text_cursor: &mut usize) -> bool {
105 if *text_cursor > 0 {
106 *text_cursor -= 1;
107 true
108 } else {
109 false
110 }
111 }
112
113 /// Moves the text cursor forward until the end of the current word.
114 ///
115 /// # Arguments
116 /// * `text_cursor` - A mutable reference to the current text cursor position.
117 ///
118 /// # Returns
119 /// `true` if the cursor was successfully moved to the end of the word, `false` otherwise.
120 ///
121 /// # Examples
122 /// ```
123 /// use text_editing::TextLine;
124 ///
125 /// let line = TextLine::from_string("Hello, world!".into());
126 /// let mut text_cursor = 6;
127 /// assert!(line.skip_forward(&mut text_cursor));
128 /// assert_eq!(text_cursor, 12);
129 /// ```
130 pub fn skip_forward(&self, text_cursor: &mut usize) -> bool {
131 let len = self.len();
132
133 let start_kind = loop {
134 if *text_cursor == len {
135 return false;
136 }
137
138 match CharKind::of(self.char_at(*text_cursor)) {
139 CharKind::Whitespace => (),
140 kind => break kind,
141 }
142
143 *text_cursor += 1;
144 };
145
146 loop {
147 *text_cursor += 1;
148
149 if *text_cursor == len {
150 return true;
151 }
152
153 if CharKind::of(self.char_at(*text_cursor)) != start_kind {
154 return true;
155 }
156 }
157 }
158
159 /// Moves the text cursor back until the start of the current word.
160 ///
161 /// # Arguments
162 /// * `text_cursor` - A mutable reference to the current text cursor position.
163 ///
164 /// # Returns
165 /// `true` if the cursor was successfully moved to the start of the word, `false` otherwise.
166 ///
167 /// # Examples
168 /// ```
169 /// use text_editing::TextLine;
170 ///
171 /// let line = TextLine::from_string("Hello, world!".into());
172 /// let mut text_cursor = 10;
173 /// assert!(line.skip_backward(&mut text_cursor));
174 /// assert_eq!(text_cursor, 7);
175 /// ```
176 pub fn skip_backward(&self, text_cursor: &mut usize) -> bool {
177 if *text_cursor == 0 {
178 return false;
179 }
180
181 let start_kind = loop {
182 *text_cursor -= 1;
183
184 if *text_cursor == 0 {
185 return false;
186 }
187
188 match CharKind::of(self.char_at(*text_cursor)) {
189 CharKind::Whitespace => (),
190 kind => break kind,
191 }
192 };
193
194 loop {
195 *text_cursor -= 1;
196
197 if CharKind::of(self.char_at(*text_cursor)) != start_kind {
198 *text_cursor += 1;
199 return true;
200 }
201
202 if *text_cursor == 0 {
203 return true;
204 }
205 }
206 }
207}