Fixup paste locations

This commit is contained in:
Keith Simmons 2022-05-26 11:28:05 -07:00
parent e104cb94e7
commit d11bc2a4b7
4 changed files with 75 additions and 33 deletions

View file

@ -194,6 +194,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
});
}
// Supports non empty selections so it can be bound and called from visual mode
fn paste(_: &mut Workspace, _: &Paste, cx: &mut ViewContext<Workspace>) {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |editor, cx| {
@ -256,6 +257,23 @@ fn paste(_: &mut Workspace, _: &Paste, cx: &mut ViewContext<Workspace>) {
new_selections.push(selection.map(|_| selection_point.clone()));
point..point
} else {
let mut selection = selection.clone();
if !selection.reversed {
let mut adjusted = selection.end;
// Head is at the end of the selection. Adjust the end position to
// to include the character under the cursor.
*adjusted.column_mut() = adjusted.column() + 1;
adjusted = display_map.clip_point(adjusted, Bias::Right);
// If the selection is empty, move both the start and end forward one
// character
if selection.is_empty() {
selection.start = adjusted;
selection.end = adjusted;
} else {
selection.end = adjusted;
}
}
let range = selection.map(|p| p.to_point(&display_map)).range();
new_selections.push(selection.map(|_| range.start.clone()));
range
@ -1141,5 +1159,26 @@ mod test {
The quick brown
the lazy dog
|fox jumps over"});
cx.set_state(
indoc! {"
The quick brown
fox [jump}s over
the lazy dog"},
Mode::Normal,
);
cx.simulate_keystroke("y");
cx.set_state(
indoc! {"
The quick brown
fox jump|s over
the lazy dog"},
Mode::Normal,
);
cx.simulate_keystroke("p");
cx.assert_editor_state(indoc! {"
The quick brown
fox jumps|jumps over
the lazy dog"});
}
}

View file

@ -6,7 +6,7 @@ use workspace::Workspace;
use crate::{motion::Motion, state::Mode, utils::copy_selections_content, Vim};
actions!(vim, [VisualDelete, VisualChange, VisualYank,]);
actions!(vim, [VisualDelete, VisualChange, VisualYank]);
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(change);
@ -55,7 +55,7 @@ pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspac
// Head is at the end of the selection. Adjust the end position to
// to include the character under the cursor.
*selection.end.column_mut() = selection.end.column() + 1;
selection.end = map.clip_point(selection.end, Bias::Left);
selection.end = map.clip_point(selection.end, Bias::Right);
}
if line_mode {
@ -77,6 +77,7 @@ pub fn change(_: &mut Workspace, _: &VisualChange, cx: &mut ViewContext<Workspac
edits.push((range, ""));
new_selections.push(selection.map(|_| anchor.clone()));
}
selection.goal = SelectionGoal::None;
});
});
copy_selections_content(editor, editor.selections.line_mode, cx);
@ -106,6 +107,7 @@ pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspac
*selection.end.column_mut() = selection.end.column() + 1;
selection.end = map.clip_point(selection.end, Bias::Right);
}
selection.goal = SelectionGoal::None;
});
});
copy_selections_content(editor, line_mode, cx);
@ -134,16 +136,18 @@ pub fn yank(_: &mut Workspace, _: &VisualYank, cx: &mut ViewContext<Workspace>)
vim.update_active_editor(cx, |editor, cx| {
editor.set_clip_at_line_ends(false, cx);
let line_mode = editor.selections.line_mode;
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
if !line_mode && !selection.reversed {
// Head is at the end of the selection. Adjust the end position to
// to include the character under the cursor.
*selection.end.column_mut() = selection.end.column() + 1;
selection.end = map.clip_point(selection.end, Bias::Left);
}
if !editor.selections.line_mode {
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
if !selection.reversed {
// Head is at the end of the selection. Adjust the end position to
// to include the character under the cursor.
*selection.end.column_mut() = selection.end.column() + 1;
selection.end = map.clip_point(selection.end, Bias::Right);
}
});
});
});
}
copy_selections_content(editor, line_mode, cx);
editor.change_selections(None, cx, |s| {
s.move_with(|_, selection| {
@ -251,8 +255,8 @@ mod test {
cx.simulate_keystrokes(["j", "p"]);
cx.assert_editor_state(indoc! {"
The ver
the lazy d|quick brown
fox jumps oog"});
the l|quick brown
fox jumps oazy dog"});
cx.assert(
indoc! {"