Skip to content

Commit 35b5a18

Browse files
committed
Final little fix
1 parent ac7d6c0 commit 35b5a18

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/components/textinput.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl TextInputComponent {
302302

303303
let mut dist =
304304
self.get_real_distance(line_start, self.cursor_position);
305+
305306
self.cursor_position = line_end;
306307

307308
if line_start != 0 {
@@ -314,25 +315,20 @@ impl TextInputComponent {
314315
fn get_real_distance(&self, start: usize, end: usize) -> usize {
315316
let mut index = start;
316317
let mut dist = 0;
317-
while index < end {
318+
while index <= end {
318319
if self.msg.is_char_boundary(index) {
319320
dist += 1;
320321
}
321322
index += 1;
322323
}
323-
dist + 1
324+
dist
324325
}
325326

326-
/// Move forward `distance` amount of charachters stopping at a newline
327+
/// Move forward `distance` amount of characters stopping at a newline
327328
fn cursor_forward(&mut self, distance: usize) {
328329
let mut travelled = 0;
329330
let mut index = self.cursor_position;
330331
while index < self.msg.len() + 1 {
331-
if index == self.msg.len() - 1 {
332-
self.cursor_position = index + 1;
333-
break;
334-
}
335-
336332
if self.msg.is_char_boundary(index) {
337333
travelled += 1;
338334
}
@@ -342,6 +338,11 @@ impl TextInputComponent {
342338
break;
343339
}
344340

341+
if index == self.msg.len() - 1 {
342+
self.cursor_position = index + 1;
343+
break;
344+
}
345+
345346
index += 1;
346347

347348
if index != self.msg.len() - 1
@@ -897,7 +898,7 @@ mod tests {
897898
false,
898899
);
899900

900-
comp.set_text(String::from("aaaaa\näaa\naaa"));
901+
comp.set_text(String::from("aaaaa\näaa\naaa\naaa"));
901902

902903
comp.cursor_position = 0;
903904
comp.cursor_down();
@@ -915,6 +916,10 @@ mod tests {
915916
comp.cursor_down();
916917
assert_eq!(comp.cursor_position, 12);
917918

919+
comp.cursor_position = 13;
920+
comp.cursor_down();
921+
assert_eq!(comp.cursor_position, 17);
922+
918923
comp.cursor_position = 6;
919924
comp.cursor_up();
920925
assert_eq!(comp.cursor_position, 0);

0 commit comments

Comments
 (0)