From 694c912201683296ab369a7bd56b2c0e765db82e Mon Sep 17 00:00:00 2001 From: Horam Zarri Date: Wed, 4 Sep 2024 22:56:32 +0330 Subject: [PATCH] vim: Set current line as default sed command scope (#17234) Closes #16977 Release Notes: - added current line as default sed range to match vim's behavior - changed tests accordingly This also simplifies `ReplaceCommand` implementation by changing `Option` to `CommandRange` . --- crates/vim/src/command.rs | 21 +++++++++----- crates/vim/src/normal/search.rs | 28 +++++++++---------- .../vim/test_data/test_command_replace.json | 15 ++++++++-- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/crates/vim/src/command.rs b/crates/vim/src/command.rs index 0ed1f0e1d9..669bf6813e 100644 --- a/crates/vim/src/command.rs +++ b/crates/vim/src/command.rs @@ -656,13 +656,11 @@ pub fn command_interceptor(mut input: &str, cx: &AppContext) -> Option, + pub(crate) range: CommandRange, pub(crate) replacement: Replacement, } @@ -338,20 +338,18 @@ impl Vim { else { return; }; - if let Some(range) = &action.range { - if let Some(result) = self.update_editor(cx, |vim, editor, cx| { - let range = range.buffer_range(vim, editor, cx)?; - let snapshot = &editor.snapshot(cx).buffer_snapshot; - let end_point = Point::new(range.end.0, snapshot.line_len(range.end)); - let range = snapshot.anchor_before(Point::new(range.start.0, 0)) - ..snapshot.anchor_after(end_point); - editor.set_search_within_ranges(&[range], cx); - anyhow::Ok(()) - }) { - workspace.update(cx, |workspace, cx| { - result.notify_err(workspace, cx); - }) - } + if let Some(result) = self.update_editor(cx, |vim, editor, cx| { + let range = action.range.buffer_range(vim, editor, cx)?; + let snapshot = &editor.snapshot(cx).buffer_snapshot; + let end_point = Point::new(range.end.0, snapshot.line_len(range.end)); + let range = snapshot.anchor_before(Point::new(range.start.0, 0)) + ..snapshot.anchor_after(end_point); + editor.set_search_within_ranges(&[range], cx); + anyhow::Ok(()) + }) { + workspace.update(cx, |workspace, cx| { + result.notify_err(workspace, cx); + }) } let vim = cx.view().clone(); pane.update(cx, |pane, cx| { diff --git a/crates/vim/test_data/test_command_replace.json b/crates/vim/test_data/test_command_replace.json index 13928d5c7e..698ef2a3bf 100644 --- a/crates/vim/test_data/test_command_replace.json +++ b/crates/vim/test_data/test_command_replace.json @@ -1,4 +1,4 @@ -{"Put":{"state":"ˇa\nb\nc"}} +{"Put":{"state":"ˇa\nb\nb\nc"}} {"Key":":"} {"Key":"%"} {"Key":"s"} @@ -7,7 +7,7 @@ {"Key":"/"} {"Key":"d"} {"Key":"enter"} -{"Get":{"state":"a\nˇd\nc","mode":"Normal"}} +{"Get":{"state":"a\nd\nˇd\nc","mode":"Normal"}} {"Key":":"} {"Key":"%"} {"Key":"s"} @@ -19,4 +19,13 @@ {"Key":"\\"} {"Key":"0"} {"Key":"enter"} -{"Get":{"state":"aa\ndd\nˇcc","mode":"Normal"}} +{"Get":{"state":"aa\ndd\ndd\nˇcc","mode":"Normal"}} +{"Key":"k"} +{"Key":":"} +{"Key":"s"} +{"Key":"/"} +{"Key":"dd"} +{"Key":"/"} +{"Key":"ee"} +{"Key":"enter"} +{"Get":{"state":"aa\ndd\nˇee\ncc", "mode":"Normal"}} \ No newline at end of file