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
use super::TextLine;
impl TextLine {
/// Adds a char at the current text cursor position.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
/// * `c` - The char to add.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, ".into());
/// let mut text_cursor = 7;
/// line.add_char(&mut text_cursor, 'w');
/// assert_eq!(line.as_str(), "Hello, w");
/// assert_eq!(text_cursor, 8);
/// ```
pub fn add_char(&mut self, text_cursor: &mut usize, c: char) {
self.insert(*text_cursor, c);
*text_cursor += 1;
}
/// Removes the next char at the current text cursor position, if it exists.
///
/// # Arguments
/// * `text_cursor` - The current text cursor position.
///
/// # Returns
/// `true` if a char was removed, `false` otherwise.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, world!".into());
/// let text_cursor = 7;
/// assert!(line.remove_forward(text_cursor));
/// assert_eq!(line.as_str(), "Hello, orld!");
/// ```
pub fn remove_forward(&mut self, text_cursor: usize) -> bool {
let remove = text_cursor < self.len();
if remove {
self.remove(text_cursor);
}
remove
}
/// Removes the next word at the current text cursor position, if it exists.
///
/// # Arguments
/// * `text_cursor` - The current text cursor position.
///
/// # Returns
/// `true` if a word was removed, `false` otherwise.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, world!".into());
/// let text_cursor = 7;
/// assert!(line.remove_forward_skip(text_cursor));
/// assert_eq!(line.as_str(), "Hello, !");
/// ```
pub fn remove_forward_skip(&mut self, text_cursor: usize) -> bool {
let mut end_cursor = text_cursor;
self.skip_forward(&mut end_cursor);
let range = text_cursor..end_cursor;
let moved = !range.is_empty();
if moved {
self.remove_range(range);
}
moved
}
/// Removes the previous char at the current text cursor position, if it exists, and updates the text cursor.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
///
/// # Returns
/// `true` if a char was removed, `false` otherwise.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, world!".into());
/// let mut text_cursor = 7;
/// assert!(line.remove_backward(&mut text_cursor));
/// assert_eq!(line.as_str(), "Hello,world!");
/// assert_eq!(text_cursor, 6);
/// ```
pub fn remove_backward(&mut self, text_cursor: &mut usize) -> bool {
let remove = *text_cursor > 0;
if remove {
*text_cursor -= 1;
self.remove(*text_cursor);
}
remove
}
/// Removes the previous word at the current text cursor position, if it exists, and updates the text cursor.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
///
/// # Returns
/// `true` if a word was removed, `false` otherwise.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, world!".into());
/// let mut text_cursor = 12;
/// assert!(line.remove_backward_skip(&mut text_cursor));
/// assert_eq!(line.as_str(), "Hello, !");
/// assert_eq!(text_cursor, 7);
/// ```
pub fn remove_backward_skip(&mut self, text_cursor: &mut usize) -> bool {
let end_cursor = *text_cursor;
self.skip_backward(text_cursor);
let range = *text_cursor..end_cursor;
let moved = !range.is_empty();
if moved {
self.remove_range(range);
}
moved
}
/// Moves the text cursor to the start of the line.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let line = TextLine::from_string("Hello, world!".into());
/// let mut text_cursor = 7;
/// line.cursor_to_start(&mut text_cursor);
/// assert_eq!(text_cursor, 0);
/// ```
pub fn cursor_to_start(&self, text_cursor: &mut usize) {
*text_cursor = 0;
}
/// Moves the text cursor to the end of the line.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let line = TextLine::from_string("Hello, world!".into());
/// let mut text_cursor = 7;
/// line.cursor_to_end(&mut text_cursor);
/// assert_eq!(text_cursor, 13);
/// ```
pub fn cursor_to_end(&self, text_cursor: &mut usize) {
*text_cursor = self.len();
}
/// Inserts a string at the current text cursor position and advances the cursor.
///
/// # Arguments
/// * `text_cursor` - A mutable reference to the current text cursor position.
/// * `text` - The string to insert.
///
/// # Examples
/// ```
/// use text_editing::TextLine;
///
/// let mut line = TextLine::from_string("Hello, !".into());
/// let mut text_cursor = 7;
/// line.insert_str(&mut text_cursor, "world");
/// assert_eq!(line.as_str(), "Hello, world!");
/// assert_eq!(text_cursor, 12);
/// ```
pub fn insert_str(&mut self, text_cursor: &mut usize, text: &str) {
let byte_index = self.string_index(*text_cursor);
self.text.insert_str(byte_index, text);
*text_cursor += text.chars().count();
self.indices.clear();
self.refresh_indices();
}
}