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));
} 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
// 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,

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
// followed by other blanks changes only the first blank; this is probably a
// 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(
map: &DisplaySnapshot,
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 editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Bias};
use gpui::WindowContext;
use language::Point;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
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();
original_columns.insert(selection.id, original_head.column());
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);
@ -129,28 +151,44 @@ mod test {
#[gpui::test]
async fn test_delete_w(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await.binding(["d", "w"]);
cx.assert("Teˇst").await;
cx.assert("Tˇest test").await;
cx.assert(indoc! {"
Test teˇst
test"})
.await;
cx.assert(indoc! {"
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_neovim_compatible(
indoc! {"
Test tesˇt
test"})
.await;
cx.assert_exempted(
test"},
["d", "w"],
)
.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! {"
Test test
ˇ
test"},
ExemptionFeatures::DeleteWordOnEmptyLine,
["d", "w"],
)
.await;
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]

View File

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

View File

@ -20,10 +20,3 @@
{"Key":"0"}
{"Key":"enter"}
{"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"}}
{"Key":"d"}
{"Key":"w"}
@ -14,6 +18,10 @@
{"Key":"d"}
{"Key":"w"}
{"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"}}
{"Key":"d"}
{"Key":"shift-w"}