Tidy-up

Conrad Irwin created

Change summary

crates/editor/src/editor.rs                      |   1 
crates/editor/src/movement.rs                    |  16 +-
crates/editor/src/multi_buffer.rs                |   1 
crates/vim/src/test/neovim_connection.rs         |   2 
crates/vim/src/visual.rs                         | 116 ++++++++---------
crates/vim/test_data/test_visual_block_mode.json |   1 
6 files changed, 62 insertions(+), 75 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -2514,7 +2514,6 @@ impl Editor {
     }
 
     pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
-        dbg!("insert!");
         self.insert_with_autoindent_mode(
             text,
             Some(AutoindentMode::Block {

crates/editor/src/movement.rs 🔗

@@ -61,10 +61,10 @@ pub fn up_by_rows(
     goal: SelectionGoal,
     preserve_column_at_start: bool,
 ) -> (DisplayPoint, SelectionGoal) {
-    let mut goal_column = if let SelectionGoal::Column(column) = goal {
-        column
-    } else {
-        map.column_to_chars(start.row(), start.column())
+    let mut goal_column = match goal {
+        SelectionGoal::Column(column) => column,
+        SelectionGoal::ColumnRange { end, .. } => end,
+        _ => map.column_to_chars(start.row(), start.column()),
     };
 
     let prev_row = start.row().saturating_sub(row_count);
@@ -95,10 +95,10 @@ pub fn down_by_rows(
     goal: SelectionGoal,
     preserve_column_at_end: bool,
 ) -> (DisplayPoint, SelectionGoal) {
-    let mut goal_column = if let SelectionGoal::Column(column) = goal {
-        column
-    } else {
-        map.column_to_chars(start.row(), start.column())
+    let mut goal_column = match goal {
+        SelectionGoal::Column(column) => column,
+        SelectionGoal::ColumnRange { end, .. } => end,
+        _ => map.column_to_chars(start.row(), start.column()),
     };
 
     let new_row = start.row() + row_count;

crates/editor/src/multi_buffer.rs 🔗

@@ -364,7 +364,6 @@ impl MultiBuffer {
         S: ToOffset,
         T: Into<Arc<str>>,
     {
-        dbg!("edit", &autoindent_mode);
         if self.buffers.borrow().is_empty() {
             return;
         }

crates/vim/src/test/neovim_connection.rs 🔗

@@ -520,5 +520,5 @@ fn encode_ranges(text: &str, point_ranges: &Vec<Range<Point>>) -> String {
             byte_range
         })
         .collect::<Vec<_>>();
-    util::test::generate_marked_text(text, &byte_ranges[..], true);
+    util::test::generate_marked_text(text, &byte_ranges[..], true)
 }

crates/vim/src/visual.rs 🔗

@@ -129,62 +129,37 @@ pub fn visual_block_motion(
 
         let was_reversed = tail.column() > head.column();
 
-        if !was_reversed && !(head.column() == 0 && head == map.max_point()) {
+        if !was_reversed && !preserve_goal {
             head = movement::saturating_left(map, head);
         }
 
-        let Some((new_head, new_goal)) = move_selection(&map, head, goal) else {
+        let Some((new_head, _)) = move_selection(&map, head, goal) else {
             return
         };
         head = new_head;
-        if goal == SelectionGoal::None {
-            goal = new_goal;
-        }
 
-        let mut is_reversed = tail.column() > head.column();
+        let is_reversed = tail.column() > head.column();
         if was_reversed && !is_reversed {
             tail = movement::left(map, tail)
         } else if !was_reversed && is_reversed {
             tail = movement::right(map, tail)
         }
-        if !is_reversed {
+        if !is_reversed && !preserve_goal {
             head = movement::saturating_right(map, head)
         }
 
-        if !preserve_goal
-            || !matches!(
-                goal,
-                SelectionGoal::ColumnRange { .. } | SelectionGoal::Column(_)
-            )
-        {
-            goal = SelectionGoal::ColumnRange {
-                start: tail.column(),
-                end: head.column(),
-            }
-        }
+        let (start, end) = match goal {
+            SelectionGoal::ColumnRange { start, end } if preserve_goal => (start, end),
+            SelectionGoal::Column(start) if preserve_goal => (start, start + 1),
+            _ => (tail.column(), head.column()),
+        };
+        goal = SelectionGoal::ColumnRange { start, end };
 
-        let mut columns = if let SelectionGoal::ColumnRange { start, end } = goal {
-            if start > end {
-                is_reversed = true;
-                end..start
-            } else {
-                is_reversed = false;
-                start..end
-            }
-        } else if let SelectionGoal::Column(column) = goal {
-            is_reversed = false;
-            column..(column + 1)
+        let columns = if is_reversed {
+            head.column()..tail.column()
         } else {
-            unreachable!()
+            tail.column()..head.column()
         };
-
-        if columns.start >= map.line_len(head.row()) {
-            columns.start = map.line_len(head.row()).saturating_sub(1);
-        }
-        if columns.start >= map.line_len(tail.row()) {
-            columns.start = map.line_len(tail.row()).saturating_sub(1);
-        }
-
         let mut selections = Vec::new();
         let mut row = tail.row();
 
@@ -291,37 +266,39 @@ pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspac
             let mut original_columns: HashMap<_, _> = Default::default();
             let line_mode = editor.selections.line_mode;
 
-            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
-                s.move_with(|map, selection| {
-                    if line_mode {
-                        let mut position = selection.head();
-                        if !selection.reversed {
-                            position = movement::left(map, position);
+            editor.transact(cx, |editor, cx| {
+                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
+                    s.move_with(|map, selection| {
+                        if line_mode {
+                            let mut position = selection.head();
+                            if !selection.reversed {
+                                position = movement::left(map, position);
+                            }
+                            original_columns.insert(selection.id, position.to_point(map).column);
                         }
-                        original_columns.insert(selection.id, position.to_point(map).column);
-                    }
-                    selection.goal = SelectionGoal::None;
+                        selection.goal = SelectionGoal::None;
+                    });
                 });
-            });
-            copy_selections_content(editor, line_mode, cx);
-            editor.insert("", cx);
+                copy_selections_content(editor, line_mode, cx);
+                editor.insert("", cx);
 
-            // Fixup cursor position after the deletion
-            editor.set_clip_at_line_ends(true, cx);
-            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
-                s.move_with(|map, selection| {
-                    let mut cursor = selection.head().to_point(map);
+                // Fixup cursor position after the deletion
+                editor.set_clip_at_line_ends(true, cx);
+                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
+                    s.move_with(|map, selection| {
+                        let mut cursor = selection.head().to_point(map);
 
-                    if let Some(column) = original_columns.get(&selection.id) {
-                        cursor.column = *column
+                        if let Some(column) = original_columns.get(&selection.id) {
+                            cursor.column = *column
+                        }
+                        let cursor = map.clip_point(cursor.to_display_point(map), Bias::Left);
+                        selection.collapse_to(cursor, selection.goal)
+                    });
+                    if vim.state.mode == Mode::VisualBlock {
+                        s.select_anchors(vec![s.first_anchor()])
                     }
-                    let cursor = map.clip_point(cursor.to_display_point(map), Bias::Left);
-                    selection.collapse_to(cursor, selection.goal)
                 });
-                if vim.state.mode == Mode::VisualBlock {
-                    s.select_anchors(vec![s.first_anchor()])
-                }
-            });
+            })
         });
         vim.switch_mode(Mode::Normal, true, cx);
     });
@@ -948,8 +925,19 @@ mod test {
             "
         })
         .await;
-        cx.simulate_shared_keystrokes(["ctrl-v", "down", "down", "down"])
+        cx.simulate_shared_keystrokes(["ctrl-v", "down", "down"])
             .await;
+        cx.assert_shared_state(indoc! {
+            "The«ˇ q»uick
+            bro«ˇwn»
+            foxˇ
+            jumps over the
+
+            lazy dog
+            "
+        })
+        .await;
+        cx.simulate_shared_keystrokes(["down"]).await;
         cx.assert_shared_state(indoc! {
             "The «qˇ»uick
             brow«nˇ»

crates/vim/test_data/test_visual_block_mode.json 🔗

@@ -21,6 +21,7 @@
 {"Key":"ctrl-v"}
 {"Key":"down"}
 {"Key":"down"}
+{"Get":{"state":"The«ˇ q»uick\nbro«ˇwn»\nfoxˇ\njumps over the\n\nlazy dog\n","mode":"VisualBlock"}}
 {"Key":"down"}
 {"Get":{"state":"The «qˇ»uick\nbrow«nˇ»\nfox\njump«sˇ» over the\n\nlazy dog\n","mode":"VisualBlock"}}
 {"Key":"left"}