vim: Fix some dw edge cases (#3058)

Release Notes:

- vim: Fix `dw` on the last word of a line, and on empty lines.
This commit is contained in:
Conrad Irwin 2023-09-28 15:32:29 -06:00 committed by GitHub
commit e7ee8a95f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 28 deletions

View file

@ -467,6 +467,22 @@ impl Motion {
(_, selection.end) = map.next_line_boundary(selection.end.to_point(map)); (_, selection.end) = map.next_line_boundary(selection.end.to_point(map));
} else { } else {
// Another special case: When using the "w" motion in combination with an
// operator and the last word moved over is at the end of a line, the end of
// that word becomes the end of the operated text, not the first word in the
// next line.
if let Motion::NextWordStart {
ignore_punctuation: _,
} = self
{
let start_row = selection.start.to_point(&map).row;
if selection.end.to_point(&map).row > start_row {
selection.end =
Point::new(start_row, map.buffer_snapshot.line_len(start_row))
.to_display_point(&map)
}
}
// If the motion is exclusive and the end of the motion is in column 1, the // If the motion is exclusive and the end of the motion is in column 1, the
// end of the motion is moved to the end of the previous line and the motion // end of the motion is moved to the end of the previous line and the motion
// becomes inclusive. Example: "}" moves to the first line after a paragraph, // becomes inclusive. Example: "}" moves to the first line after a paragraph,

View file

@ -76,12 +76,6 @@ pub fn change_object(vim: &mut Vim, object: Object, around: bool, cx: &mut Windo
// word does not include the following white space. {Vi: "cw" when on a blank // word does not include the following white space. {Vi: "cw" when on a blank
// followed by other blanks changes only the first blank; this is probably a // followed by other blanks changes only the first blank; this is probably a
// bug, because "dw" deletes all the blanks} // bug, because "dw" deletes all the blanks}
//
// NOT HANDLED YET
// Another special case: When using the "w" motion in combination with an
// operator and the last word moved over is at the end of a line, the end of
// that word becomes the end of the operated text, not the first word in the
// next line.
fn expand_changed_word_selection( fn expand_changed_word_selection(
map: &DisplaySnapshot, map: &DisplaySnapshot,
selection: &mut Selection<DisplayPoint>, selection: &mut Selection<DisplayPoint>,

View file

@ -2,6 +2,7 @@ use crate::{motion::Motion, object::Object, utils::copy_selections_content, Vim}
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias}; use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
use gpui::WindowContext; use gpui::WindowContext;
use language::Point;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) { pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.stop_recording(); vim.stop_recording();
@ -14,6 +15,27 @@ pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &m
let original_head = selection.head(); let original_head = selection.head();
original_columns.insert(selection.id, original_head.column()); original_columns.insert(selection.id, original_head.column());
motion.expand_selection(map, selection, times, true); motion.expand_selection(map, selection, times, true);
// Motion::NextWordStart on an empty line should delete it.
if let Motion::NextWordStart {
ignore_punctuation: _,
} = motion
{
if selection.is_empty()
&& map
.buffer_snapshot
.line_len(selection.start.to_point(&map).row)
== 0
{
selection.end = map
.buffer_snapshot
.clip_point(
Point::new(selection.start.to_point(&map).row + 1, 0),
Bias::Left,
)
.to_display_point(map)
}
}
}); });
}); });
copy_selections_content(editor, motion.linewise(), cx); copy_selections_content(editor, motion.linewise(), cx);
@ -129,28 +151,44 @@ mod test {
#[gpui::test] #[gpui::test]
async fn test_delete_w(cx: &mut gpui::TestAppContext) { async fn test_delete_w(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await.binding(["d", "w"]); let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert("Teˇst").await; cx.assert_neovim_compatible(
cx.assert("Tˇest test").await; indoc! {"
cx.assert(indoc! {"
Test teˇst
test"})
.await;
cx.assert(indoc! {"
Test tesˇt Test tesˇt
test"}) test"},
.await; ["d", "w"],
cx.assert_exempted( )
.await;
cx.assert_neovim_compatible("Teˇst", ["d", "w"]).await;
cx.assert_neovim_compatible("Tˇest test", ["d", "w"]).await;
cx.assert_neovim_compatible(
indoc! {"
Test teˇst
test"},
["d", "w"],
)
.await;
cx.assert_neovim_compatible(
indoc! {"
Test tesˇt
test"},
["d", "w"],
)
.await;
cx.assert_neovim_compatible(
indoc! {" indoc! {"
Test test Test test
ˇ ˇ
test"}, test"},
ExemptionFeatures::DeleteWordOnEmptyLine, ["d", "w"],
) )
.await; .await;
let mut cx = cx.binding(["d", "shift-w"]); let mut cx = cx.binding(["d", "shift-w"]);
cx.assert("Test teˇst-test test").await; cx.assert_neovim_compatible("Test teˇst-test test", ["d", "shift-w"])
.await;
} }
#[gpui::test] #[gpui::test]

View file

@ -23,8 +23,6 @@ pub enum ExemptionFeatures {
// MOTIONS // MOTIONS
// When an operator completes at the end of the file, an extra newline is left // When an operator completes at the end of the file, an extra newline is left
OperatorLastNewlineRemains, OperatorLastNewlineRemains,
// Deleting a word on an empty line doesn't remove the newline
DeleteWordOnEmptyLine,
// OBJECTS // OBJECTS
// Resulting position after the operation is slightly incorrect for unintuitive reasons. // Resulting position after the operation is slightly incorrect for unintuitive reasons.

View file

@ -20,10 +20,3 @@
{"Key":"0"} {"Key":"0"}
{"Key":"enter"} {"Key":"enter"}
{"Get":{"state":"aa\ndd\nˇcc","mode":"Normal"}} {"Get":{"state":"aa\ndd\nˇcc","mode":"Normal"}}
{"Key":":"}
{"Key":"%"}
{"Key":"s"}
{"Key":"/"}
{"Key":"/"}
{"Key":"/"}
{"Key":"enter"}

View file

@ -1,3 +1,7 @@
{"Put":{"state":"Test tesˇt\n test"}}
{"Key":"d"}
{"Key":"w"}
{"Get":{"state":"Test teˇs\n test","mode":"Normal"}}
{"Put":{"state":"Teˇst"}} {"Put":{"state":"Teˇst"}}
{"Key":"d"} {"Key":"d"}
{"Key":"w"} {"Key":"w"}
@ -14,6 +18,10 @@
{"Key":"d"} {"Key":"d"}
{"Key":"w"} {"Key":"w"}
{"Get":{"state":"Test teˇs\ntest","mode":"Normal"}} {"Get":{"state":"Test teˇs\ntest","mode":"Normal"}}
{"Put":{"state":"Test test\nˇ\ntest"}}
{"Key":"d"}
{"Key":"w"}
{"Get":{"state":"Test test\nˇtest","mode":"Normal"}}
{"Put":{"state":"Test teˇst-test test"}} {"Put":{"state":"Test teˇst-test test"}}
{"Key":"d"} {"Key":"d"}
{"Key":"shift-w"} {"Key":"shift-w"}