Skip to content

Commit 222e603

Browse files
committed
DRY
1 parent 0080077 commit 222e603

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/core_editor/line_buffer.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl LineBuffer {
8383
self.insertion_point = self.lines.len();
8484
}
8585

86-
/// Calculates the current the user is on
86+
/// Calculates the current line the user is on
8787
///
8888
/// Zero-based index
8989
pub fn line(&self) -> usize {
@@ -105,20 +105,21 @@ impl LineBuffer {
105105
self.insertion_point = 0;
106106
}
107107

108-
/// Move the cursor before the first character of the line
109-
pub fn move_to_line_start(&mut self) {
110-
self.insertion_point = self.lines[..self.insertion_point]
108+
fn line_start(&self) -> usize {
109+
self.lines[..self.insertion_point]
111110
.rfind('\n')
112-
.map_or(0, |offset| offset + 1);
111+
.map_or(0, |offset| offset + 1)
113112
// str is guaranteed to be utf8, thus \n is safe to assume 1 byte long
114113
}
115114

115+
/// Move the cursor before the first character of the line
116+
pub fn move_to_line_start(&mut self) {
117+
self.insertion_point = self.line_start();
118+
}
119+
116120
/// Move the cursor before the first non whitespace character of the line
117121
pub fn move_to_line_non_blank_start(&mut self) {
118-
let line_start = self.lines[..self.insertion_point]
119-
.rfind('\n')
120-
.map_or(0, |offset| offset + 1);
121-
// str is guaranteed to be utf8, thus \n is safe to assume 1 byte long
122+
let line_start = self.line_start();
122123

123124
self.insertion_point = self.lines[line_start..]
124125
.find(|c: char| !c.is_whitespace() || c == '\n')
@@ -535,9 +536,7 @@ impl LineBuffer {
535536
/// extending beyond the potential carriage return and line feed characters
536537
/// terminating the line
537538
pub fn current_line_range(&self) -> Range<usize> {
538-
let left_index = self.lines[..self.insertion_point]
539-
.rfind('\n')
540-
.map_or(0, |offset| offset + 1);
539+
let left_index = self.line_start();
541540
let right_index = self.lines[self.insertion_point..]
542541
.find('\n')
543542
.map_or_else(|| self.lines.len(), |i| i + self.insertion_point + 1);

0 commit comments

Comments
 (0)