From cff610e1ec27bd3e39fce22e61fcc37fbda04b48 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 6 Dec 2021 11:59:32 -0700 Subject: [PATCH] Rename FragmentList to ExcerptList Co-Authored-By: Max Brunsfeld --- .../src/{fragment_list.rs => excerpt_list.rs} | 102 +++++++++--------- crates/language/src/language.rs | 2 +- 2 files changed, 50 insertions(+), 54 deletions(-) rename crates/language/src/{fragment_list.rs => excerpt_list.rs} (84%) diff --git a/crates/language/src/fragment_list.rs b/crates/language/src/excerpt_list.rs similarity index 84% rename from crates/language/src/fragment_list.rs rename to crates/language/src/excerpt_list.rs index 6f16e9ab51..bd47965e10 100644 --- a/crates/language/src/fragment_list.rs +++ b/crates/language/src/excerpt_list.rs @@ -14,10 +14,10 @@ pub trait ToOffset { fn to_offset<'a>(&self, content: &Snapshot) -> usize; } -pub type FragmentId = Location; +pub type ExcerptId = Location; #[derive(Default)] -pub struct FragmentList { +pub struct ExcerptList { snapshot: Mutex, buffers: HashMap, } @@ -25,7 +25,7 @@ pub struct FragmentList { struct BufferState { buffer: ModelHandle, subscription: text::Subscription, - fragments: Vec, + excerpts: Vec, } #[derive(Clone, Default)] @@ -33,7 +33,7 @@ pub struct Snapshot { entries: SumTree, } -pub struct FragmentProperties<'a, T> { +pub struct ExcerptProperties<'a, T> { buffer: &'a ModelHandle, range: Range, header_height: u8, @@ -41,7 +41,7 @@ pub struct FragmentProperties<'a, T> { #[derive(Clone)] struct Entry { - id: FragmentId, + id: ExcerptId, buffer: buffer::Snapshot, buffer_range: Range, text_summary: TextSummary, @@ -50,7 +50,7 @@ struct Entry { #[derive(Clone, Debug, Default)] struct EntrySummary { - fragment_id: FragmentId, + excerpt_id: ExcerptId, text: TextSummary, } @@ -65,7 +65,7 @@ pub struct Chunks<'a> { theme: Option<&'a SyntaxTheme>, } -impl FragmentList { +impl ExcerptList { pub fn new() -> Self { Self::default() } @@ -75,11 +75,7 @@ impl FragmentList { self.snapshot.lock().clone() } - pub fn push( - &mut self, - props: FragmentProperties, - cx: &mut ModelContext, - ) -> FragmentId + pub fn push(&mut self, props: ExcerptProperties, cx: &mut ModelContext) -> ExcerptId where O: text::ToOffset, { @@ -99,7 +95,7 @@ impl FragmentList { let mut snapshot = self.snapshot.lock(); let prev_id = snapshot.entries.last().map(|e| &e.id); - let id = FragmentId::between(prev_id.unwrap_or(&FragmentId::min()), &FragmentId::max()); + let id = ExcerptId::between(prev_id.unwrap_or(&ExcerptId::min()), &ExcerptId::max()); snapshot.entries.push( Entry { id: id.clone(), @@ -118,10 +114,10 @@ impl FragmentList { BufferState { buffer: props.buffer.clone(), subscription, - fragments: Default::default(), + excerpts: Default::default(), } }) - .fragments + .excerpts .push(id.clone()); id @@ -130,32 +126,32 @@ impl FragmentList { fn sync(&self, cx: &AppContext) { let mut snapshot = self.snapshot.lock(); let mut patches = Vec::new(); - let mut fragments_to_edit = Vec::new(); + let mut excerpts_to_edit = Vec::new(); for buffer_state in self.buffers.values() { let patch = buffer_state.subscription.consume(); if !patch.is_empty() { let patch_ix = patches.len(); patches.push(patch); - fragments_to_edit.extend( + excerpts_to_edit.extend( buffer_state - .fragments + .excerpts .iter() - .map(|fragment_id| (&buffer_state.buffer, fragment_id, patch_ix)), + .map(|excerpt_id| (&buffer_state.buffer, excerpt_id, patch_ix)), ) } } - fragments_to_edit.sort_unstable_by_key(|(_, fragment_id, _)| *fragment_id); + excerpts_to_edit.sort_unstable_by_key(|(_, excerpt_id, _)| *excerpt_id); - let old_fragments = mem::take(&mut snapshot.entries); - let mut cursor = old_fragments.cursor::(); - for (buffer, fragment_id, patch_ix) in fragments_to_edit { + let old_excerpts = mem::take(&mut snapshot.entries); + let mut cursor = old_excerpts.cursor::(); + for (buffer, excerpt_id, patch_ix) in excerpts_to_edit { let buffer = buffer.read(cx); snapshot .entries - .push_tree(cursor.slice(fragment_id, Bias::Left, &()), &()); + .push_tree(cursor.slice(excerpt_id, Bias::Left, &()), &()); - let fragment = cursor.item().unwrap(); - let mut new_range = fragment.buffer_range.to_offset(buffer); + let excerpt = cursor.item().unwrap(); + let mut new_range = excerpt.buffer_range.to_offset(buffer); for edit in patches[patch_ix].edits() { let edit_start = edit.new.start; let edit_end = edit.new.start + edit.old_len(); @@ -180,20 +176,20 @@ impl FragmentList { } let mut text_summary: TextSummary = buffer.text_summary_for_range(new_range.clone()); - if fragment.header_height > 0 { + if excerpt.header_height > 0 { text_summary.first_line_chars = 0; - text_summary.lines.row += fragment.header_height as u32; - text_summary.lines_utf16.row += fragment.header_height as u32; - text_summary.bytes += fragment.header_height as usize; + text_summary.lines.row += excerpt.header_height as u32; + text_summary.lines_utf16.row += excerpt.header_height as u32; + text_summary.bytes += excerpt.header_height as usize; } snapshot.entries.push( Entry { - id: fragment.id.clone(), + id: excerpt.id.clone(), buffer: buffer.snapshot(), buffer_range: buffer.anchor_before(new_range.start) ..buffer.anchor_after(new_range.end), text_summary, - header_height: fragment.header_height, + header_height: excerpt.header_height, }, &(), ); @@ -204,7 +200,7 @@ impl FragmentList { } } -impl Entity for FragmentList { +impl Entity for ExcerptList { type Event = (); } @@ -254,7 +250,7 @@ impl sum_tree::Item for Entry { fn summary(&self) -> Self::Summary { EntrySummary { - fragment_id: self.id.clone(), + excerpt_id: self.id.clone(), text: self.text_summary.clone(), } } @@ -264,8 +260,8 @@ impl sum_tree::Summary for EntrySummary { type Context = (); fn add_summary(&mut self, summary: &Self, _: &()) { - debug_assert!(summary.fragment_id > self.fragment_id); - self.fragment_id = summary.fragment_id.clone(); + debug_assert!(summary.excerpt_id > self.excerpt_id); + self.excerpt_id = summary.excerpt_id.clone(); self.text.add_summary(&summary.text, &()); } } @@ -276,10 +272,10 @@ impl<'a> sum_tree::Dimension<'a, EntrySummary> for usize { } } -impl<'a> sum_tree::Dimension<'a, EntrySummary> for FragmentId { +impl<'a> sum_tree::Dimension<'a, EntrySummary> for ExcerptId { fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) { - debug_assert!(summary.fragment_id > *self); - *self = summary.fragment_id.clone(); + debug_assert!(summary.excerpt_id > *self); + *self = summary.excerpt_id.clone(); } } @@ -376,15 +372,15 @@ mod tests { use util::test::sample_text; #[gpui::test] - fn test_fragment_buffer(cx: &mut MutableAppContext) { + fn test_excerpt_buffer(cx: &mut MutableAppContext) { 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 list = cx.add_model(|cx| { - let mut list = FragmentList::new(); + let mut list = ExcerptList::new(); list.push( - FragmentProperties { + ExcerptProperties { buffer: &buffer_1, range: Point::new(1, 2)..Point::new(2, 5), header_height: 2, @@ -392,7 +388,7 @@ mod tests { cx, ); list.push( - FragmentProperties { + ExcerptProperties { buffer: &buffer_1, range: Point::new(3, 3)..Point::new(4, 4), header_height: 1, @@ -400,7 +396,7 @@ mod tests { cx, ); list.push( - FragmentProperties { + ExcerptProperties { buffer: &buffer_2, range: Point::new(3, 1)..Point::new(3, 3), header_height: 3, @@ -464,9 +460,9 @@ mod tests { .unwrap_or(10); let mut buffers: Vec> = Vec::new(); - let list = cx.add_model(|_| FragmentList::new()); - let mut fragment_ids = Vec::new(); - let mut expected_fragments = Vec::new(); + let list = cx.add_model(|_| ExcerptList::new()); + let mut excerpt_ids = Vec::new(); + let mut expected_excerpts = Vec::new(); for _ in 0..operations { match rng.gen_range(0..100) { @@ -489,16 +485,16 @@ mod tests { let header_height = rng.gen_range(0..=5); let anchor_range = buffer.anchor_before(start_ix)..buffer.anchor_after(end_ix); log::info!( - "Pushing fragment for buffer {}: {:?}[{:?}] = {:?}", + "Pushing excerpt for buffer {}: {:?}[{:?}] = {:?}", buffer_handle.id(), buffer.text(), start_ix..end_ix, &buffer.text()[start_ix..end_ix] ); - let fragment_id = list.update(cx, |list, cx| { + let excerpt_id = list.update(cx, |list, cx| { list.push( - FragmentProperties { + ExcerptProperties { buffer: &buffer_handle, range: start_ix..end_ix, header_height, @@ -506,14 +502,14 @@ mod tests { cx, ) }); - fragment_ids.push(fragment_id); - expected_fragments.push((buffer_handle.clone(), anchor_range, header_height)); + excerpt_ids.push(excerpt_id); + expected_excerpts.push((buffer_handle.clone(), anchor_range, header_height)); } } let snapshot = list.read(cx).snapshot(cx); let mut expected_text = String::new(); - for (buffer, range, header_height) in &expected_fragments { + for (buffer, range, header_height) in &expected_excerpts { let buffer = buffer.read(cx); if !expected_text.is_empty() { expected_text.push('\n'); diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index 704e9b967c..de8d5fa893 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -1,5 +1,5 @@ mod buffer; -mod fragment_list; +mod excerpt_list; mod highlight_map; pub mod proto; #[cfg(test)]