Add test coverage for same cursor in multiple excerpts of the same buffer

This commit is contained in:
Keith Simmons 2022-05-10 14:30:27 -07:00
parent 68de51ba8a
commit c4738d7316
2 changed files with 44 additions and 52 deletions

View file

@ -9801,65 +9801,49 @@ mod tests {
#[gpui::test] #[gpui::test]
fn test_editing_overlapping_excerpts(cx: &mut gpui::MutableAppContext) { fn test_editing_overlapping_excerpts(cx: &mut gpui::MutableAppContext) {
cx.set_global(Settings::test(cx)); cx.set_global(Settings::test(cx));
let buffer = cx.add_model(|cx| { let (initial_text, excerpt_ranges) = marked_text_ranges(indoc! {"
Buffer::new( [aaaa
0, (bbbb]
indoc! {" cccc)"});
aaaa let buffer = cx.add_model(|cx| Buffer::new(0, initial_text, cx));
bbbb
cccc"},
cx,
)
});
let multibuffer = cx.add_model(|cx| { let multibuffer = cx.add_model(|cx| {
let mut multibuffer = MultiBuffer::new(0); let mut multibuffer = MultiBuffer::new(0);
multibuffer.push_excerpts( multibuffer.push_excerpts(buffer, excerpt_ranges, cx);
buffer,
[
Point::new(0, 0)..Point::new(1, 4),
Point::new(1, 0)..Point::new(2, 4),
],
cx,
);
multibuffer multibuffer
}); });
assert_eq!(
multibuffer.read(cx).read(cx).text(),
"aaaa\nbbbb\nbbbb\ncccc"
);
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(multibuffer, cx)); let (_, view) = cx.add_window(Default::default(), |cx| build_editor(multibuffer, cx));
view.update(cx, |view, cx| { view.update(cx, |view, cx| {
view.select_ranges( let (expected_text, selection_ranges) = marked_text_ranges(indoc! {"
[ aaaa
Point::new(1, 1)..Point::new(1, 1), b|bbb
Point::new(2, 3)..Point::new(2, 3), b|bb|b
], cccc"});
None, assert_eq!(view.text(cx), expected_text);
cx, view.select_ranges(selection_ranges, None, cx);
);
view.handle_input(&Input("X".to_string()), cx); view.handle_input(&Input("X".to_string()), cx);
assert_eq!(view.text(cx), "aaaa\nbXbbXb\nbXbbXb\ncccc");
assert_eq!( let (expected_text, expected_selections) = marked_text_ranges(indoc! {"
view.selected_ranges(cx), aaaa
[ bX|bbXb
Point::new(1, 2)..Point::new(1, 2), bX|bbX|b
Point::new(2, 5)..Point::new(2, 5), cccc"});
] assert_eq!(view.text(cx), expected_text);
); assert_eq!(view.selected_ranges(cx), expected_selections);
view.newline(&Newline, cx); view.newline(&Newline, cx);
assert_eq!(view.text(cx), "aaaa\nbX\nbbX\nb\nbX\nbbX\nb\ncccc"); let (expected_text, expected_selections) = marked_text_ranges(indoc! {"
assert_eq!( aaaa
view.selected_ranges(cx), bX
[ |bbX
Point::new(2, 0)..Point::new(2, 0), b
Point::new(6, 0)..Point::new(6, 0), bX
] |bbX
); |b
cccc"});
assert_eq!(view.text(cx), expected_text);
assert_eq!(view.selected_ranges(cx), expected_selections);
}); });
} }

View file

@ -55,10 +55,18 @@ pub fn marked_text_ranges_by(
(unmarked_text, range_lookup) (unmarked_text, range_lookup)
} }
pub fn marked_text_ranges(marked_text: &str) -> (String, Vec<Range<usize>>) { // Returns ranges delimited by (), [], and <> ranges. Ranges using the same markers
let (unmarked_text, mut ranges) = marked_text_ranges_by(marked_text, vec![('[', ']')]); // must not be overlapping. May also include | for empty ranges
pub fn marked_text_ranges(full_marked_text: &str) -> (String, Vec<Range<usize>>) {
let (range_marked_text, empty_offsets) = marked_text(full_marked_text);
let (unmarked, range_lookup) =
marked_text_ranges_by(&range_marked_text, vec![('[', ']'), ('(', ')'), ('<', '>')]);
( (
unmarked_text, unmarked,
ranges.remove(&('[', ']')).unwrap_or_else(Vec::new), range_lookup
.into_values()
.flatten()
.chain(empty_offsets.into_iter().map(|offset| offset..offset))
.collect(),
) )
} }