Fix minor bug in BlockMap::clip_point

Max Brunsfeld and Nathan Sobo created

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

Change summary

crates/editor/src/display_map/block_map.rs | 45 ++++++++++++++++++++++++
1 file changed, 45 insertions(+)

Detailed changes

crates/editor/src/display_map/block_map.rs 🔗

@@ -452,6 +452,11 @@ impl BlockSnapshot {
     pub fn clip_point(&self, point: BlockPoint, bias: Bias) -> BlockPoint {
         let mut cursor = self.transforms.cursor::<(BlockPoint, WrapPoint)>();
         cursor.seek(&point, Bias::Right, &());
+        if let Some(transform) = cursor.prev_item() {
+            if transform.is_isomorphic() && point == cursor.start().0 {
+                return point;
+            }
+        }
         if let Some(transform) = cursor.item() {
             if transform.is_isomorphic() {
                 let (output_start, input_start) = cursor.start();
@@ -771,6 +776,46 @@ mod tests {
             snapshot.to_block_point(WrapPoint::new(1, 0)),
             BlockPoint::new(3, 0)
         );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(1, 0), Bias::Left),
+            BlockPoint::new(1, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(1, 0), Bias::Right),
+            BlockPoint::new(1, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(1, 1), Bias::Left),
+            BlockPoint::new(1, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(1, 1), Bias::Right),
+            BlockPoint::new(3, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(3, 0), Bias::Left),
+            BlockPoint::new(3, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(3, 0), Bias::Right),
+            BlockPoint::new(3, 0)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(5, 3), Bias::Left),
+            BlockPoint::new(5, 3)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(5, 3), Bias::Right),
+            BlockPoint::new(5, 3)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(6, 0), Bias::Left),
+            BlockPoint::new(5, 3)
+        );
+        assert_eq!(
+            snapshot.clip_point(BlockPoint::new(6, 0), Bias::Right),
+            BlockPoint::new(5, 3)
+        );
 
         // Insert a line break, separating two block decorations into separate
         // lines.