Fix handling of empty blocks in BlockMap::chunks (#25031)

Closes https://github.com/zed-industries/zed/issues/23391

Release Notes:

- Fixed a bug that sometimes caused incorrect syntax highlighting when
deploying the inline assistant.
This commit is contained in:
Max Brunsfeld 2025-02-17 11:20:19 -08:00 committed by GitHub
parent 69477dfd8c
commit 1e1b637b50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 16 deletions

View file

@ -2070,18 +2070,30 @@ pub mod tests {
) )
}); });
// Insert a block in the middle of a multi-line string literal // Insert two blocks in the middle of a multi-line string literal.
// The second block has zero height.
map.update(cx, |map, cx| { map.update(cx, |map, cx| {
map.insert_blocks( map.insert_blocks(
[BlockProperties { [
placement: BlockPlacement::Below( BlockProperties {
buffer_snapshot.anchor_before(Point::new(1, 0)), placement: BlockPlacement::Below(
), buffer_snapshot.anchor_before(Point::new(1, 0)),
height: 1, ),
style: BlockStyle::Sticky, height: 1,
render: Arc::new(|_| div().into_any()), style: BlockStyle::Sticky,
priority: 0, render: Arc::new(|_| div().into_any()),
}], priority: 0,
},
BlockProperties {
placement: BlockPlacement::Below(
buffer_snapshot.anchor_before(Point::new(2, 0)),
),
height: 0,
style: BlockStyle::Sticky,
render: Arc::new(|_| div().into_any()),
priority: 0,
},
],
cx, cx,
) )
}); });

View file

@ -1721,6 +1721,7 @@ impl BlockSnapshot {
impl<'a> BlockChunks<'a> { impl<'a> BlockChunks<'a> {
/// Go to the next transform /// Go to the next transform
fn advance(&mut self) { fn advance(&mut self) {
self.input_chunk = Chunk::default();
self.transforms.next(&()); self.transforms.next(&());
while let Some(transform) = self.transforms.item() { while let Some(transform) = self.transforms.item() {
if transform if transform
@ -1748,7 +1749,6 @@ impl<'a> BlockChunks<'a> {
); );
self.input_chunks.seek(start_input_row..end_input_row); self.input_chunks.seek(start_input_row..end_input_row);
} }
self.input_chunk = Chunk::default();
} }
} }
} }
@ -1812,9 +1812,6 @@ impl<'a> Iterator for BlockChunks<'a> {
let (mut prefix, suffix) = self.input_chunk.text.split_at(prefix_bytes); let (mut prefix, suffix) = self.input_chunk.text.split_at(prefix_bytes);
self.input_chunk.text = suffix; self.input_chunk.text = suffix;
if self.output_row == transform_end {
self.advance();
}
if self.masked { if self.masked {
// Not great for multibyte text because to keep cursor math correct we // Not great for multibyte text because to keep cursor math correct we
@ -1824,10 +1821,16 @@ impl<'a> Iterator for BlockChunks<'a> {
prefix = &BULLETS[..bullet_len]; prefix = &BULLETS[..bullet_len];
} }
Some(Chunk { let chunk = Chunk {
text: prefix, text: prefix,
..self.input_chunk.clone() ..self.input_chunk.clone()
}) };
if self.output_row == transform_end {
self.advance();
}
Some(chunk)
} }
} }