# editor: Fix selection occurrence highlights during regex buffer search \(\#52611\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/1ac669d108fbcf9b14bbfa45ed0a2d16e830930e)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 1ac669d108fbcf9b14bbfa45ed0a2d16e830930e

Requested commit: 1ac669d108fbcf9b14bbfa45ed0a2d16e830930e

Commit: 1ac669d108fbcf9b14bbfa45ed0a2d16e830930e

Tree: e04340f1564a38e0fc1b0b6a2c29e944f184e6c2

Author: saberoueslati

Committer: GitHub

## Message

```
editor: Fix selection occurrence highlights during regex buffer search (#52611)

## Context

When using regex buffer search (e.g. `^something`), Zed correctly
navigates only to actual matches. However, navigating to a match selects
the matched text, which triggers the **selection occurrence highlight**
feature. That feature performs a *plain literal* search for the selected
word and highlights all occurrences, including ones that don't satisfy
the regex. The user would see a 4th mid-line occurrence highlighted,
even though `^something` never matched it, this behavior is quite
confusing.

The fix tracks whether the current selection was set by search
navigation via a new `from_search: bool` on `SelectionEffects`. When
`last_selection_from_search` is set and `BufferSearchHighlights` are
active, selection occurrence highlights are suppressed. Making a manual
text selection during an active search clears the flag, restoring normal
occurrence-highlight behavior.

The behavior now matches how VSCode handles this case.

The video below demonstrates the behavior after the fix and shows that
it matches the VSCode behavior now

[Screencast from 2026-03-28
01-33-46.webm](https://github.com/user-attachments/assets/07a005b8-53b1-4abf-93d2-96406f0b6a11)

Closes #52589

## How to Review

Three files changed — read in this order:

1. **`crates/editor/src/editor.rs`** Adds `from_search: bool` to
`SelectionEffects` (with a builder method) and
`last_selection_from_search: bool` to `Editor`.
`selections_did_change()` records the flag from effects.
`prepare_highlight_query_from_selection()` returns `None` early when
both `last_selection_from_search` and active `BufferSearchHighlights`
are set.

2. **`crates/editor/src/items.rs`** `activate_match()` now calls
`.from_search(true)` on its `SelectionEffects` so search-driven
selections are marked at the source.

3. **`crates/search/src/buffer_search.rs`** Regression test
`test_regex_search_does_not_highlight_non_matching_occurrences`:
verifies that after search navigation, `SelectedTextHighlight` is
suppressed and exactly 3 `BufferSearchHighlights` exist; and that after
a manual selection, `SelectedTextHighlight` is restored.

## Self-Review Checklist

- [x] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Fixed regex buffer search highlighting non-matching word occurrences
via the selection occurrence highlight feature
```

## Parents

- [bf185f7402dc7d0445f64d5d9af260f328af6eb9](https://git.cafe/gitcafe/zed/commit/bf185f7402dc7d0445f64d5d9af260f328af6eb9?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/1ac669d108fbcf9b14bbfa45ed0a2d16e830930e?format=markdown)
