Skip to content

Implement text-based matching bracket logic for Vim '%' motion to correctly find pairs within comments#45559

Merged
dinocosta merged 2 commits intozed-industries:mainfrom
tacshi:fix/vim_bracket_matching
Jan 8, 2026
Merged

Implement text-based matching bracket logic for Vim '%' motion to correctly find pairs within comments#45559
dinocosta merged 2 commits intozed-industries:mainfrom
tacshi:fix/vim_bracket_matching

Conversation

@tacshi
Copy link
Contributor

@tacshi tacshi commented Dec 23, 2025

Closes #25435

Release Notes:

  • Improved vim's '%' motion to always fall back to text-based bracket matching when language-aware matching fails

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 23, 2025
@tacshi tacshi changed the title Implement text-based matching bracket logic for Vim '%' motion to correctly find pairs within comments. Implement text-based matching bracket logic for Vim '%' motion to correctly find pairs within comments Dec 23, 2025
Copy link
Member

@dinocosta dinocosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tacshi ! 👋

Thank you for tackling this and for including tests, appreciate it! I've tested and reviewed these changes, everything looks good to me.

I've left some suggestions but they're non-blocking so if you don't want to tackle those, let me know and I can merge this! 🙂

…ing `line_range` conversions, and reorganizing variable declarations
Copilot AI review requested due to automatic review settings January 7, 2026 21:14
@tacshi
Copy link
Contributor Author

tacshi commented Jan 7, 2026

@dinocosta
Thanks for the review and the suggestions — good catch! 🙌
I’ve addressed the feedback and pushed the updates. Really appreciate you taking the time to review and test this.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances Vim's '%' motion to support text-based bracket matching as a fallback mechanism when language-based syntax tree matching is unavailable or fails. This improvement ensures that bracket matching works correctly within comments and in buffers without language support, bringing Zed's Vim emulation closer to actual Vim behavior.

Key Changes:

  • Implemented a text-based bracket matching algorithm that searches for and matches brackets using depth counting
  • Modified the matching() function to fall back to text-based matching when syntax-based matching returns no results
  • Added comprehensive test coverage for bracket matching in comments and template-style syntax

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
crates/vim/src/motion.rs Adds text-based bracket matching functions (get_bracket_pair, find_matching_bracket_text_based) and integrates them as fallback logic in the matching() function
crates/vim/test_data/test_percent_in_comment.json Provides 62 test cases covering bracket matching in comments, nested brackets, and template-style patterns like {% %} and {{ }}
crates/vim/src/normal.rs Adds test_percent_in_comment() test function that validates the new behavior against Neovim

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dinocosta dinocosta merged commit 648d1de into zed-industries:main Jan 8, 2026
30 checks passed
baldwindavid added a commit to baldwindavid/zed that referenced this pull request Jan 8, 2026
* main: (349 commits)
  component_preview: Fix license symlink (zed-industries#46379)
  Do not react on already observed buffer edits' versions (zed-industries#46308)
  Fix EP CLI output flicker (zed-industries#46313)
  vim: Fix bug where repeat operator could lead to unrecoverable replaying state (zed-industries#46376)
  vim: Implement text-based matching bracket logic for Vim '%' motion to correctly find pairs within comments (zed-industries#45559)
  Improve LSP button error message (zed-industries#46377)
  agent: Make reject/accept keybindings consistent with restore/stage (zed-industries#46373)
  Enable test-support features for some dev dependencies (zed-industries#46370)
  Capture terminal output when thread is interrupted (zed-industries#46306)
  Inline assistant tools: no more feature flag (zed-industries#46107)
  Add `ep split` subcommand for dataset splitting (zed-industries#46364)
  lsp_button: Fix long LSP version label (zed-industries#46359)
  remote: Introduce a proper mock remote connection (zed-industries#46337)
  ep: Allow matching patches against files without trailing newlines (zed-industries#46357)
  docs: Update "Custom Keybindings for Extension-Based Agents section" to include a troubleshooting note for defining an agent name (zed-industries#46144)
  Fail early if clangd is downloaded on aarch Linux (zed-industries#46346)
  ep: Handle errored requests in Anthropic batches (zed-industries#46351)
  settings_ui: Fix settings search missing results when BM25 finds partial matches (zed-industries#46349)
  workspace: Unpreview active tab when closing other tabs (zed-industries#46294)
  terminal: Skip SHLVL when loading login shell environment (zed-industries#46273)
  ...
rtfeldman pushed a commit that referenced this pull request Jan 8, 2026
…o correctly find pairs within comments (#45559)

Closes #25435

Release Notes:

- Improved vim's '%' motion to always fall back to text-based bracket
matching when language-aware matching fails
rtfeldman pushed a commit that referenced this pull request Jan 9, 2026
…o correctly find pairs within comments (#45559)

Closes #25435

Release Notes:

- Improved vim's '%' motion to always fall back to text-based bracket
matching when language-aware matching fails
@hferreiro
Copy link
Contributor

This PR doesn't appear to work with use_match_quotes: #42615.

@tacshi tacshi deleted the fix/vim_bracket_matching branch January 16, 2026 11:40
@dinocosta
Copy link
Member

This PR doesn't appear to work with use_match_quotes: #42615.

Hey @hferreiro ! Thank you for reporting, I'll take a look at both, as I believe there's some overlap here and we can probably simplify.

I believe the purpose of these changes was also to mimic Vim and NeoVim's behavior, where % does not take into consideration quotes and backticks, so not sure if use_match_quotes should even do anything if the buffer does not have a specified language 🤔

LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
…o correctly find pairs within comments (zed-industries#45559)

Closes zed-industries#25435

Release Notes:

- Improved vim's '%' motion to always fall back to text-based bracket
matching when language-aware matching fails
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
…o correctly find pairs within comments (zed-industries#45559)

Closes zed-industries#25435

Release Notes:

- Improved vim's '%' motion to always fall back to text-based bracket
matching when language-aware matching fails
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Feb 15, 2026
…o correctly find pairs within comments (zed-industries#45559)

Closes zed-industries#25435

Release Notes:

- Improved vim's '%' motion to always fall back to text-based bracket
matching when language-aware matching fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vim '%' matching bracket motion doesnt work inside comments and certain template languages

4 participants