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
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
        }
    }
}

impl TextLine {
    /// Returns the appropriate movement function based on parameters `forward` and `skip`.
    pub fn cursor_movement(forward: bool, skip: bool) -> fn(&TextLine, &mut usize) -> bool {
        match (forward, skip) {
            (false, false) => Self::back,
            (false, true) => Self::skip_back,
            (true, false) => Self::forward,
            (true, true) => Self::skip_forward,
        }
    }

    /// Moves the text cursor forward by one.
    pub fn forward(&self, text_cursor: &mut usize) -> bool {
        if *text_cursor < self.len() {
            *text_cursor += 1;
            true
        } else {
            false
        }
    }

    /// Moves the text cursor back by one.
    pub fn back(&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.
    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.
    pub fn skip_back(&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;
            }
        }
    }
}