Skip to content

Commit

Permalink
Fix some editor tests
Browse files Browse the repository at this point in the history
co-authored-by: Conrad <conrad@zed.dev>
  • Loading branch information
mikayla-maki and ConradIrwin committed May 9, 2024
1 parent 7417f62 commit 1e8af58
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 165 deletions.
4 changes: 2 additions & 2 deletions crates/editor/src/display_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ pub mod tests {
let font_size = px(14.0);

let map = cx.new_model(|cx| {
DisplayMap::new(buffer, font("Helvetica"), font_size, None, 1, 1, 0, cx)
DisplayMap::new(buffer, font("Helvetica"), font_size, None, 1, 1, 1, cx)
});
assert_eq!(
cx.update(|cx| syntax_chunks(0..5, &map, &theme, cx)),
Expand Down Expand Up @@ -1630,7 +1630,7 @@ pub mod tests {

let font_size = px(16.0);
let map = cx
.new_model(|cx| DisplayMap::new(buffer, font("Courier"), font_size, None, 1, 1, 0, cx));
.new_model(|cx| DisplayMap::new(buffer, font("Courier"), font_size, None, 1, 1, 1, cx));

enum MyType {}

Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7901,7 +7901,7 @@ impl Editor {
pub fn expand_excerpts(&mut self, action: &ExpandExcerpts, cx: &mut ViewContext<Self>) {
let selections = self.selections.disjoint_anchors();

let lines = if action.lines == 0 { 3 } else { action.lines };
let lines = if action.lines == 0 { 1 } else { action.lines };

self.buffer.update(cx, |buffer, cx| {
buffer.expand_excerpts(
Expand Down
5 changes: 3 additions & 2 deletions crates/editor/src/inlay_hint_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2871,6 +2871,7 @@ pub mod tests {
"main hint #5".to_string(),
"other hint(edited) #0".to_string(),
"other hint(edited) #1".to_string(),
"other hint(edited) #2".to_string(),
];
assert_eq!(
expected_hints,
Expand All @@ -2881,8 +2882,8 @@ pub mod tests {
assert_eq!(expected_hints, visible_hint_labels(editor, cx));

let current_cache_version = editor.inlay_hint_cache().version;
// We expect two new hints for the excerpts from `other.rs`:
let expected_version = last_scroll_update_version + 2;
// We expect three new hints for the excerpts from `other.rs`:
let expected_version = last_scroll_update_version + 3;
assert_eq!(
current_cache_version,
expected_version,
Expand Down
231 changes: 72 additions & 159 deletions crates/editor/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,11 @@ mod tests {
Buffer, DisplayMap, ExcerptRange, InlayId, MultiBuffer,
};
use gpui::{font, Context as _};
use indoc::indoc;
use language::Capability;
use project::Project;
use settings::SettingsStore;
use util::post_inc;
use util::{post_inc, test::marked_text_offsets};

#[gpui::test]
fn test_previous_word_start(cx: &mut gpui::AppContext) {
Expand Down Expand Up @@ -689,7 +690,7 @@ mod tests {
let buffer = MultiBuffer::build_simple(input_text, cx);
let buffer_snapshot = buffer.read(cx).snapshot(cx);
let display_map =
cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, 0, cx));
cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, 1, cx));

// add all kinds of inlays between two word boundaries: we should be able to cross them all, when looking for another boundary
let mut id = 0;
Expand Down Expand Up @@ -866,163 +867,75 @@ mod tests {
init_test(cx);
});

let mut cx = EditorTestContext::new(cx).await;
let editor = cx.editor.clone();
let window = cx.window;
_ = cx.update_window(window, |_, cx| {
let text_layout_details =
editor.update(cx, |editor, cx| editor.text_layout_details(cx));

let font = font("Helvetica");

let buffer = cx.new_model(|cx| Buffer::local("abc\ndefg\nhijkl\nmn", cx));
let multibuffer = cx.new_model(|cx| {
let mut multibuffer = MultiBuffer::new(0, Capability::ReadWrite);
multibuffer.push_excerpts(
buffer.clone(),
[
ExcerptRange {
context: Point::new(0, 0)..Point::new(1, 4),
primary: None,
},
ExcerptRange {
context: Point::new(2, 0)..Point::new(3, 2),
primary: None,
},
],
cx,
);
multibuffer
});
let display_map =
cx.new_model(|cx| DisplayMap::new(multibuffer, font, px(14.0), None, 2, 2, 1, cx));
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));

assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\nhijkl\nmn");

let col_2_x =
snapshot.x_for_display_point(DisplayPoint::new(2, 2), &text_layout_details);

// Can't move up into the first excerpt's header
assert_eq!(
up(
&snapshot,
DisplayPoint::new(2, 2),
SelectionGoal::HorizontalPosition(col_2_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(2, 0),
SelectionGoal::HorizontalPosition(0.0)
),
);
assert_eq!(
up(
&snapshot,
DisplayPoint::new(2, 0),
SelectionGoal::None,
false,
&text_layout_details
),
(
DisplayPoint::new(2, 0),
SelectionGoal::HorizontalPosition(0.0)
),
);

let col_4_x =
snapshot.x_for_display_point(DisplayPoint::new(3, 4), &text_layout_details);

// Move up and down within first excerpt
assert_eq!(
up(
&snapshot,
DisplayPoint::new(3, 4),
SelectionGoal::HorizontalPosition(col_4_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(2, 3),
SelectionGoal::HorizontalPosition(col_4_x.0)
),
);
assert_eq!(
down(
&snapshot,
DisplayPoint::new(2, 3),
SelectionGoal::HorizontalPosition(col_4_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(3, 4),
SelectionGoal::HorizontalPosition(col_4_x.0)
),
);

let col_5_x =
snapshot.x_for_display_point(DisplayPoint::new(6, 5), &text_layout_details);

// Move up and down across second excerpt's header
assert_eq!(
up(
&snapshot,
DisplayPoint::new(6, 5),
SelectionGoal::HorizontalPosition(col_5_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(3, 4),
SelectionGoal::HorizontalPosition(col_5_x.0)
),
);
assert_eq!(
down(
&snapshot,
DisplayPoint::new(3, 4),
SelectionGoal::HorizontalPosition(col_5_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(6, 5),
SelectionGoal::HorizontalPosition(col_5_x.0)
),
);

let max_point_x =
snapshot.x_for_display_point(DisplayPoint::new(7, 2), &text_layout_details);

// Can't move down off the end
assert_eq!(
down(
&snapshot,
DisplayPoint::new(7, 0),
SelectionGoal::HorizontalPosition(0.0),
false,
&text_layout_details
),
(
DisplayPoint::new(7, 2),
SelectionGoal::HorizontalPosition(max_point_x.0)
),
);
assert_eq!(
down(
&snapshot,
DisplayPoint::new(7, 2),
SelectionGoal::HorizontalPosition(max_point_x.0),
false,
&text_layout_details
),
(
DisplayPoint::new(7, 2),
SelectionGoal::HorizontalPosition(max_point_x.0)
),
);
let mut cx = EditorTestContext::new_multibuffer(cx, ["«abc\ndefg»\n«hijkl\nmn»"]);
cx.update_editor(|editor, cx| {
editor.move_right(&Default::default(), cx);
editor.move_right(&Default::default(), cx)
});
cx.assert_editor_state(indoc! {
"abˇc
defg
hijkl
mn"
});
cx.update_editor(|editor, cx| editor.move_up(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"ˇabc
defg
hijkl
mn"
});
cx.update_editor(|editor, cx| editor.move_up(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"ˇabc
defg
hijkl
mn"
});
cx.update_editor(|editor, cx| editor.move_down(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"abc
ˇdefg
hijkl
mn"
});
cx.update_editor(|editor, cx| editor.move_down(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"abc
defg
ˇhijkl
mn"
});
cx.update_editor(|editor, cx| {
editor.move_to_end_of_line(&Default::default(), cx);
editor.move_up(&Default::default(), cx)
});
cx.assert_editor_state(indoc! {
"abc
defgˇ
hijkl
mn"
});
cx.update_editor(|editor, cx| editor.move_down(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"abc
defg
hijklˇ
mn"
});
cx.update_editor(|editor, cx| editor.move_down(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"abc
defg
hijkl
mnˇ"
});
cx.update_editor(|editor, cx| editor.move_down(&Default::default(), cx));
cx.assert_editor_state(indoc! {
"abc
defg
hijkl
mnˇ"
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn marked_display_snapshot(

let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
let display_map =
cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, 0, cx));
cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, 1, cx));
let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
let markers = markers
.into_iter()
Expand Down
4 changes: 4 additions & 0 deletions crates/editor/src/test/editor_test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl EditorTestContext {
self.multibuffer(|buffer, cx| buffer.snapshot(cx).text())
}

pub fn display_text(&mut self) -> String {
self.update_editor(|editor, cx| editor.display_text(cx))
}

pub fn buffer<F, T>(&mut self, read: F) -> T
where
F: FnOnce(&Buffer, &AppContext) -> T,
Expand Down

0 comments on commit 1e8af58

Please sign in to comment.