1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::TextLine;

#[derive(Copy, Clone, PartialEq, Eq)]
enum CharKind {
    Whitespace,
    Alphanumeric,
    Other,
}

impl CharKind {
    fn of(c: char) -> Self {
        use CharKind::*;
        if c.is_whitespace() {
            Whitespace
        } else if c.is_alphanumeric() {
            Alphanumeric
        } else {
            Other
        }
    }
}

/// Represents the direction of movement for the text cursor.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Direction {
    /// Moves the text cursor forward.
    Forward,
    /// Moves the text cursor backward.
    Backward,
}

impl TextLine {
    /// Returns the appropriate movement function based on parameters `movement` and `skip`.
    ///
    /// # Arguments
    /// * `movement` - The direction of movement (`Forward` or `Backward`).
    /// * `skip` - Whether to skip over whitespace characters.
    ///
    /// # Returns
    /// A function pointer to the appropriate movement function.
    ///
    /// # Examples
    /// ```
    /// use text_editing::{TextLine, Direction};
    ///
    /// let line = TextLine::from_string("Hello, world!".into());
    /// let mut text_cursor = 7;
    /// let movement_fn = TextLine::cursor_movement(Direction::Forward, false);
    /// movement_fn(&line, &mut text_cursor);
    /// assert_eq!(text_cursor, 8);
    /// ```
    pub fn cursor_movement(movement: Direction, skip: bool) -> fn(&Self, &mut usize) -> bool {
        use Direction::*;
        match (movement, skip) {
            (Backward, false) => Self::backward,
            (Backward, true) => Self::skip_backward,
            (Forward, false) => Self::forward,
            (Forward, true) => Self::skip_forward,
        }
    }

    /// Moves the text cursor forward by one.
    ///
    /// # Arguments
    /// * `text_cursor` - A mutable reference to the current text cursor position.
    ///
    /// # Returns
    /// `true` if the cursor was successfully moved forward, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use text_editing::TextLine;
    ///
    /// let line = TextLine::from_string("Hello, world!".into());
    /// let mut text_cursor = 6;
    /// assert!(line.forward(&mut text_cursor));
    /// assert_eq!(text_cursor, 7);
    /// ```
    pub fn forward(&self, text_cursor: &mut usize) -> bool {
        if *text_cursor < self.len() {
            *text_cursor += 1;
            true
        } else {
            false
        }
    }

    /// Moves the text cursor backward by one.
    ///
    /// # Arguments
    /// * `text_cursor` - A mutable reference to the current text cursor position.
    ///
    /// # Returns
    /// `true` if the cursor was successfully moved backward, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use text_editing::TextLine;
    ///
    /// let line = TextLine::from_string("Hello, world!".into());
    /// let mut text_cursor = 7;
    /// assert!(line.backward(&mut text_cursor));
    /// assert_eq!(text_cursor, 6);
    /// ```
    pub fn backward(&self, text_cursor: &mut usize) -> bool {
        if *text_cursor > 0 {
            *text_cursor -= 1;
            true
        } else {
            false
        }
    }

    /// Moves the text cursor forward until the end of the current word.
    ///
    /// # Arguments
    /// * `text_cursor` - A mutable reference to the current text cursor position.
    ///
    /// # Returns
    /// `true` if the cursor was successfully moved to the end of the word, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use text_editing::TextLine;
    ///
    /// let line = TextLine::from_string("Hello, world!".into());
    /// let mut text_cursor = 6;
    /// assert!(line.skip_forward(&mut text_cursor));
    /// assert_eq!(text_cursor, 12);
    /// ```
    pub fn skip_forward(&self, text_cursor: &mut usize) -> bool {
        let len = self.len();

        let start_kind = loop {
            if *text_cursor == len {
                return false;
            }

            match CharKind::of(self.char_at(*text_cursor)) {
                CharKind::Whitespace => (),
                kind => break kind,
            }

            *text_cursor += 1;
        };

        loop {
            *text_cursor += 1;

            if *text_cursor == len {
                return true;
            }

            if CharKind::of(self.char_at(*text_cursor)) != start_kind {
                return true;
            }
        }
    }

    /// Moves the text cursor back until the start of the current word.
    ///
    /// # Arguments
    /// * `text_cursor` - A mutable reference to the current text cursor position.
    ///
    /// # Returns
    /// `true` if the cursor was successfully moved to the start of the word, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use text_editing::TextLine;
    ///
    /// let line = TextLine::from_string("Hello, world!".into());
    /// let mut text_cursor = 10;
    /// assert!(line.skip_backward(&mut text_cursor));
    /// assert_eq!(text_cursor, 7);
    /// ```
    pub fn skip_backward(&self, text_cursor: &mut usize) -> bool {
        if *text_cursor == 0 {
            return false;
        }

        let start_kind = loop {
            *text_cursor -= 1;

            if *text_cursor == 0 {
                return false;
            }

            match CharKind::of(self.char_at(*text_cursor)) {
                CharKind::Whitespace => (),
                kind => break kind,
            }
        };

        loop {
            *text_cursor -= 1;

            if CharKind::of(self.char_at(*text_cursor)) != start_kind {
                *text_cursor += 1;
                return true;
            }

            if *text_cursor == 0 {
                return true;
            }
        }
    }
}