Clarify variable and method names

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2021-05-14 16:42:46 +02:00
co-authored by Nathan Sobo
parent 0d50c74cab
commit 580fc4fed7
2 changed files with 22 additions and 26 deletions
+18 -18
View File
@@ -34,30 +34,30 @@ impl Rope {
} }
pub fn push(&mut self, text: &str) { pub fn push(&mut self, text: &str) {
let mut chunks = SmallVec::<[_; 16]>::new(); let mut new_chunks = SmallVec::<[_; 16]>::new();
let mut chunk = ArrayString::new(); let mut new_chunk = ArrayString::new();
for ch in text.chars() { for ch in text.chars() {
if chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE { if new_chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE {
chunks.push(Chunk(chunk)); new_chunks.push(Chunk(new_chunk));
chunk = ArrayString::new(); new_chunk = ArrayString::new();
} }
chunk.push(ch); new_chunk.push(ch);
} }
if !chunk.is_empty() { if !new_chunk.is_empty() {
chunks.push(Chunk(chunk)); new_chunks.push(Chunk(new_chunk));
} }
let mut chunks = chunks.into_iter(); let mut new_chunks = new_chunks.into_iter();
let mut first_chunk = chunks.next(); let mut first_new_chunk = new_chunks.next();
self.chunks.with_last_mut( self.chunks.update_last(
|last_chunk| { |last_chunk| {
if let Some(chunk) = first_chunk.as_mut() { if let Some(first_new_chunk_ref) = first_new_chunk.as_mut() {
if last_chunk.0.len() + chunk.0.len() <= 2 * CHUNK_BASE { if last_chunk.0.len() + first_new_chunk_ref.0.len() <= 2 * CHUNK_BASE {
last_chunk.0.push_str(&first_chunk.take().unwrap().0); last_chunk.0.push_str(&first_new_chunk.take().unwrap().0);
} else { } else {
let mut text = ArrayString::<[_; 4 * CHUNK_BASE]>::new(); let mut text = ArrayString::<[_; 4 * CHUNK_BASE]>::new();
text.push_str(&last_chunk.0); text.push_str(&last_chunk.0);
text.push_str(&chunk.0); text.push_str(&first_new_chunk_ref.0);
let mut midpoint = text.len() / 2; let mut midpoint = text.len() / 2;
while !text.is_char_boundary(midpoint) { while !text.is_char_boundary(midpoint) {
@@ -66,8 +66,8 @@ impl Rope {
let (left, right) = text.split_at(midpoint); let (left, right) = text.split_at(midpoint);
last_chunk.0.clear(); last_chunk.0.clear();
last_chunk.0.push_str(left); last_chunk.0.push_str(left);
chunk.0.clear(); first_new_chunk_ref.0.clear();
chunk.0.push_str(right); first_new_chunk_ref.0.push_str(right);
} }
} }
}, },
@@ -75,7 +75,7 @@ impl Rope {
); );
self.chunks self.chunks
.extend(first_chunk.into_iter().chain(chunks), &()); .extend(first_new_chunk.into_iter().chain(new_chunks), &());
self.check_invariants(); self.check_invariants();
} }
+4 -8
View File
@@ -101,15 +101,11 @@ impl<T: Item> SumTree<T> {
self.rightmost_leaf().0.items().last() self.rightmost_leaf().0.items().last()
} }
pub fn with_last_mut( pub fn update_last(&mut self, f: impl FnOnce(&mut T), ctx: &<T::Summary as Summary>::Context) {
&mut self, self.update_last_recursive(f, ctx);
f: impl FnOnce(&mut T),
ctx: &<T::Summary as Summary>::Context,
) {
self.with_last_mut_recursive(f, ctx);
} }
fn with_last_mut_recursive( fn update_last_recursive(
&mut self, &mut self,
f: impl FnOnce(&mut T), f: impl FnOnce(&mut T),
ctx: &<T::Summary as Summary>::Context, ctx: &<T::Summary as Summary>::Context,
@@ -123,7 +119,7 @@ impl<T: Item> SumTree<T> {
} => { } => {
let last_summary = child_summaries.last_mut().unwrap(); let last_summary = child_summaries.last_mut().unwrap();
let last_child = child_trees.last_mut().unwrap(); let last_child = child_trees.last_mut().unwrap();
*last_summary = last_child.with_last_mut_recursive(f, ctx).unwrap(); *last_summary = last_child.update_last_recursive(f, ctx).unwrap();
*summary = sum(child_summaries.iter(), ctx); *summary = sum(child_summaries.iter(), ctx);
Some(summary.clone()) Some(summary.clone())
} }