Skip to content

search: Escape seeded buffer search query in regex mode - #57748

Merged
smitbarmase merged 2 commits into
zed-industries:mainfrom
fjgbue:fix/escape-regex-seeded-search-query
Jul 24, 2026
Merged

search: Escape seeded buffer search query in regex mode#57748
smitbarmase merged 2 commits into
zed-industries:mainfrom
fjgbue:fix/escape-regex-seeded-search-query

Conversation

@fjgbue

@fjgbue fjgbue commented May 26, 2026

Copy link
Copy Markdown
Contributor

What

When seed_search_query_from_cursor is enabled and regex mode is active, the selected text is used verbatim as the regex pattern. This means selecting text like z.d seeds the regex z.d, where . acts as a wildcard — matching zed, zXd, etc. — rather than only the literal string the user selected.

Fixes #57253.

Why it was broken

search_suggested() in BufferSearchBar retrieves the raw selection text from query_suggestion() and passes it directly to search(). There was no escaping step, even when default_options had SearchOptions::REGEX set.

Fix

Run the seeded suggestion through regex::escape() before passing it to search() when regex mode is enabled. This mirrors the identical pattern already used in crates/vim/src/normal/search.rs:485.

// Before
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });

// After
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        let suggestion = if self.default_options.contains(SearchOptions::REGEX) {
            regex::escape(&suggestion)
        } else {
            suggestion
        };
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });

Test

Added test_seeded_query_is_escaped_in_regex_mode in buffer_search.rs. It creates a buffer with "z.d\nzed\n", enables regex mode, selects z.d, seeds the search, and asserts:

  • The query text becomes z\.d (escaped), not z.d
  • Exactly 1 match is highlighted (the literal z.d), not 2

The regex crate was already a workspace dependency and is already used in the search crate transitively; this adds it explicitly to crates/search/Cargo.toml.

