WIP - Add excerpt headers as a built-in feature of BlockMap

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-02-08 18:15:45 -08:00
parent 8b1fb9a2cc
commit c7e2fae9cb
3 changed files with 105 additions and 13 deletions

View file

@ -15,6 +15,7 @@ use std::{
cmp, fmt, io,
iter::{self, FromIterator},
ops::{Range, Sub},
path::Path,
str,
sync::Arc,
time::{Duration, Instant},
@ -101,6 +102,14 @@ pub struct ExcerptProperties<'a, T> {
pub range: Range<T>,
}
pub struct ExcerptBoundary {
pub row: u32,
pub buffer: BufferSnapshot,
pub path: Option<Arc<Path>>,
pub range: Range<text::Anchor>,
pub starts_new_buffer: bool,
}
#[derive(Clone)]
struct Excerpt {
id: ExcerptId,
@ -1769,6 +1778,24 @@ impl MultiBufferSnapshot {
start_id != end_id
}
pub fn excerpt_boundaries_in_range<'a, T: ToOffset>(
&'a self,
range: Range<T>,
) -> impl Iterator<Item = ExcerptBoundary> + 'a {
let start = range.start.to_offset(self);
let end = range.end.to_offset(self);
let mut cursor = self.excerpts.cursor::<(usize, Option<&ExcerptId>)>();
cursor.seek(&start, Bias::Right, &());
let prev_buffer_id = cursor.prev_item().map(|excerpt| excerpt.buffer_id);
std::iter::from_fn(move || {
let excerpt = cursor.item()?;
let starts_new_buffer = Some(excerpt.buffer_id) != prev_buffer_id;
todo!()
})
}
pub fn parse_count(&self) -> usize {
self.parse_count
}
@ -2628,6 +2655,17 @@ mod tests {
assert!(!snapshot.range_contains_excerpt_boundary(Point::new(4, 0)..Point::new(4, 2)));
assert!(!snapshot.range_contains_excerpt_boundary(Point::new(4, 2)..Point::new(4, 2)));
assert_eq!(
snapshot
.excerpt_boundaries_in_range(Point::new(0, 0)..Point::new(4, 2))
.collect::<Vec<_>>(),
&[
(Some(buffer_1.clone()), true),
(Some(buffer_1.clone()), false),
(Some(buffer_2.clone()), false),
]
);
buffer_1.update(cx, |buffer, cx| {
buffer.edit(
[