Skip to content

Commit cc78e4b

Browse files
committed
replace operation for a BlockMapWriter
1 parent 1aabaed commit cc78e4b

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

crates/editor/src/display_map.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,6 @@ impl DisplayMap {
273273
heights_and_renderers: HashMap<BlockId, (Option<u8>, RenderBlock)>,
274274
cx: &mut ModelContext<Self>,
275275
) {
276-
self.block_map.replace(heights_and_renderers);
277-
278276
let snapshot = self.buffer.read(cx).snapshot(cx);
279277
let edits = self.buffer_subscription.consume().into_inner();
280278
let tab_size = Self::tab_size(&self.buffer, cx);
@@ -284,7 +282,8 @@ impl DisplayMap {
284282
let (snapshot, edits) = self
285283
.wrap_map
286284
.update(cx, |map, cx| map.sync(snapshot, edits, cx));
287-
self.block_map.write(snapshot, edits);
285+
let mut block_map = self.block_map.write(snapshot, edits);
286+
block_map.replace(heights_and_renderers);
288287
}
289288

290289
pub fn remove_blocks(&mut self, ids: HashSet<BlockId>, cx: &mut ModelContext<Self>) {

crates/editor/src/display_map/block_map.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,48 @@ impl<'a> BlockMapWriter<'a> {
561561
ids
562562
}
563563

564+
pub fn replace(&mut self, mut heights_and_renderers: HashMap<BlockId, (u8, RenderBlock)>) {
565+
let wrap_snapshot = &*self.0.wrap_snapshot.borrow();
566+
let buffer = wrap_snapshot.buffer_snapshot();
567+
let mut edits = Patch::default();
568+
let mut last_block_buffer_row = None;
569+
570+
for block in &mut self.0.blocks {
571+
if let Some((new_height, render)) = heights_and_renderers.remove(&block.id) {
572+
if block.height != new_height {
573+
let new_block = Block {
574+
id: block.id,
575+
position: block.position,
576+
height: new_height,
577+
style: block.style,
578+
render: Mutex::new(render),
579+
disposition: block.disposition,
580+
};
581+
*block = Arc::new(new_block);
582+
583+
let buffer_row = block.position.to_point(buffer).row;
584+
if last_block_buffer_row != Some(buffer_row) {
585+
last_block_buffer_row = Some(buffer_row);
586+
let wrap_row = wrap_snapshot
587+
.make_wrap_point(Point::new(buffer_row, 0), Bias::Left)
588+
.row();
589+
let start_row =
590+
wrap_snapshot.prev_row_boundary(WrapPoint::new(wrap_row, 0));
591+
let end_row = wrap_snapshot
592+
.next_row_boundary(WrapPoint::new(wrap_row, 0))
593+
.unwrap_or(wrap_snapshot.max_point().row() + 1);
594+
edits.push(Edit {
595+
old: start_row..end_row,
596+
new: start_row..end_row,
597+
})
598+
}
599+
}
600+
}
601+
}
602+
603+
self.0.sync(wrap_snapshot, edits);
604+
}
605+
564606
pub fn remove(&mut self, block_ids: HashSet<BlockId>) {
565607
let wrap_snapshot = &*self.0.wrap_snapshot.borrow();
566608
let buffer = wrap_snapshot.buffer_snapshot();

0 commit comments

Comments
 (0)