Don't toggle WHOLE_WORD in vim search

Conrad Irwin created

Fixes */# in visual mode, and avoids setting up irritating state.

Change summary

Cargo.lock                                      |  1 
assets/keymaps/vim.json                         | 16 +++++++-
crates/vim/Cargo.toml                           |  1 
crates/vim/README.md                            | 36 +++++++++++++++++++
crates/vim/src/normal/search.rs                 | 35 +++++++++++++-----
crates/vim/test_data/test_visual_star_hash.json |  6 +++
6 files changed, 83 insertions(+), 12 deletions(-)

Detailed changes

Cargo.lock πŸ”—

@@ -9079,6 +9079,7 @@ dependencies = [
  "nvim-rs",
  "parking_lot 0.11.2",
  "project",
+ "regex",
  "search",
  "serde",
  "serde_derive",

assets/keymaps/vim.json πŸ”—

@@ -104,8 +104,6 @@
       "shift-v": "vim::ToggleVisualLine",
       "ctrl-v": "vim::ToggleVisualBlock",
       "ctrl-q": "vim::ToggleVisualBlock",
-      "*": "vim::MoveToNext",
-      "#": "vim::MoveToPrev",
       "0": "vim::StartOfLine", // When no number operator present, use start of line motion
       "ctrl-f": "vim::PageDown",
       "pagedown": "vim::PageDown",
@@ -329,6 +327,8 @@
           "backwards": true
         }
       ],
+      "*": "vim::MoveToNext",
+      "#": "vim::MoveToPrev",
       ";": "vim::RepeatFind",
       ",": [
         "vim::RepeatFind",
@@ -421,6 +421,18 @@
       "shift-r": "vim::SubstituteLine",
       "c": "vim::Substitute",
       "~": "vim::ChangeCase",
+      "*": [
+        "vim::MoveToNext",
+        {
+          "partialWord": true
+        }
+      ],
+      "#": [
+        "vim::MoveToPrev",
+        {
+          "partialWord": true
+        }
+      ],
       "ctrl-a": "vim::Increment",
       "ctrl-x": "vim::Decrement",
       "g ctrl-a": [

crates/vim/Cargo.toml πŸ”—

@@ -23,6 +23,7 @@ async-trait = { workspace = true, "optional" = true }
 nvim-rs = { git = "https://github.com/KillTheMule/nvim-rs", branch = "master", features = ["use_tokio"], optional = true }
 tokio = { version = "1.15", "optional" = true }
 serde_json.workspace = true
+regex.workspace = true
 
 collections = { path = "../collections" }
 command_palette = { path = "../command_palette" }

crates/vim/README.md πŸ”—

@@ -0,0 +1,36 @@
+This contains the code for Zed's Vim emulation mode.
+
+Vim mode in Zed is supposed to primarily "do what you expect": it mostly tries to copy vim exactly, but will use Zed-specific functionality when available to make things smoother. This means Zed will never be 100% vim compatible, but should be 100% vim familiar!
+
+The backlog is maintained in the `#vim` channel notes.
+
+## Testing against Neovim
+
+If you are making a change to make Zed's behaviour more closely match vim/nvim, you can create a test using the `NeovimBackedTestContext`.
+
+For example, the following test checks that Zed and Neovim have the same behaviour when running `*` in visual mode:
+
+```rust
+#[gpui::test]
+async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
+    let mut cx = NeovimBackedTestContext::new(cx).await;
+
+    cx.set_shared_state("Λ‡a.c. abcd a.c. abcd").await;
+    cx.simulate_shared_keystrokes(["v", "3", "l", "*"]).await;
+    cx.assert_shared_state("a.c. abcd Λ‡a.c. abcd").await;
+}
+```
+
+To keep CI runs fast, by default the neovim tests use a cached JSON file that records what neovim did (see crates/vim/test_data),
+but while developing this test you'll need to run it with the neovim flag enabled:
+
+```
+cargo test -p vim --features neovim test_visual_star_hash
+```
+
+This will run your keystrokes against a headless neovim and cache the results in the test_data directory.
+
+
+## Testing zed-only behaviour
+
+Zed does more than vim/neovim in their default modes. The `VimTestContext` can be used instead. This lets you test integration with the language server and other parts of zed's UI that don't have a NeoVim equivalent.

crates/vim/src/normal/search.rs πŸ”—

@@ -91,7 +91,6 @@ fn search(workspace: &mut Workspace, action: &Search, cx: &mut ViewContext<Works
 
                     if query.is_empty() {
                         search_bar.set_replacement(None, cx);
-                        search_bar.set_search_options(SearchOptions::CASE_SENSITIVE, cx);
                         search_bar.activate_search_mode(SearchMode::Regex, cx);
                     }
                     vim.workspace_state.search = SearchState {
@@ -149,15 +148,19 @@ pub fn move_to_internal(
         pane.update(cx, |pane, cx| {
             if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
                 let search = search_bar.update(cx, |search_bar, cx| {
-                    let mut options = SearchOptions::CASE_SENSITIVE;
-                    options.set(SearchOptions::WHOLE_WORD, whole_word);
-                    if search_bar.show(cx) {
-                        search_bar
-                            .query_suggestion(cx)
-                            .map(|query| search_bar.search(&query, Some(options), cx))
-                    } else {
-                        None
+                    let options = SearchOptions::CASE_SENSITIVE;
+                    if !search_bar.show(cx) {
+                        return None;
+                    }
+                    let Some(query) = search_bar.query_suggestion(cx) else {
+                        return None;
+                    };
+                    let mut query = regex::escape(&query);
+                    if whole_word {
+                        query = format!(r"\b{}\b", query);
                     }
+                    search_bar.activate_search_mode(SearchMode::Regex, cx);
+                    Some(search_bar.search(&query, Some(options), cx))
                 });
 
                 if let Some(search) = search {
@@ -350,7 +353,10 @@ mod test {
     use editor::DisplayPoint;
     use search::BufferSearchBar;
 
-    use crate::{state::Mode, test::VimTestContext};
+    use crate::{
+        state::Mode,
+        test::{NeovimBackedTestContext, VimTestContext},
+    };
 
     #[gpui::test]
     async fn test_move_to_next(cx: &mut gpui::TestAppContext) {
@@ -474,4 +480,13 @@ mod test {
         cx.simulate_keystrokes(["shift-enter"]);
         cx.assert_editor_state("«oneˇ» one one one");
     }
+
+    #[gpui::test]
+    async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
+        let mut cx = NeovimBackedTestContext::new(cx).await;
+
+        cx.set_shared_state("Λ‡a.c. abcd a.c. abcd").await;
+        cx.simulate_shared_keystrokes(["v", "3", "l", "*"]).await;
+        cx.assert_shared_state("a.c. abcd Λ‡a.c. abcd").await;
+    }
 }