Release Notes:

  • Fixed regex search seeded from cursor/selection matching more than the selected text when the selection contained regex special characters (e.g. ., *, ()

@cla-bot

cla-bot Bot commented May 26, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @12122J on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label May 26, 2026
@fjgbue

fjgbue commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot

cla-bot Bot commented May 26, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @12122J on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented May 26, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@maxdeviant maxdeviant changed the title search: escape regex special characters when seeding query from cursor search: Escape regex special characters when seeding query from cursor May 26, 2026
@fjgbue

fjgbue commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

@cla-bot check

sry about that

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 26, 2026
@cla-bot

cla-bot Bot commented May 26, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@smitbarmase smitbarmase added the area:search buffer search, project search, etc label May 27, 2026
@smitbarmase smitbarmase self-assigned this Jun 1, 2026
@osiewicz osiewicz mentioned this pull request Jul 2, 2026
5 tasks
@cla-bot

cla-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @fjgbue on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot cla-bot Bot removed the cla-signed The user has signed the Contributor License Agreement label Jul 20, 2026

@smitbarmase smitbarmase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Looks like the CLA is on file under your old username, so after the account rename cla-bot can't find it, could you re-sign at https://zed.dev/cla with your current account and then comment @cla-bot check?

@smitbarmase smitbarmase changed the title search: Escape regex special characters when seeding query from cursor search: Escape seeded buffer search query in regex mode Jul 20, 2026
idanov pushed a commit to idanov/zed that referenced this pull request Jul 21, 2026
…ies#61335)

Fixes what zed-industries#57748 does for
buffer search.

When project search is deployed with regex mode enabled and the query is
seeded from the editor's selection or the word under the cursor, that
text is literal, so regex chars in it (e.g. `.` in `z.d`) are now
escaped instead of being interpreted as regex syntax. This follows how
VSCode handles it.

Release Notes:

- Fixed project search queries seeded from the current selection being
interpreted as a regular expression instead of literal text when regex
mode was enabled.

@smitbarmase smitbarmase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pinging again in case you missed: #57748 (review)

@fjgbue

fjgbue commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Hi! Sorry , yes I completely missed it in notifications. @cla-bot check

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Jul 24, 2026
@cla-bot

cla-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

fjgbue and others added 2 commits July 24, 2026 21:03
When `seed_search_query_from_cursor` is enabled and regex mode is active,
the selected text is used verbatim as the regex pattern. This means
selecting text like `z.d` would seed the regex `z.d`, where `.` acts as a
wildcard and matches `zed`, `zXd`, etc. — not what the user selected.

Fix by running the seeded suggestion through `regex::escape` before passing
it to `search()` when `SearchOptions::REGEX` is set in `default_options`.
This mirrors the same pattern already used in `vim/src/normal/search.rs`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@smitbarmase
smitbarmase force-pushed the fix/escape-regex-seeded-search-query branch from 740f013 to ed2b7d5 Compare July 24, 2026 15:37

@smitbarmase smitbarmase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again!

@smitbarmase
smitbarmase enabled auto-merge July 24, 2026 15:38
@smitbarmase
smitbarmase added this pull request to the merge queue Jul 24, 2026
Merged via the queue into zed-industries:main with commit 2d692b4 Jul 24, 2026
36 checks passed
mdz-axo added a commit to mdz-axo/zed-kask that referenced this pull request Jul 25, 2026
Upstream changes (zed-industries/zed main, 27 commits):
- agent: Add agent.compaction_model setting for context compaction (zed-industries#60012)
- agent: Show effort selector for anthropic compatible providers (zed-industries#61579)
- acp: Update agent-client-protocol SDK to 2.0.0 (zed-industries#61570)
- client: Extract proxy handshakes into new proxy_handshake crate (zed-industries#61427)
- collab: Fix multiworkspace location out of sync bugs (zed-industries#61598)
- editor: Fix sticky header drag cancels autoscroll (zed-industries#53592)
- editor: Fix crash when copying and pasting using multiple cursors (zed-industries#61545)
- editor: Skip untitled buffers when saving a multi-buffer (zed-industries#61380)
- gpui: Fix images not being drawn with rounded corners with ObjectFit::Cover (zed-industries#61383)
- gpui: Fix deadlock in performance profiler and reenable it (zed-industries#61584)
- git_ui: Prevent Git panel bindings in repository selector (zed-industries#61282)
- language_model: Add explicit OpenAI conversation compaction and fix Anthropic compaction (zed-industries#61370)
- markdown: Fix squashed Mermaid diagrams in markdown preview (zed-industries#61260)
- Opus 5 BYOK Support (zed-industries#61596)
- repl: Show add-cell controls in empty notebooks (zed-industries#61329)
- search: Escape seeded buffer search query in regex mode (zed-industries#57748)
- settings: Fix VS Code import appending duplicate file associations (zed-industries#61355)
- settings: Split VSCode and Zed keymap files (zed-industries#61532)
- Treat blank spawn_agent session IDs as absent (zed-industries#60893)
- worktree: Reload git state when a watcher rescan covers a repository (zed-industries#61541)
- Plus 7 more minor fixes.

Merge fixes:
- crates/agent/src/thread.rs: replay_tool_call used 'message_ix' (undefined)
  after auto-merge; renamed to 'owning_message_ix' (the parameter name).
- Cargo.toml: Removed stale workspace members hkask-wallet and hkask-git-cas
  (both directories deleted in prior commits but workspace entries remained).
- kask/crates/hkask-regulation/src/wallet_manager.rs: Stubbed consume() and
  settle_rjoules() on WalletBudgetPort — these were API-key encumbrance
  operations from the deleted hkask-wallet crate; regulation tracks per-agent
  gas balances, not per-key encumbrances.
- kask/crates/hkask-regulation/src/wallet_gas_calibrator.rs: Fixed test to
  use crate::agent_wallet_store::WalletStore instead of hkask_storage::WalletStore.
- kask/crates/hkask-regulation/Cargo.toml: Added tokio macros feature to
  dev-dependencies for #[tokio::test].
- kask/crates/kask_bridge/Cargo.toml: Added futures dependency (needed by
  context_injector.rs for futures::executor::block_on).
- kask/crates/kask_bridge/src/context_injector.rs: Fixed futures_util::executor
  to futures::executor (futures-util doesn't include executor module).

Release Notes:

- N/A
0arm pushed a commit to 0arm/zed that referenced this pull request Jul 26, 2026
…es#57748)

## What

When `seed_search_query_from_cursor` is enabled and regex mode is
active, the selected text is used verbatim as the regex pattern. This
means selecting text like `z.d` seeds the regex `z.d`, where `.` acts as
a wildcard — matching `zed`, `zXd`, etc. — rather than only the literal
string the user selected.

Fixes zed-industries#57253.

## Why it was broken

`search_suggested()` in `BufferSearchBar` retrieves the raw selection
text from `query_suggestion()` and passes it directly to `search()`.
There was no escaping step, even when `default_options` had
`SearchOptions::REGEX` set.

## Fix

Run the seeded suggestion through `regex::escape()` before passing it to
`search()` when regex mode is enabled. This mirrors the identical
pattern already used in `crates/vim/src/normal/search.rs:485`.

```rust
// Before
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });

// After
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        let suggestion = if self.default_options.contains(SearchOptions::REGEX) {
            regex::escape(&suggestion)
        } else {
            suggestion
        };
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });
```

## Test

Added `test_seeded_query_is_escaped_in_regex_mode` in
`buffer_search.rs`. It creates a buffer with `"z.d\nzed\n"`, enables
regex mode, selects `z.d`, seeds the search, and asserts:
- The query text becomes `z\.d` (escaped), not `z.d`
- Exactly 1 match is highlighted (the literal `z.d`), not 2

The `regex` crate was already a workspace dependency and is already used
in the `search` crate transitively; this adds it explicitly to
`crates/search/Cargo.toml`.

Release Notes:

- Fixed regex search seeded from cursor/selection matching more than the
selected text when the selection contained regex special characters
(e.g. `.`, `*`, `(`)

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…ies#61335)

Fixes what zed-industries#57748 does for
buffer search.

When project search is deployed with regex mode enabled and the query is
seeded from the editor's selection or the word under the cursor, that
text is literal, so regex chars in it (e.g. `.` in `z.d`) are now
escaped instead of being interpreted as regex syntax. This follows how
VSCode handles it.

Release Notes:

- Fixed project search queries seeded from the current selection being
interpreted as a regular expression instead of literal text when regex
mode was enabled.
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…es#57748)

## What

When `seed_search_query_from_cursor` is enabled and regex mode is
active, the selected text is used verbatim as the regex pattern. This
means selecting text like `z.d` seeds the regex `z.d`, where `.` acts as
a wildcard — matching `zed`, `zXd`, etc. — rather than only the literal
string the user selected.

Fixes zed-industries#57253.

## Why it was broken

`search_suggested()` in `BufferSearchBar` retrieves the raw selection
text from `query_suggestion()` and passes it directly to `search()`.
There was no escaping step, even when `default_options` had
`SearchOptions::REGEX` set.

## Fix

Run the seeded suggestion through `regex::escape()` before passing it to
`search()` when regex mode is enabled. This mirrors the identical
pattern already used in `crates/vim/src/normal/search.rs:485`.

```rust
// Before
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });

// After
let search = self
    .query_suggestion(seed_query_override, window, cx)
    .map(|suggestion| {
        let suggestion = if self.default_options.contains(SearchOptions::REGEX) {
            regex::escape(&suggestion)
        } else {
            suggestion
        };
        self.search(&suggestion, Some(self.default_options), true, window, cx)
    });
```

## Test

Added `test_seeded_query_is_escaped_in_regex_mode` in
`buffer_search.rs`. It creates a buffer with `"z.d\nzed\n"`, enables
regex mode, selects `z.d`, seeds the search, and asserts:
- The query text becomes `z\.d` (escaped), not `z.d`
- Exactly 1 match is highlighted (the literal `z.d`), not 2

The `regex` crate was already a workspace dependency and is already used
in the `search` crate transitively; this adds it explicitly to
`crates/search/Cargo.toml`.

Release Notes:

- Fixed regex search seeded from cursor/selection matching more than the
selected text when the selection contained regex special characters
(e.g. `.`, `*`, `(`)

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:search buffer search, project search, etc cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regex search doesn't escape seeded query

2 participants