From 74b467aaa855f81506fe67c65174b54216513897 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Thu, 28 Apr 2022 13:46:06 -0700 Subject: [PATCH] tweak editor selection mutating functions --- crates/editor/src/editor.rs | 8 ++++---- crates/vim/src/normal/delete.rs | 16 +++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index aea65029ce..02fbc2537c 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1316,7 +1316,7 @@ impl Editor { pub fn replace_selections_with( &mut self, cx: &mut ViewContext, - find_replacement: impl Fn(&DisplaySnapshot) -> DisplayPoint, + mut find_replacement: impl FnMut(&DisplaySnapshot) -> DisplayPoint, ) { let display_map = self.snapshot(cx); let cursor = find_replacement(&display_map); @@ -1334,7 +1334,7 @@ impl Editor { pub fn move_selections( &mut self, cx: &mut ViewContext, - move_selection: impl Fn(&DisplaySnapshot, &mut Selection), + mut move_selection: impl FnMut(&DisplaySnapshot, &mut Selection), ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let selections = self @@ -1352,7 +1352,7 @@ impl Editor { pub fn move_selection_heads( &mut self, cx: &mut ViewContext, - update_head: impl Fn( + mut update_head: impl FnMut( &DisplaySnapshot, DisplayPoint, SelectionGoal, @@ -1367,7 +1367,7 @@ impl Editor { pub fn move_cursors( &mut self, cx: &mut ViewContext, - update_cursor_position: impl Fn( + mut update_cursor_position: impl FnMut( &DisplaySnapshot, DisplayPoint, SelectionGoal, diff --git a/crates/vim/src/normal/delete.rs b/crates/vim/src/normal/delete.rs index 8516b5ee94..c90c1dbafd 100644 --- a/crates/vim/src/normal/delete.rs +++ b/crates/vim/src/normal/delete.rs @@ -1,29 +1,31 @@ use crate::{motion::Motion, Vim}; +use collections::HashMap; use editor::Bias; use gpui::MutableAppContext; -use language::SelectionGoal; pub fn delete_over(vim: &mut Vim, motion: Motion, cx: &mut MutableAppContext) { vim.update_active_editor(cx, |editor, cx| { editor.transact(cx, |editor, cx| { editor.set_clip_at_line_ends(false, cx); + let mut original_columns: HashMap<_, _> = Default::default(); editor.move_selections(cx, |map, selection| { let original_head = selection.head(); motion.expand_selection(map, selection, true); - selection.goal = SelectionGoal::Column(original_head.column()); + original_columns.insert(selection.id, original_head.column()); }); editor.insert(&"", cx); // Fixup cursor position after the deletion editor.set_clip_at_line_ends(true, cx); - editor.move_cursors(cx, |map, mut cursor, goal| { + editor.move_selections(cx, |map, selection| { + let mut cursor = selection.head(); if motion.linewise() { - if let SelectionGoal::Column(column) = goal { - *cursor.column_mut() = column + if let Some(column) = original_columns.get(&selection.id) { + *cursor.column_mut() = *column } } - - (map.clip_point(cursor, Bias::Left), SelectionGoal::None) + cursor = map.clip_point(cursor, Bias::Left); + selection.collapse_to(cursor, selection.goal) }); }); });