From cd797a5a0d6bfb9af7b7bb9994da1bfc01e3458f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 15 Jul 2021 14:19:58 -0600 Subject: [PATCH] Give the FoldMap its own edit type that does not have deleted_lines We want to use this struct to communicate about edits performed to the fold map itself, and it will be simpler if we don't have to communicate this data. Co-Authored-By: Max Brunsfeld --- zed/src/editor/display_map/fold_map.rs | 37 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 111c99505f..49c8d5308a 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -1,6 +1,6 @@ use super::{ buffer::{AnchorRangeExt, TextSummary}, - Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset, + Anchor, Buffer, DisplayPoint, Point, ToOffset, }; use crate::{ editor::buffer, @@ -70,7 +70,6 @@ impl FoldMap { edits.push(Edit { old_bytes: range.clone(), new_bytes: range.clone(), - ..Default::default() }); } } @@ -116,7 +115,6 @@ impl FoldMap { edits.push(Edit { old_bytes: offset_range.clone(), new_bytes: offset_range, - ..Default::default() }); fold_ixs_to_delete.push(*folds_cursor.start()); folds_cursor.next(&buffer); @@ -147,7 +145,10 @@ impl FoldMap { fn sync(&self, cx: &AppContext) -> MutexGuard> { let buffer = self.buffer.read(cx); - let mut edits = buffer.edits_since(self.last_sync.lock().clone()).peekable(); + let mut edits = buffer + .edits_since(self.last_sync.lock().clone()) + .map(Into::into) + .peekable(); if edits.peek().is_some() { self.apply_edits(edits, cx); } @@ -838,6 +839,34 @@ impl<'a> sum_tree::Dimension<'a, TransformSummary> for usize { } } +struct Edit { + old_bytes: Range, + new_bytes: Range, +} + +impl Edit { + pub fn delta(&self) -> isize { + self.inserted_bytes() as isize - self.deleted_bytes() as isize + } + + pub fn deleted_bytes(&self) -> usize { + self.old_bytes.len() + } + + pub fn inserted_bytes(&self) -> usize { + self.new_bytes.len() + } +} + +impl From for Edit { + fn from(edit: buffer::Edit) -> Self { + Self { + old_bytes: edit.old_bytes, + new_bytes: edit.new_bytes, + } + } +} + #[cfg(test)] mod tests { use super::*;