Clean up inline assist editor rendering (#15536)
Release Notes: - N/A --------- Co-authored-by: Nathan <nathan@zed.dev> Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
73d8370177
commit
5b1ea7eda0
19 changed files with 267 additions and 1777 deletions
|
@ -35,9 +35,9 @@ pub struct BlockMap {
|
|||
custom_blocks_by_id: TreeMap<CustomBlockId, Arc<CustomBlock>>,
|
||||
transforms: RefCell<SumTree<Transform>>,
|
||||
show_excerpt_controls: bool,
|
||||
buffer_header_height: u8,
|
||||
excerpt_header_height: u8,
|
||||
excerpt_footer_height: u8,
|
||||
buffer_header_height: u32,
|
||||
excerpt_header_height: u32,
|
||||
excerpt_footer_height: u32,
|
||||
}
|
||||
|
||||
pub struct BlockMapReader<'a> {
|
||||
|
@ -52,6 +52,9 @@ pub struct BlockSnapshot {
|
|||
wrap_snapshot: WrapSnapshot,
|
||||
transforms: SumTree<Transform>,
|
||||
custom_blocks_by_id: TreeMap<CustomBlockId, Arc<CustomBlock>>,
|
||||
pub(super) buffer_header_height: u32,
|
||||
pub(super) excerpt_header_height: u32,
|
||||
pub(super) excerpt_footer_height: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
|
@ -77,15 +80,15 @@ pub type RenderBlock = Box<dyn Send + FnMut(&mut BlockContext) -> AnyElement>;
|
|||
pub struct CustomBlock {
|
||||
id: CustomBlockId,
|
||||
position: Anchor,
|
||||
height: u8,
|
||||
height: u32,
|
||||
style: BlockStyle,
|
||||
render: Mutex<RenderBlock>,
|
||||
render: Arc<Mutex<RenderBlock>>,
|
||||
disposition: BlockDisposition,
|
||||
}
|
||||
|
||||
pub struct BlockProperties<P> {
|
||||
pub position: P,
|
||||
pub height: u8,
|
||||
pub height: u32,
|
||||
pub style: BlockStyle,
|
||||
pub render: RenderBlock,
|
||||
pub disposition: BlockDisposition,
|
||||
|
@ -189,14 +192,14 @@ pub enum Block {
|
|||
id: ExcerptId,
|
||||
buffer: BufferSnapshot,
|
||||
range: ExcerptRange<text::Anchor>,
|
||||
height: u8,
|
||||
height: u32,
|
||||
starts_new_buffer: bool,
|
||||
show_excerpt_controls: bool,
|
||||
},
|
||||
ExcerptFooter {
|
||||
id: ExcerptId,
|
||||
disposition: BlockDisposition,
|
||||
height: u8,
|
||||
height: u32,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -231,7 +234,7 @@ impl Block {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn height(&self) -> u8 {
|
||||
pub fn height(&self) -> u32 {
|
||||
match self {
|
||||
Block::Custom(block) => block.height,
|
||||
Block::ExcerptHeader { height, .. } => *height,
|
||||
|
@ -301,9 +304,9 @@ impl BlockMap {
|
|||
pub fn new(
|
||||
wrap_snapshot: WrapSnapshot,
|
||||
show_excerpt_controls: bool,
|
||||
buffer_header_height: u8,
|
||||
excerpt_header_height: u8,
|
||||
excerpt_footer_height: u8,
|
||||
buffer_header_height: u32,
|
||||
excerpt_header_height: u32,
|
||||
excerpt_footer_height: u32,
|
||||
) -> Self {
|
||||
let row_count = wrap_snapshot.max_point().row() + 1;
|
||||
let map = Self {
|
||||
|
@ -336,6 +339,9 @@ impl BlockMap {
|
|||
wrap_snapshot,
|
||||
transforms: self.transforms.borrow().clone(),
|
||||
custom_blocks_by_id: self.custom_blocks_by_id.clone(),
|
||||
buffer_header_height: self.buffer_header_height,
|
||||
excerpt_header_height: self.excerpt_header_height,
|
||||
excerpt_footer_height: self.excerpt_footer_height,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -551,7 +557,7 @@ impl BlockMap {
|
|||
*transforms = new_transforms;
|
||||
}
|
||||
|
||||
pub fn replace_renderers(&mut self, mut renderers: HashMap<CustomBlockId, RenderBlock>) {
|
||||
pub fn replace_blocks(&mut self, mut renderers: HashMap<CustomBlockId, RenderBlock>) {
|
||||
for block in &mut self.custom_blocks {
|
||||
if let Some(render) = renderers.remove(&block.id) {
|
||||
*block.render.lock() = render;
|
||||
|
@ -565,9 +571,9 @@ impl BlockMap {
|
|||
|
||||
pub fn header_and_footer_blocks<'a, 'b: 'a, 'c: 'a + 'b, R, T>(
|
||||
show_excerpt_controls: bool,
|
||||
excerpt_footer_height: u8,
|
||||
buffer_header_height: u8,
|
||||
excerpt_header_height: u8,
|
||||
excerpt_footer_height: u32,
|
||||
buffer_header_height: u32,
|
||||
excerpt_header_height: u32,
|
||||
buffer: &'b multi_buffer::MultiBufferSnapshot,
|
||||
range: R,
|
||||
wrap_snapshot: &'c WrapSnapshot,
|
||||
|
@ -793,7 +799,7 @@ impl<'a> BlockMapWriter<'a> {
|
|||
id,
|
||||
position,
|
||||
height: block.height,
|
||||
render: Mutex::new(block.render),
|
||||
render: Arc::new(Mutex::new(block.render)),
|
||||
disposition: block.disposition,
|
||||
style: block.style,
|
||||
});
|
||||
|
@ -810,24 +816,21 @@ impl<'a> BlockMapWriter<'a> {
|
|||
ids
|
||||
}
|
||||
|
||||
pub fn replace(
|
||||
&mut self,
|
||||
mut heights_and_renderers: HashMap<CustomBlockId, (u8, RenderBlock)>,
|
||||
) {
|
||||
pub fn resize(&mut self, mut heights: HashMap<CustomBlockId, u32>) {
|
||||
let wrap_snapshot = &*self.0.wrap_snapshot.borrow();
|
||||
let buffer = wrap_snapshot.buffer_snapshot();
|
||||
let mut edits = Patch::default();
|
||||
let mut last_block_buffer_row = None;
|
||||
|
||||
for block in &mut self.0.custom_blocks {
|
||||
if let Some((new_height, render)) = heights_and_renderers.remove(&block.id) {
|
||||
if let Some(new_height) = heights.remove(&block.id) {
|
||||
if block.height != new_height {
|
||||
let new_block = CustomBlock {
|
||||
id: block.id,
|
||||
position: block.position,
|
||||
height: new_height,
|
||||
style: block.style,
|
||||
render: Mutex::new(render),
|
||||
render: block.render.clone(),
|
||||
disposition: block.disposition,
|
||||
};
|
||||
let new_block = Arc::new(new_block);
|
||||
|
@ -1174,7 +1177,7 @@ impl Transform {
|
|||
Self {
|
||||
summary: TransformSummary {
|
||||
input_rows: 0,
|
||||
output_rows: block.height() as u32,
|
||||
output_rows: block.height(),
|
||||
},
|
||||
block: Some(block),
|
||||
}
|
||||
|
@ -1445,7 +1448,7 @@ mod tests {
|
|||
.blocks_in_range(0..8)
|
||||
.map(|(start_row, block)| {
|
||||
let block = block.as_custom().unwrap();
|
||||
(start_row..start_row + block.height as u32, block.id)
|
||||
(start_row..start_row + block.height, block.id)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
|
@ -1697,10 +1700,9 @@ mod tests {
|
|||
|
||||
let mut block_map_writer = block_map.write(wraps_snapshot.clone(), Default::default());
|
||||
|
||||
let mut hash_map = HashMap::default();
|
||||
let render: RenderBlock = Box::new(|_| div().into_any());
|
||||
hash_map.insert(block_ids[0], (2_u8, render));
|
||||
block_map_writer.replace(hash_map);
|
||||
let mut new_heights = HashMap::default();
|
||||
new_heights.insert(block_ids[0], 2);
|
||||
block_map_writer.resize(new_heights);
|
||||
let snapshot = block_map.read(wraps_snapshot.clone(), Default::default());
|
||||
assert_eq!(snapshot.text(), "aaa\n\n\n\n\nbbb\nccc\nddd\n\n\n");
|
||||
}
|
||||
|
@ -1708,10 +1710,9 @@ mod tests {
|
|||
{
|
||||
let mut block_map_writer = block_map.write(wraps_snapshot.clone(), Default::default());
|
||||
|
||||
let mut hash_map = HashMap::default();
|
||||
let render: RenderBlock = Box::new(|_| div().into_any());
|
||||
hash_map.insert(block_ids[0], (1_u8, render));
|
||||
block_map_writer.replace(hash_map);
|
||||
let mut new_heights = HashMap::default();
|
||||
new_heights.insert(block_ids[0], 1);
|
||||
block_map_writer.resize(new_heights);
|
||||
|
||||
let snapshot = block_map.read(wraps_snapshot.clone(), Default::default());
|
||||
assert_eq!(snapshot.text(), "aaa\n\n\n\nbbb\nccc\nddd\n\n\n");
|
||||
|
@ -1720,10 +1721,9 @@ mod tests {
|
|||
{
|
||||
let mut block_map_writer = block_map.write(wraps_snapshot.clone(), Default::default());
|
||||
|
||||
let mut hash_map = HashMap::default();
|
||||
let render: RenderBlock = Box::new(|_| div().into_any());
|
||||
hash_map.insert(block_ids[0], (0_u8, render));
|
||||
block_map_writer.replace(hash_map);
|
||||
let mut new_heights = HashMap::default();
|
||||
new_heights.insert(block_ids[0], 0);
|
||||
block_map_writer.resize(new_heights);
|
||||
|
||||
let snapshot = block_map.read(wraps_snapshot.clone(), Default::default());
|
||||
assert_eq!(snapshot.text(), "aaa\n\n\nbbb\nccc\nddd\n\n\n");
|
||||
|
@ -1732,10 +1732,9 @@ mod tests {
|
|||
{
|
||||
let mut block_map_writer = block_map.write(wraps_snapshot.clone(), Default::default());
|
||||
|
||||
let mut hash_map = HashMap::default();
|
||||
let render: RenderBlock = Box::new(|_| div().into_any());
|
||||
hash_map.insert(block_ids[0], (3_u8, render));
|
||||
block_map_writer.replace(hash_map);
|
||||
let mut new_heights = HashMap::default();
|
||||
new_heights.insert(block_ids[0], 3);
|
||||
block_map_writer.resize(new_heights);
|
||||
|
||||
let snapshot = block_map.read(wraps_snapshot.clone(), Default::default());
|
||||
assert_eq!(snapshot.text(), "aaa\n\n\n\n\n\nbbb\nccc\nddd\n\n\n");
|
||||
|
@ -1744,10 +1743,9 @@ mod tests {
|
|||
{
|
||||
let mut block_map_writer = block_map.write(wraps_snapshot.clone(), Default::default());
|
||||
|
||||
let mut hash_map = HashMap::default();
|
||||
let render: RenderBlock = Box::new(|_| div().into_any());
|
||||
hash_map.insert(block_ids[0], (3_u8, render));
|
||||
block_map_writer.replace(hash_map);
|
||||
let mut new_heights = HashMap::default();
|
||||
new_heights.insert(block_ids[0], 3);
|
||||
block_map_writer.resize(new_heights);
|
||||
|
||||
let snapshot = block_map.read(wraps_snapshot.clone(), Default::default());
|
||||
// Same height as before, should remain the same
|
||||
|
@ -2185,17 +2183,17 @@ mod tests {
|
|||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum ExpectedBlock {
|
||||
ExcerptHeader {
|
||||
height: u8,
|
||||
height: u32,
|
||||
starts_new_buffer: bool,
|
||||
},
|
||||
ExcerptFooter {
|
||||
height: u8,
|
||||
height: u32,
|
||||
disposition: BlockDisposition,
|
||||
},
|
||||
Custom {
|
||||
disposition: BlockDisposition,
|
||||
id: CustomBlockId,
|
||||
height: u8,
|
||||
height: u32,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -2214,7 +2212,7 @@ mod tests {
|
|||
}
|
||||
|
||||
impl ExpectedBlock {
|
||||
fn height(&self) -> u8 {
|
||||
fn height(&self) -> u32 {
|
||||
match self {
|
||||
ExpectedBlock::ExcerptHeader { height, .. } => *height,
|
||||
ExpectedBlock::Custom { height, .. } => *height,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue