From ef42d14b8c8e9e357587e1c3013e67694c1bcc1b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 21 Jul 2021 18:32:32 +0200 Subject: [PATCH] Fix `WrapMap::clip_point` at the end of a soft-wrapped line If that's the case and `Bias` is `Left` we clip to the last character of the soft-wrapped line. Co-Authored-By: Nathan Sobo --- zed/src/editor/display_map.rs | 8 ++++++++ zed/src/editor/display_map/wrap_map.rs | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/zed/src/editor/display_map.rs b/zed/src/editor/display_map.rs index af9e3a529b01ba0e55fd9746504e793748e26764..7047a035a79a7d0223fd85bc170e8ef47283dcd8 100644 --- a/zed/src/editor/display_map.rs +++ b/zed/src/editor/display_map.rs @@ -369,6 +369,14 @@ mod tests { .collect::(), " two \nthree four \nfive\nsix seven \neight" ); + assert_eq!( + snapshot.clip_point(DisplayPoint::new(0, 8), Bias::Left), + DisplayPoint::new(0, 7) + ); + assert_eq!( + snapshot.clip_point(DisplayPoint::new(0, 8), Bias::Right), + DisplayPoint::new(1, 0) + ); buffer.update(&mut cx, |buffer, cx| { let ix = buffer.text().find("seven").unwrap(); diff --git a/zed/src/editor/display_map/wrap_map.rs b/zed/src/editor/display_map/wrap_map.rs index a6098de47e6c008dff59706ff3d9b4fdef425f09..1870f037527c5fa08a01656485544d6652ac353c 100644 --- a/zed/src/editor/display_map/wrap_map.rs +++ b/zed/src/editor/display_map/wrap_map.rs @@ -208,7 +208,16 @@ impl Snapshot { OutputPoint(cursor.sum_start().0 + (point.0 - cursor.seek_start().0)) } - pub fn clip_point(&self, point: OutputPoint, bias: Bias) -> OutputPoint { + pub fn clip_point(&self, mut point: OutputPoint, bias: Bias) -> OutputPoint { + if bias == Bias::Left { + let mut cursor = self.transforms.cursor::(); + cursor.seek(&point, Bias::Right, &()); + let transform = cursor.item().expect("invalid point"); + if !transform.is_isomorphic() { + *point.column_mut() -= 1; + } + } + self.to_output_point(self.input.clip_point(self.to_input_point(point), bias)) } }