From 580fc4fed7db2b6ac3cd28c6cdea96cfbdb3606f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 14 May 2021 16:42:46 +0200 Subject: [PATCH] Clarify variable and method names Co-Authored-By: Nathan Sobo --- zed/src/editor/buffer/rope.rs | 36 +++++++++++++++++------------------ zed/src/sum_tree/mod.rs | 12 ++++-------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index 4bd2297c39272930e4fb556e15c167e7e238ea53..724beb37d767e35e9f80605006823ad743f7a6a5 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/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(); } diff --git a/zed/src/sum_tree/mod.rs b/zed/src/sum_tree/mod.rs index 7511da6fc61ee3042a0806382e5b7533970f1ffe..b96b36c49d4444566688c72a470974dffb5dac62 100644 --- a/zed/src/sum_tree/mod.rs +++ b/zed/src/sum_tree/mod.rs @@ -101,15 +101,11 @@ impl SumTree { self.rightmost_leaf().0.items().last() } - pub fn with_last_mut( - &mut self, - f: impl FnOnce(&mut T), - ctx: &::Context, - ) { - self.with_last_mut_recursive(f, ctx); + pub fn update_last(&mut self, f: impl FnOnce(&mut T), ctx: &::Context) { + self.update_last_recursive(f, ctx); } - fn with_last_mut_recursive( + fn update_last_recursive( &mut self, f: impl FnOnce(&mut T), ctx: &::Context, @@ -123,7 +119,7 @@ impl SumTree { } => { 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()) }