Skip to content

Commit

Permalink
Fix empty line checks (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusGix authored Mar 25, 2024
1 parent eba11fb commit f0e12db
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
48 changes: 44 additions & 4 deletions src/views/editor/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ fn find_prev_rvline(view: &Editor, start: RVLine, count: usize) -> Option<RVLine
let mut info = None;
let mut found_count = 0;
for prev_info in view.iter_rvlines(true, start).skip(1) {
if prev_info.is_empty() {
if prev_info.is_empty_phantom() {
// We skip any phantom text lines in our consideration
continue;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ fn find_next_rvline_info(
return Some(next_info);
}

if next_info.is_empty() {
if next_info.is_empty_phantom() {
// We skip any phantom text lines in our consideration
// TODO: Would this skip over an empty line?
continue;
Expand Down Expand Up @@ -784,14 +784,14 @@ mod tests {

use floem_editor_core::{
buffer::rope_text::{RopeText, RopeTextVal},
cursor::CursorAffinity,
cursor::{ColPosition, CursorAffinity},
mode::Mode,
};
use floem_reactive::Scope;
use lapce_xi_rope::Rope;

use crate::views::editor::{
movement::{correct_crlf, end_of_line},
movement::{correct_crlf, end_of_line, move_down, move_up},
text::SimpleStyling,
text_document::TextDocument,
};
Expand Down Expand Up @@ -861,4 +861,44 @@ mod tests {
let mut aff = CursorAffinity::Backward;
assert_eq!(end_of_line(&ed, &mut aff, 0, Mode::Insert).0, 7);
}

#[test]
fn test_move_down() {
let ed = make_ed("abc\n\n\ndef\n\nghi");

let mut aff = CursorAffinity::Forward;

assert_eq!(move_down(&ed, 0, &mut aff, None, Mode::Insert, 1).0, 4);

let (offset, horiz) = move_down(&ed, 1, &mut aff, None, Mode::Insert, 1);
assert_eq!(offset, 4);
assert!(matches!(horiz, ColPosition::Col(_)));
let (offset, horiz) = move_down(&ed, 4, &mut aff, Some(horiz), Mode::Insert, 1);
assert_eq!(offset, 5);
assert!(matches!(horiz, ColPosition::Col(_)));
let (offset, _) = move_down(&ed, 5, &mut aff, Some(horiz), Mode::Insert, 1);
// Moving down with a horiz starting from position 1 on first line will put cursor at
// (approximately) position 1 on the next line with content they arrive at
assert_eq!(offset, 7);
}

#[test]
fn test_move_up() {
let ed = make_ed("abc\n\n\ndef\n\nghi");

let mut aff = CursorAffinity::Forward;

assert_eq!(move_up(&ed, 0, &mut aff, None, Mode::Insert, 1).0, 0);

let (offset, horiz) = move_up(&ed, 7, &mut aff, None, Mode::Insert, 1);
assert_eq!(offset, 5);
assert!(matches!(horiz, ColPosition::Col(_)));
let (offset, horiz) = move_up(&ed, 5, &mut aff, Some(horiz), Mode::Insert, 1);
assert_eq!(offset, 4);
assert!(matches!(horiz, ColPosition::Col(_)));
let (offset, _) = move_up(&ed, 4, &mut aff, Some(horiz), Mode::Insert, 1);
// Moving up with a horiz starting from position 1 on first line will put cursor at
// (approximately) position 1 on the next line with content they arrive at
assert_eq!(offset, 1);
}
}
4 changes: 2 additions & 2 deletions src/views/editor/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl EditorView {
let right_col = phantom_text.col_after(right_col, false);

// Skip over empty selections
if !info.is_empty() && left_col == right_col {
if !info.is_empty_phantom() && left_col == right_col {
continue;
}

Expand All @@ -392,7 +392,7 @@ impl EditorView {
x1
};

let (x0, width) = if info.is_empty() {
let (x0, width) = if info.is_empty_phantom() {
let text_layout = ed.text_layout(line);
let width = text_layout
.get_layout_x(rvline.line_index)
Expand Down
9 changes: 7 additions & 2 deletions src/views/editor/visual_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,12 @@ impl<L: std::fmt::Debug> VLineInfo<L> {
self.interval.is_empty()
}

/// Check whether the interval is empty and we're not on the first line,
/// thus likely being phantom text (or possibly poor wrapping)
pub fn is_empty_phantom(&self) -> bool {
self.is_empty() && self.rvline.line_index != 0
}

pub fn is_first(&self) -> bool {
self.rvline.is_first()
}
Expand Down Expand Up @@ -1516,8 +1522,7 @@ impl<L: std::fmt::Debug> VLineInfo<L> {
let start_offset = text_prov.text().offset_of_line(self.rvline.line);
// If these subtractions crash, then it is likely due to a bad vline being kept around
// somewhere
if !caret && !self.interval.is_empty() {
// TODO: is this prev grapheme offset for vline end correct for \r\n lines?
if !caret && !self.is_empty() {
let vline_pre_end = text_prov.rope_text().prev_grapheme_offset(vline_end, 1, 0);
vline_pre_end - start_offset
} else {
Expand Down

0 comments on commit f0e12db

Please sign in to comment.