Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vim: Update yank, delete to use target_visual_mode for objects #24513

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/vim/src/normal/delete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{motion::Motion, object::Object, Vim};
use crate::{motion::Motion, object::Object, state::Mode, Vim};
use collections::{HashMap, HashSet};
use editor::{
display_map::{DisplaySnapshot, ToDisplayPoint},
Expand Down Expand Up @@ -90,15 +90,29 @@ impl Vim {
cx: &mut Context<Self>,
) {
self.stop_recording(cx);
let target_mode = object.target_visual_mode(self.mode, around);
self.update_editor(window, cx, |vim, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
// Emulates behavior in vim where if we expanded backwards to include a newline
// the cursor gets set back to the start of the line
let mut should_move_to_start: HashSet<_> = Default::default();
let text_layout_details = editor.text_layout_details(window);
editor.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_with(|map, selection| {
object.expand_selection(map, selection, around);
// Use the object's target visual mode to determine selection behavior
if target_mode == Mode::VisualLine {
object.expand_selection(map, selection, around);
Motion::CurrentLine.expand_selection(
map,
selection,
None,
false,
&text_layout_details,
);
} else {
object.expand_selection(map, selection, around);
}
let offset_range = selection.map(|p| p.to_offset(map, Bias::Left)).range();
let mut move_selection_start_to_previous_line =
|map: &DisplaySnapshot, selection: &mut Selection<DisplayPoint>| {
Expand Down Expand Up @@ -148,7 +162,7 @@ impl Vim {
}
});
});
vim.copy_selections_content(editor, false, cx);
vim.copy_selections_content(editor, target_mode == Mode::VisualLine, cx);
editor.insert("", window, cx);

// Fixup cursor position after the deletion
Expand Down
20 changes: 18 additions & 2 deletions crates/vim/src/normal/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,34 @@ impl Vim {
window: &mut Window,
cx: &mut Context<Self>,
) {
let target_mode = object.target_visual_mode(self.mode, around);
self.update_editor(window, cx, |vim, editor, window, cx| {
editor.transact(window, cx, |editor, window, cx| {
editor.set_clip_at_line_ends(false, cx);
let mut original_positions: HashMap<_, _> = Default::default();
let text_layout_details = editor.text_layout_details(window);
editor.change_selections(None, window, cx, |s| {
s.move_with(|map, selection| {
let original_position = (selection.head(), selection.goal);
object.expand_selection(map, selection, around);
// Use the object's target visual mode to determine selection behavior
if target_mode == Mode::VisualLine {
object.expand_selection(map, selection, around);
Motion::CurrentLine.expand_selection(
map,
selection,
None,
false,
&text_layout_details,
);
} else {
object.expand_selection(map, selection, around);
}
original_positions.insert(selection.id, original_position);
});
});
vim.yank_selections_content(editor, false, cx);

vim.yank_selections_content(editor, target_mode == Mode::VisualLine, cx);

editor.change_selections(None, window, cx, |s| {
s.move_with(|_, selection| {
let (head, goal) = original_positions.remove(&selection.id).unwrap();
Expand Down
Loading