Skip to content

Commit

Permalink
Fix block lookup and the related simple test
Browse files Browse the repository at this point in the history
co-authored-by: Antonio Scandurra <[email protected]>
  • Loading branch information
SomeoneToIgnore and as-cii committed Dec 9, 2024
1 parent caf933e commit 26ba48e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
35 changes: 19 additions & 16 deletions crates/editor/src/display_map/block_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ impl BlockMap {
}
}

println!("item after seeking to start {:?}", cursor.item());
// Decide where the edit ends
// * It should end at a transform boundary
// * Coalesce edits that intersect the same transform
Expand All @@ -575,10 +574,7 @@ impl BlockMap {
loop {
// Seek to the transform starting at or after the end of the edit
cursor.seek(&old_end, Bias::Left, &());
println!("item after seeking to end {:?}", cursor.item());
cursor.next(&());
println!("item after calling next {:?}", cursor.item());

// Extend edit to the end of the discarded transform so it is reconstructed in full
let transform_rows_after_edit = cursor.start().0 - old_end.0;
old_end.0 += transform_rows_after_edit;
Expand Down Expand Up @@ -611,8 +607,6 @@ impl BlockMap {
}
}

dbg!(cursor.item());

// Find the blocks within this edited region.
let new_buffer_start =
wrap_snapshot.to_point(WrapPoint::new(new_start.0, 0), Bias::Left);
Expand Down Expand Up @@ -805,23 +799,24 @@ impl BlockMap {
if let Some(new_buffer_id) = new_buffer_id {
if folded_buffers.contains(&new_buffer_id) {
let mut next_excerpt = excerpt_boundary.next;
let mut wrap_end_row = wrap_row;
while let Some(next_boundary) = boundaries.peek() {
wrap_end_row = wrap_snapshot
.make_wrap_point(Point::new(next_boundary.row.0, 0), Bias::Left)
.row();
let mut buffer_end = Point::new(excerpt_boundary.row.0, 0)
+ next_excerpt.as_ref().unwrap().text_summary.lines;

while let Some(next_boundary) = boundaries.peek() {
if let Some(next_excerpt) = &next_boundary.next {
if next_excerpt.buffer_id != new_buffer_id {
// TODO kb works but seems like a hack
// wrap_end_row = wrap_end_row.saturating_sub(1);
if next_excerpt.buffer_id == new_buffer_id {
buffer_end = Point::new(next_boundary.row.0, 0)
+ next_excerpt.text_summary.lines;
} else {
break;
}
}

next_excerpt = boundaries.next().unwrap().next;
}

let wrap_end_row = wrap_snapshot.make_wrap_point(buffer_end, Bias::Right).row();

return Some((
BlockPlacement::Replace(WrapRow(wrap_row)..WrapRow(wrap_end_row)),
Block::ExcerptBoundary {
Expand Down Expand Up @@ -1257,8 +1252,16 @@ impl<'a> BlockMapWriter<'a> {

let mut edits = Patch::default();
for range in multi_buffer.excerpt_ranges_for_buffer(buffer_id, cx) {
let range = wrap_snapshot.make_wrap_point(range.start, Bias::Left).row()
..wrap_snapshot.make_wrap_point(range.end, Bias::Right).row() + 1;
let last_edit_row = wrap_snapshot.make_wrap_point(range.end, Bias::Right).row()
// make range exclusive
+ 1
// invalidate more, to ensure that the next block's `show_excerpt_controls` are affected
// as that field influences previous buffer's controls too
+ 1;
let last_edit_row = wrap_snapshot
.clip_point(WrapPoint(Point::new(last_edit_row, 0)), Bias::Right)
.row();
let range = wrap_snapshot.make_wrap_point(range.start, Bias::Left).row()..last_edit_row;
edits.push(Edit {
old: range.clone(),
new: range,
Expand Down
46 changes: 18 additions & 28 deletions crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14156,7 +14156,6 @@ async fn test_multi_buffer_folding_with_fewer_excerpts(cx: &mut gpui::TestAppCon
full_text,
);

dbg!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
multi_buffer_editor.update(cx, |editor, cx| {
editor.fold_buffer(buffer_1.read(cx).remote_id(), cx)
});
Expand All @@ -14166,14 +14165,10 @@ async fn test_multi_buffer_folding_with_fewer_excerpts(cx: &mut gpui::TestAppCon
"After folding the first buffer, its text should not be displayed"
);

// TODO kb remove
if true {
return;
}

multi_buffer_editor.update(cx, |editor, cx| {
editor.fold_buffer(buffer_2.read(cx).remote_id(), cx)
});

assert_eq!(
multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
"\n\n\n\n\n\n\n7777\n8888\n9999\n",
Expand All @@ -14189,37 +14184,32 @@ async fn test_multi_buffer_folding_with_fewer_excerpts(cx: &mut gpui::TestAppCon
"After folding the third buffer, its text should not be displayed"
);

// Emulate selection inside the fold logic, that should work
multi_buffer_editor.update(cx, |editor, cx| {
editor.snapshot(cx).next_line_boundary(Point::new(0, 4));
editor.unfold_buffer(buffer_2.read(cx).remote_id(), cx)
});
assert_eq!(
multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
"\n\n\n\n\n4444\n5555\n6666\n\n\n",
"After unfolding the second buffer, its text should be displayed"
);

multi_buffer_editor.update(cx, |editor, cx| {
editor.unfold_buffer(buffer_2.read(cx).remote_id(), cx)
editor.unfold_buffer(buffer_1.read(cx).remote_id(), cx)
});
assert_eq!(
multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
"\n\n\n\n\n4444\n5555\n6666\n\n\n",
"\n\n\n1111\n2222\n3333\n\n\n\n\n4444\n5555\n6666\n\n\n",
"After unfolding the first buffer, its text should be displayed"
);

// TODO kb
// multi_buffer_editor.update(cx, |editor, cx| {
// editor.unfold_buffer(buffer_1.read(cx).remote_id(), cx)
// });
// assert_eq!(
// multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
// "\n\n\naaaa\nbbbb\ncccc\n\n\n\nffff\ngggg\n\n\n\njjjj\n\n\n",
// "After unfolding the first buffer, its text should be displayed"
// );
//
// // multi_buffer_editor.update(cx, |editor, cx| {
// editor.unfold_buffer(buffer_2.read(cx).remote_id(), cx)
// });
// assert_eq!(
// multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
// "\n\n\n\n\nllll\nmmmm\nnnnn\n\n\n\nqqqq\nrrrr\n\n\n\nuuuu\n\n\n",
// "After unfolding the second buffer, all original text should be displayed"
// );
multi_buffer_editor.update(cx, |editor, cx| {
editor.unfold_buffer(buffer_3.read(cx).remote_id(), cx)
});
assert_eq!(
multi_buffer_editor.update(cx, |editor, cx| editor.display_text(cx)),
full_text,
"After unfolding all buffers, all original text should be displayed"
);
}

#[gpui::test]
Expand Down
5 changes: 3 additions & 2 deletions crates/multi_buffer/src/multi_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub struct ExcerptInfo {
pub buffer: BufferSnapshot,
pub buffer_id: BufferId,
pub range: ExcerptRange<text::Anchor>,
pub text_summary: TextSummary,
}

impl std::fmt::Debug for ExcerptInfo {
Expand Down Expand Up @@ -1562,8 +1563,6 @@ impl MultiBuffer {
let excerpt_start = cursor.start().1;
let excerpt_end = excerpt_start + excerpt.text_summary.lines;
Some(excerpt_start..excerpt_end)
// TODO kb
// Some(cursor.start().1..cursor.end(&()).1)
} else {
None
}
Expand Down Expand Up @@ -3581,6 +3580,7 @@ impl MultiBufferSnapshot {
buffer: excerpt.buffer.clone(),
buffer_id: excerpt.buffer_id,
range: excerpt.range.clone(),
text_summary: excerpt.text_summary.clone(),
});

if next.is_none() {
Expand All @@ -3596,6 +3596,7 @@ impl MultiBufferSnapshot {
buffer: prev_excerpt.buffer.clone(),
buffer_id: prev_excerpt.buffer_id,
range: prev_excerpt.range.clone(),
text_summary: prev_excerpt.text_summary.clone(),
});
let row = MultiBufferRow(cursor.start().1.row);

Expand Down

0 comments on commit 26ba48e

Please sign in to comment.