Rename ExcerptList to MultiBuffer

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-12-08 10:07:15 -08:00
parent 5f8e406c18
commit a7634ccd5f
2 changed files with 38 additions and 38 deletions

View file

@ -1,6 +1,6 @@
mod buffer; mod buffer;
mod excerpt_list;
mod highlight_map; mod highlight_map;
mod multi_buffer;
pub mod proto; pub mod proto;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -17,18 +17,18 @@ const NEWLINES: &'static [u8] = &[b'\n'; u8::MAX as usize];
pub type ExcerptId = Location; pub type ExcerptId = Location;
#[derive(Default)] #[derive(Default)]
pub struct ExcerptList { pub struct MultiBuffer {
snapshot: Mutex<ExcerptListSnapshot>, snapshot: Mutex<MultiBufferSnapshot>,
buffers: HashMap<usize, BufferState>, buffers: HashMap<usize, BufferState>,
subscriptions: Topic, subscriptions: Topic,
} }
pub trait ToOffset { pub trait ToOffset {
fn to_offset<'a>(&self, snapshot: &ExcerptListSnapshot) -> usize; fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize;
} }
pub trait ToPoint { pub trait ToPoint {
fn to_point<'a>(&self, snapshot: &ExcerptListSnapshot) -> Point; fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point;
} }
#[derive(Debug)] #[derive(Debug)]
@ -39,7 +39,7 @@ struct BufferState {
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct ExcerptListSnapshot { pub struct MultiBufferSnapshot {
excerpts: SumTree<Excerpt>, excerpts: SumTree<Excerpt>,
} }
@ -59,7 +59,7 @@ struct Excerpt {
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
struct EntrySummary { struct ExcerptSummary {
excerpt_id: ExcerptId, excerpt_id: ExcerptId,
text: TextSummary, text: TextSummary,
} }
@ -71,16 +71,16 @@ pub struct Chunks<'a> {
range: Range<usize>, range: Range<usize>,
cursor: Cursor<'a, Excerpt, usize>, cursor: Cursor<'a, Excerpt, usize>,
header_height: u8, header_height: u8,
entry_chunks: Option<buffer::BufferChunks<'a>>, excerpt_chunks: Option<buffer::BufferChunks<'a>>,
theme: Option<&'a SyntaxTheme>, theme: Option<&'a SyntaxTheme>,
} }
impl ExcerptList { impl MultiBuffer {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn snapshot(&self, cx: &AppContext) -> ExcerptListSnapshot { pub fn snapshot(&self, cx: &AppContext) -> MultiBufferSnapshot {
self.sync(cx); self.sync(cx);
self.snapshot.lock().clone() self.snapshot.lock().clone()
} }
@ -192,11 +192,11 @@ impl ExcerptList {
} }
} }
impl Entity for ExcerptList { impl Entity for MultiBuffer {
type Event = (); type Event = ();
} }
impl ExcerptListSnapshot { impl MultiBufferSnapshot {
pub fn text(&self) -> String { pub fn text(&self) -> String {
self.chunks(0..self.len(), None) self.chunks(0..self.len(), None)
.map(|chunk| chunk.text) .map(|chunk| chunk.text)
@ -299,7 +299,7 @@ impl ExcerptListSnapshot {
cursor.seek(&range.start, Bias::Right, &()); cursor.seek(&range.start, Bias::Right, &());
let mut header_height: u8 = 0; let mut header_height: u8 = 0;
let entry_chunks = cursor.item().map(|excerpt| { let excerpt_chunks = cursor.item().map(|excerpt| {
let buffer_range = excerpt.range.to_offset(&excerpt.buffer); let buffer_range = excerpt.range.to_offset(&excerpt.buffer);
header_height = excerpt.header_height; header_height = excerpt.header_height;
@ -333,7 +333,7 @@ impl ExcerptListSnapshot {
range, range,
cursor, cursor,
header_height, header_height,
entry_chunks, excerpt_chunks,
theme, theme,
} }
} }
@ -567,17 +567,17 @@ impl Excerpt {
} }
impl sum_tree::Item for Excerpt { impl sum_tree::Item for Excerpt {
type Summary = EntrySummary; type Summary = ExcerptSummary;
fn summary(&self) -> Self::Summary { fn summary(&self) -> Self::Summary {
EntrySummary { ExcerptSummary {
excerpt_id: self.id.clone(), excerpt_id: self.id.clone(),
text: self.text_summary.clone(), text: self.text_summary.clone(),
} }
} }
} }
impl sum_tree::Summary for EntrySummary { impl sum_tree::Summary for ExcerptSummary {
type Context = (); type Context = ();
fn add_summary(&mut self, summary: &Self, _: &()) { fn add_summary(&mut self, summary: &Self, _: &()) {
@ -587,32 +587,32 @@ impl sum_tree::Summary for EntrySummary {
} }
} }
impl<'a> sum_tree::Dimension<'a, EntrySummary> for TextSummary { impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for TextSummary {
fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
*self += &summary.text; *self += &summary.text;
} }
} }
impl<'a> sum_tree::Dimension<'a, EntrySummary> for usize { impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for usize {
fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
*self += summary.text.bytes; *self += summary.text.bytes;
} }
} }
impl<'a> sum_tree::Dimension<'a, EntrySummary> for Point { impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for Point {
fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
*self += summary.text.lines; *self += summary.text.lines;
} }
} }
impl<'a> sum_tree::Dimension<'a, EntrySummary> for PointUtf16 { impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for PointUtf16 {
fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
*self += summary.text.lines_utf16 *self += summary.text.lines_utf16
} }
} }
impl<'a> sum_tree::Dimension<'a, EntrySummary> for Location { impl<'a> sum_tree::Dimension<'a, ExcerptSummary> for Location {
fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { fn add_summary(&mut self, summary: &'a ExcerptSummary, _: &()) {
debug_assert!(summary.excerpt_id > *self); debug_assert!(summary.excerpt_id > *self);
*self = summary.excerpt_id.clone(); *self = summary.excerpt_id.clone();
} }
@ -634,11 +634,11 @@ impl<'a> Iterator for Chunks<'a> {
return Some(chunk); return Some(chunk);
} }
if let Some(entry_chunks) = self.entry_chunks.as_mut() { if let Some(excerpt_chunks) = self.excerpt_chunks.as_mut() {
if let Some(chunk) = entry_chunks.next() { if let Some(chunk) = excerpt_chunks.next() {
return Some(chunk); return Some(chunk);
} }
self.entry_chunks.take(); self.excerpt_chunks.take();
if self.cursor.end(&()) <= self.range.end { if self.cursor.end(&()) <= self.range.end {
return Some(Chunk { return Some(Chunk {
text: "\n", text: "\n",
@ -663,7 +663,7 @@ impl<'a> Iterator for Chunks<'a> {
); );
self.header_height = excerpt.header_height; self.header_height = excerpt.header_height;
self.entry_chunks = Some( self.excerpt_chunks = Some(
excerpt excerpt
.buffer .buffer
.chunks(buffer_range.start..buffer_end, self.theme), .chunks(buffer_range.start..buffer_end, self.theme),
@ -673,32 +673,32 @@ impl<'a> Iterator for Chunks<'a> {
} }
impl ToOffset for Point { impl ToOffset for Point {
fn to_offset<'a>(&self, snapshot: &ExcerptListSnapshot) -> usize { fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_to_offset(*self) snapshot.point_to_offset(*self)
} }
} }
impl ToOffset for PointUtf16 { impl ToOffset for PointUtf16 {
fn to_offset<'a>(&self, snapshot: &ExcerptListSnapshot) -> usize { fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_utf16_to_offset(*self) snapshot.point_utf16_to_offset(*self)
} }
} }
impl ToOffset for usize { impl ToOffset for usize {
fn to_offset<'a>(&self, snapshot: &ExcerptListSnapshot) -> usize { fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
assert!(*self <= snapshot.len(), "offset is out of range"); assert!(*self <= snapshot.len(), "offset is out of range");
*self *self
} }
} }
impl ToPoint for usize { impl ToPoint for usize {
fn to_point<'a>(&self, snapshot: &ExcerptListSnapshot) -> Point { fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
snapshot.offset_to_point(*self) snapshot.offset_to_point(*self)
} }
} }
impl ToPoint for Point { impl ToPoint for Point {
fn to_point<'a>(&self, _: &ExcerptListSnapshot) -> Point { fn to_point<'a>(&self, _: &MultiBufferSnapshot) -> Point {
*self *self
} }
} }
@ -748,7 +748,7 @@ mod tests {
let buffer_1 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'a'), cx)); let buffer_1 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'a'), cx));
let buffer_2 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'g'), cx)); let buffer_2 = cx.add_model(|cx| Buffer::new(0, sample_text(6, 6, 'g'), cx));
let list = cx.add_model(|_| ExcerptList::new()); let list = cx.add_model(|_| MultiBuffer::new());
let subscription = list.update(cx, |list, cx| { let subscription = list.update(cx, |list, cx| {
let subscription = list.subscribe(); let subscription = list.subscribe();
@ -857,7 +857,7 @@ mod tests {
.unwrap_or(10); .unwrap_or(10);
let mut buffers: Vec<ModelHandle<Buffer>> = Vec::new(); let mut buffers: Vec<ModelHandle<Buffer>> = Vec::new();
let list = cx.add_model(|_| ExcerptList::new()); let list = cx.add_model(|_| MultiBuffer::new());
let mut excerpt_ids = Vec::new(); let mut excerpt_ids = Vec::new();
let mut expected_excerpts = Vec::new(); let mut expected_excerpts = Vec::new();
let mut old_versions = Vec::new(); let mut old_versions = Vec::new();