From 49e38e6e003c71cb402c42bfbbfb95789b9f6c91 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 9 Mar 2022 20:31:29 -0700 Subject: [PATCH] Eliminate ToFoldPoint trait Just make it a method on FoldMap --- crates/editor/src/display_map.rs | 8 ++-- crates/editor/src/display_map/fold_map.rs | 50 ++++++++++------------- crates/editor/src/display_map/tab_map.rs | 10 ++--- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 053883e938..d36f571308 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -6,7 +6,7 @@ mod wrap_map; use crate::{Anchor, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint}; use block_map::{BlockMap, BlockPoint}; use collections::{HashMap, HashSet}; -use fold_map::{FoldMap, ToFoldPoint as _}; +use fold_map::FoldMap; use gpui::{fonts::FontId, Entity, ModelContext, ModelHandle}; use language::{Point, Subscription as BufferSubscription}; use std::ops::Range; @@ -200,7 +200,7 @@ impl DisplaySnapshot { pub fn prev_line_boundary(&self, mut point: Point) -> (Point, DisplayPoint) { loop { - let mut fold_point = point.to_fold_point(&self.folds_snapshot, Bias::Left); + let mut fold_point = self.folds_snapshot.to_fold_point(point, Bias::Left); *fold_point.column_mut() = 0; point = fold_point.to_buffer_point(&self.folds_snapshot); @@ -216,7 +216,7 @@ impl DisplaySnapshot { pub fn next_line_boundary(&self, mut point: Point) -> (Point, DisplayPoint) { loop { - let mut fold_point = point.to_fold_point(&self.folds_snapshot, Bias::Right); + let mut fold_point = self.folds_snapshot.to_fold_point(point, Bias::Right); *fold_point.column_mut() = self.folds_snapshot.line_len(fold_point.row()); point = fold_point.to_buffer_point(&self.folds_snapshot); @@ -231,7 +231,7 @@ impl DisplaySnapshot { } fn point_to_display_point(&self, point: Point, bias: Bias) -> DisplayPoint { - let fold_point = point.to_fold_point(&self.folds_snapshot, bias); + let fold_point = self.folds_snapshot.to_fold_point(point, bias); let tab_point = self.tabs_snapshot.to_tab_point(fold_point); let wrap_point = self.wraps_snapshot.from_tab_point(tab_point); let block_point = self.blocks_snapshot.to_block_point(wrap_point); diff --git a/crates/editor/src/display_map/fold_map.rs b/crates/editor/src/display_map/fold_map.rs index 2866ae8f63..daafbee57b 100644 --- a/crates/editor/src/display_map/fold_map.rs +++ b/crates/editor/src/display_map/fold_map.rs @@ -12,10 +12,6 @@ use std::{ }; use sum_tree::{Bias, Cursor, FilterCursor, SumTree}; -pub trait ToFoldPoint { - fn to_fold_point(&self, snapshot: &FoldSnapshot, bias: Bias) -> FoldPoint; -} - #[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)] pub struct FoldPoint(pub super::Point); @@ -75,26 +71,6 @@ impl FoldPoint { } } -impl ToFoldPoint for Point { - fn to_fold_point(&self, snapshot: &FoldSnapshot, bias: Bias) -> FoldPoint { - let mut cursor = snapshot.transforms.cursor::<(Point, FoldPoint)>(); - cursor.seek(self, Bias::Right, &()); - if cursor.item().map_or(false, |t| t.is_fold()) { - if bias == Bias::Left || *self == cursor.start().0 { - cursor.start().1 - } else { - cursor.end(&()).1 - } - } else { - let overshoot = *self - cursor.start().0; - FoldPoint(cmp::min( - cursor.start().1 .0 + overshoot, - cursor.end(&()).1 .0, - )) - } - } -} - pub struct FoldMapWriter<'a>(&'a mut FoldMap); impl<'a> FoldMapWriter<'a> { @@ -554,6 +530,24 @@ impl FoldSnapshot { summary } + pub fn to_fold_point(&self, point: Point, bias: Bias) -> FoldPoint { + let mut cursor = self.transforms.cursor::<(Point, FoldPoint)>(); + cursor.seek(&point, Bias::Right, &()); + if cursor.item().map_or(false, |t| t.is_fold()) { + if bias == Bias::Left || point == cursor.start().0 { + cursor.start().1 + } else { + cursor.end(&()).1 + } + } else { + let overshoot = point - cursor.start().0; + FoldPoint(cmp::min( + cursor.start().1 .0 + overshoot, + cursor.end(&()).1 .0, + )) + } + } + pub fn len(&self) -> FoldOffset { FoldOffset(self.transforms.summary().output.bytes) } @@ -1356,7 +1350,7 @@ mod tests { let buffer_point = fold_point.to_buffer_point(&snapshot); let buffer_offset = buffer_point.to_offset(&buffer_snapshot); assert_eq!( - buffer_point.to_fold_point(&snapshot, Right), + snapshot.to_fold_point(buffer_point, Right), fold_point, "{:?} -> fold point", buffer_point, @@ -1428,10 +1422,8 @@ mod tests { } for fold_range in map.merged_fold_ranges() { - let fold_point = fold_range - .start - .to_point(&buffer_snapshot) - .to_fold_point(&snapshot, Right); + let fold_point = + snapshot.to_fold_point(fold_range.start.to_point(&buffer_snapshot), Right); assert!(snapshot.is_line_folded(fold_point.row())); } diff --git a/crates/editor/src/display_map/tab_map.rs b/crates/editor/src/display_map/tab_map.rs index e2239e7671..dc62783eb3 100644 --- a/crates/editor/src/display_map/tab_map.rs +++ b/crates/editor/src/display_map/tab_map.rs @@ -1,4 +1,4 @@ -use super::fold_map::{self, FoldEdit, FoldPoint, FoldSnapshot, ToFoldPoint}; +use super::fold_map::{self, FoldEdit, FoldPoint, FoldSnapshot}; use crate::MultiBufferSnapshot; use language::{rope, Chunk}; use parking_lot::Mutex; @@ -201,10 +201,6 @@ impl TabSnapshot { TabPoint::new(input.row(), expanded as u32) } - pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint { - self.to_tab_point(point.to_fold_point(&self.fold_snapshot, bias)) - } - pub fn to_fold_point(&self, output: TabPoint, bias: Bias) -> (FoldPoint, usize, usize) { let chars = self.fold_snapshot.chars_at(FoldPoint::new(output.row(), 0)); let expanded = output.column() as usize; @@ -217,6 +213,10 @@ impl TabSnapshot { ) } + pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint { + self.to_tab_point(self.fold_snapshot.to_fold_point(point, bias)) + } + pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point { self.to_fold_point(point, bias) .0