Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

vim: Fix some dw edge cases

Conrad Irwin768c991909af
diff --git a/crates/vim/src/motion.rs b/crates/vim/src/motion.rs
index fbec1278432..a1971216268 100644
--- a/crates/vim/src/motion.rs
+++ b/crates/vim/src/motion.rs
@@ -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,
diff --git a/crates/vim/src/normal/change.rs b/crates/vim/src/normal/change.rs
index e9f3001392c..6704fb7594b 100644
--- a/crates/vim/src/normal/change.rs
+++ b/crates/vim/src/normal/change.rs
@@ -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>,
diff --git a/crates/vim/src/normal/delete.rs b/crates/vim/src/normal/delete.rs
index 848e9f725d8..517ece80336 100644
--- a/crates/vim/src/normal/delete.rs
+++ b/crates/vim/src/normal/delete.rs
@@ -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! {"
+ let mut cx = NeovimBackedTestContext::new(cx).await;
+ cx.assert_neovim_compatible(
+ indoc! {"
+ Test tesˇt
+ 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"})
- .await;
- cx.assert(indoc! {"
+ test"},
+ ["d", "w"],
+ )
+ .await;
+ cx.assert_neovim_compatible(
+ indoc! {"
Test tesˇt
- test"})
- .await;
- cx.assert_exempted(
+ 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]
diff --git a/crates/vim/src/test/neovim_backed_test_context.rs b/crates/vim/src/test/neovim_backed_test_context.rs
index 53aa536c2f7..c6a77d13eab 100644
--- a/crates/vim/src/test/neovim_backed_test_context.rs
+++ b/crates/vim/src/test/neovim_backed_test_context.rs
@@ -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.
diff --git a/crates/vim/test_data/test_command_replace.json b/crates/vim/test_data/test_command_replace.json
index 91827c0285b..13928d5c7e1 100644
--- a/crates/vim/test_data/test_command_replace.json
+++ b/crates/vim/test_data/test_command_replace.json
@@ -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"}
diff --git a/crates/vim/test_data/test_delete_w.json b/crates/vim/test_data/test_delete_w.json
index c385ccb5469..e5e48ca444a 100644
--- a/crates/vim/test_data/test_delete_w.json
+++ b/crates/vim/test_data/test_delete_w.json
@@ -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"}