Clarify variable and method names

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

zed/src/editor/buffer/rope.rs | 36 ++++++++++++++++++------------------
zed/src/sum_tree/mod.rs       | 12 ++++--------
2 files changed, 22 insertions(+), 26 deletions(-)

Detailed changes

zed/src/editor/buffer/rope.rs 🔗

@@ -34,30 +34,30 @@ impl Rope {
     }
 
     pub fn push(&mut self, text: &str) {
-        let mut chunks = SmallVec::<[_; 16]>::new();
-        let mut chunk = ArrayString::new();
+        let mut new_chunks = SmallVec::<[_; 16]>::new();
+        let mut new_chunk = ArrayString::new();
         for ch in text.chars() {
-            if chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE {
-                chunks.push(Chunk(chunk));
-                chunk = ArrayString::new();
+            if new_chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE {
+                new_chunks.push(Chunk(new_chunk));
+                new_chunk = ArrayString::new();
             }
-            chunk.push(ch);
+            new_chunk.push(ch);
         }
-        if !chunk.is_empty() {
-            chunks.push(Chunk(chunk));
+        if !new_chunk.is_empty() {
+            new_chunks.push(Chunk(new_chunk));
         }
 
-        let mut chunks = chunks.into_iter();
-        let mut first_chunk = chunks.next();
-        self.chunks.with_last_mut(
+        let mut new_chunks = new_chunks.into_iter();
+        let mut first_new_chunk = new_chunks.next();
+        self.chunks.update_last(
             |last_chunk| {
-                if let Some(chunk) = first_chunk.as_mut() {
-                    if last_chunk.0.len() + chunk.0.len() <= 2 * CHUNK_BASE {
-                        last_chunk.0.push_str(&first_chunk.take().unwrap().0);
+                if let Some(first_new_chunk_ref) = first_new_chunk.as_mut() {
+                    if last_chunk.0.len() + first_new_chunk_ref.0.len() <= 2 * CHUNK_BASE {
+                        last_chunk.0.push_str(&first_new_chunk.take().unwrap().0);
                     } else {
                         let mut text = ArrayString::<[_; 4 * CHUNK_BASE]>::new();
                         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;
                         while !text.is_char_boundary(midpoint) {
@@ -66,8 +66,8 @@ impl Rope {
                         let (left, right) = text.split_at(midpoint);
                         last_chunk.0.clear();
                         last_chunk.0.push_str(left);
-                        chunk.0.clear();
-                        chunk.0.push_str(right);
+                        first_new_chunk_ref.0.clear();
+                        first_new_chunk_ref.0.push_str(right);
                     }
                 }
             },
@@ -75,7 +75,7 @@ impl Rope {
         );
 
         self.chunks
-            .extend(first_chunk.into_iter().chain(chunks), &());
+            .extend(first_new_chunk.into_iter().chain(new_chunks), &());
         self.check_invariants();
     }
 

zed/src/sum_tree/mod.rs 🔗

@@ -101,15 +101,11 @@ impl<T: Item> SumTree<T> {
         self.rightmost_leaf().0.items().last()
     }
 
-    pub fn with_last_mut(
-        &mut self,
-        f: impl FnOnce(&mut T),
-        ctx: &<T::Summary as Summary>::Context,
-    ) {
-        self.with_last_mut_recursive(f, ctx);
+    pub fn update_last(&mut self, f: impl FnOnce(&mut T), ctx: &<T::Summary as Summary>::Context) {
+        self.update_last_recursive(f, ctx);
     }
 
-    fn with_last_mut_recursive(
+    fn update_last_recursive(
         &mut self,
         f: impl FnOnce(&mut T),
         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_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);
                 Some(summary.clone())
             }