![]() ### Overview This PR improves the existing [mini.ai‐like](https://github.com/echasnovski/mini.ai) text-object logic for both “AnyQuotes” (quotes) and “AnyBrackets” (brackets) by adding a multi‐line fallback. The first pass searches only the current line for a best match (cover or next); if none are found, we do a multi‐line pass. This preserves mini.ai's usual “line priority” while ensuring we can detect pairs that start on one line and end on another. ### What Changed 1. Brackets - Line-based pass uses `gather_line_brackets(map, caret.row()) `to find bracket pairs `((), [], {}, <>) `on the caret’s line. - If that fails, we call `gather_brackets_multiline(map)` to single‐pass scan the entire buffer, collecting bracket pairs that might span multiple lines. - Finally, we apply the mini.ai “**cover or next**” logic (`pick_best_range`) to choose the best. 2. Quotes - Similar line-based pass with `gather_line_quotes(map, caret.row())`. - If no local quotes found, we do a multi‐line fallback with `gather_quotes_multiline(map)`, building a big string for the whole buffer and using naive regex for "...", '...', and `...`. - Also preserves “inner vs. outer” logic: - For inner (e.g. `ciq`), we skip bounding quotes or brackets if the range is at least 2 characters wide. - For outer (`caq`), we return the entire range. 3. Shared “`finalize`” helpers - `finalize_bracket_range` and `finalize_quote_range` handle the “inner” skip‐chars vs. “outer” logic. - Both rely on the same “line first, then full fallback” approach. ### Why This Matters - **Old Behavior**: If you had multi‐line brackets { ... } or multi‐line quotes spanning multiple lines, they weren’t found at all, since we only scanned line by line. That made text objects like ci{ or ciq fail in multi-line scenarios. - **New Behavior**: We still do a quick line pass (for user‐friendly “line priority”), but now if that fails, we do a single‐pass approach across the entire buffer. This detects multi‐line pairs and maintains mini.ai’s “cover‐or‐next” picking logic. ### Example Use Cases - **Curly braces:** e.g., opening { on line 10, closing } on line 15 → previously missed; now recognized. - **Multi‐line quotes**: e.g., "'Line 1\nLine 2', no longer missed. We do gather_quotes_multiline with a naive regex matching across newlines. ### Tests - Updated and expanded coverage in: - test_anyquotes_object: - Includes a multi-line '...' test case. - E.g. 'first' false\n<caret>string 'second' → ensuring we detect multi‐line quotes. - test_anybrackets_object: - Verifies line‐based priority but also multi‐line bracket detection. - E.g., an open bracket ( on line 3, close ) on line 5, which used to fail. ### Limitations / Future Enhancements - **Escaping**: The current approach for quotes is naive and doesn’t handle escape sequences (like \") or advanced parser logic. For deeper correctness, we’ll need more advanced logic, this is also not supported in the original mini.ai plugin so it is a known issue that won't be attended for now. ### Important Notes - Fix for the bug: https://github.com/zed-industries/zed/issues/23889 this PR addresses that bug specifically for the AnyQuotes text object. Note that the issue still remains in the built-in motions (ci', ci", ci`). - Caret Position Differences: The caret position now slightly deviates from Vim’s default behavior. This is intentional. I aim to closely mimic the mini.ai plugin. Because these text objects are optional (configurable via vim.json), this adjusted behavior is considered acceptable and in my opinion the new behavior is better and it should be the default in vim. Please review the new tests for details and context. - Improved Special Cases: I’ve also refined how “false strings” in the middle and certain curly-bracket scenarios are handled. The test suite reflects these improvements, resulting in a more seamless coding experience overall. ### References: - Mini.AI plugin in nvim: https://github.com/echasnovski/mini.ai Thank you for reviewing these changes! Release Notes: - Improve logic of aq, iq, ab and ib motions to work more like mini.ai plugin |
||
---|---|---|
.. | ||
src | ||
test_data | ||
Cargo.toml | ||
LICENSE-GPL | ||
README.md |
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 behavior 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 behavior when running *
in visual mode:
#[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 behavior
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.