wip - wiring up blockmap into displaymap

This commit is contained in:
Max Brunsfeld 2021-11-12 17:29:09 -08:00
parent c278503166
commit 364fab7b5f
2 changed files with 95 additions and 55 deletions

View file

@ -4,14 +4,18 @@ mod patch;
mod tab_map; mod tab_map;
mod wrap_map; mod wrap_map;
use block_map::{BlockId, BlockMap, BlockPoint, BlockProperties};
use buffer::Rope;
use fold_map::{FoldMap, ToFoldPoint as _}; use fold_map::{FoldMap, ToFoldPoint as _};
use gpui::{fonts::FontId, Entity, ModelContext, ModelHandle}; use gpui::{fonts::FontId, Entity, ModelContext, ModelHandle};
use language::{Anchor, Buffer, Point, ToOffset, ToPoint}; use language::{Anchor, Buffer, Point, ToOffset, ToPoint};
use std::ops::Range; use std::{collections::HashSet, ops::Range};
use sum_tree::Bias; use sum_tree::Bias;
use tab_map::TabMap; use tab_map::TabMap;
use wrap_map::WrapMap; use wrap_map::WrapMap;
pub use wrap_map::{BufferRows, HighlightedChunks};
pub use block_map::HighlightedChunks;
pub use wrap_map::BufferRows;
pub trait ToDisplayPoint { pub trait ToDisplayPoint {
fn to_display_point(&self, map: &DisplayMapSnapshot, bias: Bias) -> DisplayPoint; fn to_display_point(&self, map: &DisplayMapSnapshot, bias: Bias) -> DisplayPoint;
@ -22,6 +26,7 @@ pub struct DisplayMap {
fold_map: FoldMap, fold_map: FoldMap,
tab_map: TabMap, tab_map: TabMap,
wrap_map: ModelHandle<WrapMap>, wrap_map: ModelHandle<WrapMap>,
block_map: BlockMap,
} }
impl Entity for DisplayMap { impl Entity for DisplayMap {
@ -39,28 +44,32 @@ impl DisplayMap {
) -> Self { ) -> Self {
let (fold_map, snapshot) = FoldMap::new(buffer.clone(), cx); let (fold_map, snapshot) = FoldMap::new(buffer.clone(), cx);
let (tab_map, snapshot) = TabMap::new(snapshot, tab_size); let (tab_map, snapshot) = TabMap::new(snapshot, tab_size);
let (wrap_map, _) = WrapMap::new(snapshot, font_id, font_size, wrap_width, cx); let (wrap_map, snapshot) = WrapMap::new(snapshot, font_id, font_size, wrap_width, cx);
let block_map = BlockMap::new(buffer.clone(), snapshot);
cx.observe(&wrap_map, |_, _, cx| cx.notify()).detach(); cx.observe(&wrap_map, |_, _, cx| cx.notify()).detach();
DisplayMap { DisplayMap {
buffer, buffer,
fold_map, fold_map,
tab_map, tab_map,
wrap_map, wrap_map,
block_map,
} }
} }
pub fn snapshot(&self, cx: &mut ModelContext<Self>) -> DisplayMapSnapshot { pub fn snapshot(&self, cx: &mut ModelContext<Self>) -> DisplayMapSnapshot {
let (folds_snapshot, edits) = self.fold_map.read(cx); let (folds_snapshot, edits) = self.fold_map.read(cx);
let (tabs_snapshot, edits) = self.tab_map.sync(folds_snapshot.clone(), edits); let (tabs_snapshot, edits) = self.tab_map.sync(folds_snapshot.clone(), edits);
let (wraps_snapshot, _) = self let (wraps_snapshot, edits) = self
.wrap_map .wrap_map
.update(cx, |map, cx| map.sync(tabs_snapshot.clone(), edits, cx)); .update(cx, |map, cx| map.sync(tabs_snapshot.clone(), edits, cx));
let blocks_snapshot = self.block_map.read(wraps_snapshot.clone(), edits, cx);
DisplayMapSnapshot { DisplayMapSnapshot {
buffer_snapshot: self.buffer.read(cx).snapshot(), buffer_snapshot: self.buffer.read(cx).snapshot(),
folds_snapshot, folds_snapshot,
tabs_snapshot, tabs_snapshot,
wraps_snapshot, wraps_snapshot,
blocks_snapshot,
} }
} }
@ -94,6 +103,34 @@ impl DisplayMap {
.update(cx, |map, cx| map.sync(snapshot, edits, cx)); .update(cx, |map, cx| map.sync(snapshot, edits, cx));
} }
pub fn insert_blocks<P, T>(
&mut self,
blocks: impl IntoIterator<Item = BlockProperties<P, T>>,
cx: &mut ModelContext<Self>,
) -> Vec<BlockId>
where
P: ToOffset + Clone,
T: Into<Rope> + Clone,
{
let (snapshot, edits) = self.fold_map.read(cx);
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
let (snapshot, edits) = self
.wrap_map
.update(cx, |map, cx| map.sync(snapshot, edits, cx));
let mut block_map = self.block_map.write(snapshot, edits, cx);
block_map.insert(blocks, cx)
}
pub fn remove_blocks(&mut self, ids: HashSet<BlockId>, cx: &mut ModelContext<Self>) {
let (snapshot, edits) = self.fold_map.read(cx);
let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
let (snapshot, edits) = self
.wrap_map
.update(cx, |map, cx| map.sync(snapshot, edits, cx));
let mut block_map = self.block_map.write(snapshot, edits, cx);
block_map.remove(ids, cx);
}
pub fn set_font(&self, font_id: FontId, font_size: f32, cx: &mut ModelContext<Self>) { pub fn set_font(&self, font_id: FontId, font_size: f32, cx: &mut ModelContext<Self>) {
self.wrap_map self.wrap_map
.update(cx, |map, cx| map.set_font(font_id, font_size, cx)); .update(cx, |map, cx| map.set_font(font_id, font_size, cx));
@ -115,6 +152,7 @@ pub struct DisplayMapSnapshot {
folds_snapshot: fold_map::Snapshot, folds_snapshot: fold_map::Snapshot,
tabs_snapshot: tab_map::Snapshot, tabs_snapshot: tab_map::Snapshot,
wraps_snapshot: wrap_map::Snapshot, wraps_snapshot: wrap_map::Snapshot,
blocks_snapshot: block_map::BlockSnapshot,
} }
impl DisplayMapSnapshot { impl DisplayMapSnapshot {
@ -162,7 +200,7 @@ impl DisplayMapSnapshot {
} }
pub fn max_point(&self) -> DisplayPoint { pub fn max_point(&self) -> DisplayPoint {
DisplayPoint(self.wraps_snapshot.max_point()) DisplayPoint(self.blocks_snapshot.max_point())
} }
pub fn chunks_at(&self, display_row: u32) -> wrap_map::Chunks { pub fn chunks_at(&self, display_row: u32) -> wrap_map::Chunks {
@ -172,8 +210,8 @@ impl DisplayMapSnapshot {
pub fn highlighted_chunks_for_rows( pub fn highlighted_chunks_for_rows(
&mut self, &mut self,
display_rows: Range<u32>, display_rows: Range<u32>,
) -> wrap_map::HighlightedChunks { ) -> block_map::HighlightedChunks {
self.wraps_snapshot self.blocks_snapshot
.highlighted_chunks_for_rows(display_rows) .highlighted_chunks_for_rows(display_rows)
} }
@ -217,7 +255,7 @@ impl DisplayMapSnapshot {
} }
pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint { pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
DisplayPoint(self.wraps_snapshot.clip_point(point.0, bias)) DisplayPoint(self.blocks_snapshot.clip_point(point.0, bias))
} }
pub fn folds_in_range<'a, T>( pub fn folds_in_range<'a, T>(
@ -235,9 +273,10 @@ impl DisplayMapSnapshot {
} }
pub fn is_line_folded(&self, display_row: u32) -> bool { pub fn is_line_folded(&self, display_row: u32) -> bool {
let wrap_point = DisplayPoint::new(display_row, 0).0; let block_point = BlockPoint(Point::new(display_row, 0));
let row = self.wraps_snapshot.to_tab_point(wrap_point).row(); let wrap_point = self.blocks_snapshot.to_wrap_point(block_point);
self.folds_snapshot.is_line_folded(row) let tab_point = self.wraps_snapshot.to_tab_point(wrap_point);
self.folds_snapshot.is_line_folded(tab_point.row())
} }
pub fn soft_wrap_indent(&self, display_row: u32) -> Option<u32> { pub fn soft_wrap_indent(&self, display_row: u32) -> Option<u32> {
@ -295,11 +334,11 @@ impl DisplayMapSnapshot {
} }
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)] #[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct DisplayPoint(wrap_map::WrapPoint); pub struct DisplayPoint(BlockPoint);
impl DisplayPoint { impl DisplayPoint {
pub fn new(row: u32, column: u32) -> Self { pub fn new(row: u32, column: u32) -> Self {
Self(wrap_map::WrapPoint::new(row, column)) Self(BlockPoint(Point::new(row, column)))
} }
pub fn zero() -> Self { pub fn zero() -> Self {
@ -312,29 +351,31 @@ impl DisplayPoint {
} }
pub fn row(self) -> u32 { pub fn row(self) -> u32 {
self.0.row() self.0.row
} }
pub fn column(self) -> u32 { pub fn column(self) -> u32 {
self.0.column() self.0.column
} }
pub fn row_mut(&mut self) -> &mut u32 { pub fn row_mut(&mut self) -> &mut u32 {
self.0.row_mut() &mut self.0.row
} }
pub fn column_mut(&mut self) -> &mut u32 { pub fn column_mut(&mut self) -> &mut u32 {
self.0.column_mut() &mut self.0.column
} }
pub fn to_buffer_point(self, map: &DisplayMapSnapshot, bias: Bias) -> Point { pub fn to_buffer_point(self, map: &DisplayMapSnapshot, bias: Bias) -> Point {
let unwrapped_point = map.wraps_snapshot.to_tab_point(self.0); let unblocked_point = map.blocks_snapshot.to_wrap_point(self.0);
let unwrapped_point = map.wraps_snapshot.to_tab_point(unblocked_point);
let unexpanded_point = map.tabs_snapshot.to_fold_point(unwrapped_point, bias).0; let unexpanded_point = map.tabs_snapshot.to_fold_point(unwrapped_point, bias).0;
unexpanded_point.to_buffer_point(&map.folds_snapshot) unexpanded_point.to_buffer_point(&map.folds_snapshot)
} }
pub fn to_buffer_offset(self, map: &DisplayMapSnapshot, bias: Bias) -> usize { pub fn to_buffer_offset(self, map: &DisplayMapSnapshot, bias: Bias) -> usize {
let unwrapped_point = map.wraps_snapshot.to_tab_point(self.0); let unblocked_point = map.blocks_snapshot.to_wrap_point(self.0);
let unwrapped_point = map.wraps_snapshot.to_tab_point(unblocked_point);
let unexpanded_point = map.tabs_snapshot.to_fold_point(unwrapped_point, bias).0; let unexpanded_point = map.tabs_snapshot.to_fold_point(unwrapped_point, bias).0;
unexpanded_point.to_buffer_offset(&map.folds_snapshot) unexpanded_point.to_buffer_offset(&map.folds_snapshot)
} }
@ -345,7 +386,8 @@ impl ToDisplayPoint for Point {
let fold_point = self.to_fold_point(&map.folds_snapshot, bias); let fold_point = self.to_fold_point(&map.folds_snapshot, bias);
let tab_point = map.tabs_snapshot.to_tab_point(fold_point); let tab_point = map.tabs_snapshot.to_tab_point(fold_point);
let wrap_point = map.wraps_snapshot.to_wrap_point(tab_point); let wrap_point = map.wraps_snapshot.to_wrap_point(tab_point);
DisplayPoint(wrap_point) let block_point = map.blocks_snapshot.to_block_point(wrap_point);
DisplayPoint(block_point)
} }
} }

View file

@ -16,7 +16,7 @@ use std::{
}; };
use sum_tree::SumTree; use sum_tree::SumTree;
struct BlockMap { pub struct BlockMap {
buffer: ModelHandle<Buffer>, buffer: ModelHandle<Buffer>,
next_block_id: AtomicUsize, next_block_id: AtomicUsize,
wrap_snapshot: Mutex<WrapSnapshot>, wrap_snapshot: Mutex<WrapSnapshot>,
@ -24,18 +24,18 @@ struct BlockMap {
transforms: Mutex<SumTree<Transform>>, transforms: Mutex<SumTree<Transform>>,
} }
struct BlockMapWriter<'a>(&'a mut BlockMap); pub struct BlockMapWriter<'a>(&'a mut BlockMap);
struct BlockSnapshot { pub struct BlockSnapshot {
wrap_snapshot: WrapSnapshot, wrap_snapshot: WrapSnapshot,
transforms: SumTree<Transform>, transforms: SumTree<Transform>,
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
struct BlockId(usize); pub struct BlockId(usize);
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)] #[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct BlockPoint(super::Point); pub struct BlockPoint(pub super::Point);
#[derive(Debug)] #[derive(Debug)]
struct Block { struct Block {
@ -47,7 +47,7 @@ struct Block {
} }
#[derive(Clone)] #[derive(Clone)]
struct BlockProperties<P, T> pub struct BlockProperties<P, T>
where where
P: Clone, P: Clone,
T: Clone, T: Clone,
@ -76,7 +76,7 @@ struct TransformSummary {
output_rows: u32, output_rows: u32,
} }
struct HighlightedChunks<'a> { pub struct HighlightedChunks<'a> {
transforms: sum_tree::Cursor<'a, Transform, (OutputRow, InputRow)>, transforms: sum_tree::Cursor<'a, Transform, (OutputRow, InputRow)>,
input_chunks: wrap_map::HighlightedChunks<'a>, input_chunks: wrap_map::HighlightedChunks<'a>,
input_chunk: HighlightedChunk<'a>, input_chunk: HighlightedChunk<'a>,
@ -100,7 +100,7 @@ struct InputRow(u32);
struct OutputRow(u32); struct OutputRow(u32);
impl BlockMap { impl BlockMap {
fn new(buffer: ModelHandle<Buffer>, wrap_snapshot: WrapSnapshot) -> Self { pub fn new(buffer: ModelHandle<Buffer>, wrap_snapshot: WrapSnapshot) -> Self {
Self { Self {
buffer, buffer,
next_block_id: AtomicUsize::new(0), next_block_id: AtomicUsize::new(0),
@ -113,7 +113,7 @@ impl BlockMap {
} }
} }
fn read( pub fn read(
&self, &self,
wrap_snapshot: WrapSnapshot, wrap_snapshot: WrapSnapshot,
edits: Vec<WrapEdit>, edits: Vec<WrapEdit>,
@ -127,7 +127,7 @@ impl BlockMap {
} }
} }
fn write( pub fn write(
&mut self, &mut self,
wrap_snapshot: WrapSnapshot, wrap_snapshot: WrapSnapshot,
edits: Vec<WrapEdit>, edits: Vec<WrapEdit>,
@ -234,21 +234,17 @@ impl BlockMap {
} }
} }
impl BlockPoint { impl std::ops::Deref for BlockPoint {
fn row(&self) -> u32 { type Target = Point;
self.0.row
}
fn row_mut(&mut self) -> &mut u32 { fn deref(&self) -> &Self::Target {
&mut self.0.row &self.0
} }
}
fn column(&self) -> u32 { impl std::ops::DerefMut for BlockPoint {
self.0.column fn deref_mut(&mut self) -> &mut Self::Target {
} &mut self.0
fn column_mut(&mut self) -> &mut u32 {
&mut self.0.column
} }
} }
@ -308,8 +304,8 @@ impl<'a> BlockMapWriter<'a> {
ids ids
} }
pub fn remove(&self, ids: HashSet<BlockId>) { pub fn remove(&mut self, _: HashSet<BlockId>, _: &AppContext) {
// todo!()
} }
} }
@ -347,9 +343,13 @@ impl BlockSnapshot {
self.to_block_point(self.wrap_snapshot.max_point()) self.to_block_point(self.wrap_snapshot.max_point())
} }
pub fn clip_point(&self, point: BlockPoint, bias: Bias) -> BlockPoint {
todo!()
}
pub fn to_block_point(&self, wrap_point: WrapPoint) -> BlockPoint { pub fn to_block_point(&self, wrap_point: WrapPoint) -> BlockPoint {
let mut cursor = self.transforms.cursor::<(InputRow, OutputRow)>(); let mut cursor = self.transforms.cursor::<(InputRow, OutputRow)>();
cursor.seek(&InputRow(wrap_point.row()), Bias::Left, &()); cursor.seek(&InputRow(wrap_point.row()), Bias::Right, &());
while let Some(item) = cursor.item() { while let Some(item) = cursor.item() {
if item.is_isomorphic() { if item.is_isomorphic() {
break; break;
@ -625,6 +625,10 @@ mod tests {
snapshot.text(), snapshot.text(),
"aaa\nBLOCK 1\nBLOCK 2\nbbb\nccc\nddd\nBLOCK 3\n" "aaa\nBLOCK 1\nBLOCK 2\nbbb\nccc\nddd\nBLOCK 3\n"
); );
assert_eq!(
snapshot.to_block_point(WrapPoint::new(1, 0)),
BlockPoint(Point::new(3, 0))
);
// Insert a line break, separating two block decorations into separate // Insert a line break, separating two block decorations into separate
// lines. // lines.
@ -717,13 +721,8 @@ mod tests {
wrap_map.sync(tabs_snapshot, tab_edits, cx) wrap_map.sync(tabs_snapshot, tab_edits, cx)
}); });
let mut block_map = block_map.write(wraps_snapshot, wrap_edits, cx); let mut block_map = block_map.write(wraps_snapshot, wrap_edits, cx);
let block_ids = block_map.insert(block_properties.clone(), cx);
expected_blocks.extend( expected_blocks.extend(block_ids.into_iter().zip(block_properties));
block_map
.insert(block_properties.clone(), cx)
.into_iter()
.zip(block_properties),
);
} }
40..=59 => { 40..=59 => {
let block_count = rng.gen_range(1..=4.min(expected_blocks.len())); let block_count = rng.gen_range(1..=4.min(expected_blocks.len()));
@ -740,9 +739,8 @@ mod tests {
let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| { let (wraps_snapshot, wrap_edits) = wrap_map.update(cx, |wrap_map, cx| {
wrap_map.sync(tabs_snapshot, tab_edits, cx) wrap_map.sync(tabs_snapshot, tab_edits, cx)
}); });
let block_map = block_map.write(wraps_snapshot, wrap_edits, cx); let mut block_map = block_map.write(wraps_snapshot, wrap_edits, cx);
block_map.remove(block_ids_to_remove, cx);
block_map.remove(block_ids_to_remove);
} }
_ => { _ => {
buffer.update(cx, |buffer, _| buffer.randomly_edit(&mut rng, 5)); buffer.update(cx, |buffer, _| buffer.randomly_edit(&mut rng, 5));