Skip block lines when moving up and down

Antonio Scandurra and Nathan Sobo created

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

Change summary

crates/editor/src/display_map.rs           |  4 +++
crates/editor/src/display_map/block_map.rs |  6 ++++
crates/editor/src/movement.rs              | 32 ++++++++++++++++-------
3 files changed, 32 insertions(+), 10 deletions(-)

Detailed changes

crates/editor/src/display_map.rs 🔗

@@ -302,6 +302,10 @@ impl DisplayMapSnapshot {
         self.folds_snapshot.is_line_folded(tab_point.row())
     }
 
+    pub fn is_block_line(&self, display_row: u32) -> bool {
+        self.blocks_snapshot.is_block_line(display_row)
+    }
+
     pub fn soft_wrap_indent(&self, display_row: u32) -> Option<u32> {
         let wrap_row = self
             .blocks_snapshot

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

@@ -558,6 +558,12 @@ impl BlockSnapshot {
         }
     }
 
+    pub fn is_block_line(&self, row: u32) -> bool {
+        let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
+        cursor.seek(&BlockRow(row), Bias::Right, &());
+        cursor.item().map_or(false, |t| t.block.is_some())
+    }
+
     pub fn clip_point(&self, point: BlockPoint, bias: Bias) -> BlockPoint {
         let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
         cursor.seek(&BlockRow(point.row), Bias::Right, &());

crates/editor/src/movement.rs 🔗

@@ -33,11 +33,17 @@ pub fn up(
         map.column_to_chars(point.row(), point.column())
     };
 
-    if point.row() > 0 {
-        *point.row_mut() -= 1;
-        *point.column_mut() = map.column_from_chars(point.row(), goal_column);
-    } else {
-        point = DisplayPoint::new(0, 0);
+    loop {
+        if point.row() > 0 {
+            *point.row_mut() -= 1;
+            *point.column_mut() = map.column_from_chars(point.row(), goal_column);
+            if !map.is_block_line(point.row()) {
+                break;
+            }
+        } else {
+            point = DisplayPoint::new(0, 0);
+            break;
+        }
     }
 
     let clip_bias = if point.column() == map.line_len(point.row()) {
@@ -64,11 +70,17 @@ pub fn down(
         map.column_to_chars(point.row(), point.column())
     };
 
-    if point.row() < max_point.row() {
-        *point.row_mut() += 1;
-        *point.column_mut() = map.column_from_chars(point.row(), goal_column);
-    } else {
-        point = max_point;
+    loop {
+        if point.row() < max_point.row() {
+            *point.row_mut() += 1;
+            *point.column_mut() = map.column_from_chars(point.row(), goal_column);
+            if !map.is_block_line(point.row()) {
+                break;
+            }
+        } else {
+            point = max_point;
+            break;
+        }
     }
 
     let clip_bias = if point.column() == map.line_len(point.row()) {