# search: Fixed highlighting item beyond cursor \(\#54514\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 2d86e3b30b735e5e04e64c6e09ba76141f02fbba

Requested commit: 2d86e3b30b735e5e04e64c6e09ba76141f02fbba

Commit: 2d86e3b30b735e5e04e64c6e09ba76141f02fbba

Tree: a0e1d74d2d93e84eb0c2f807d21730be0964886e

Author: Dino

Committer: GitHub

## Message

```
search: Fixed highlighting item beyond cursor  (#54514)

Search is highly generic, editor is the only implementation that has a
concept of a cursor. When the cursor moves eventually
`Editor::selections_did_change` runs which among a bunch of things
triggers the `SearchEvent::ActiveMatchChanged`. This was handled by the
`BufferSearchBar` by moving the currently highlighted item.

When going to the next search result (from the cursor position) we call
`BufferSearchBar::match_index_for_direction`. It relied on the
current_index being updated during the handling of `ActiveMatchChanged`
which we removed. It now instead figures out the next/prev search result
from the users cursor in the buffer directly.

The `SearchEvent::ActiveMatchChanged` was being used for different
responsibilities in different places in the codebase where it was
emmited, namely:

* On the `editor::Editor::selections_did_change` it was being emmited in
  case a single selection (single cursor) existed, and this was the
  thing reponsible for indirectly updating the active match as soon as
  the cursor moved out of match.
* On the `terminal` side, it's being emitted when the user clicks on the
  terminal, to "simulate" what the cursor position would be and update
  the active match to the next closes match compared to the click
  position.
* On the `markdown_preview` side it was being emmited whenever the
  active match was updated but, from testing, it does actually seem to
  not drive anything and is unnecessary?

Given that we no longer want to update the active match in the buffer
everytime the user moves the cursor, we removed the event emission from
`editor::Editor::selections_did_change` as well as removing the one from
`markdown_preview`, as it seems it doesn't have any noticeable impact.

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] 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

Closes #44367

Release Notes:

- Fixed search highlighting changing anytime the cursor moves.

---------

Co-authored-by: Yara <git@yara.blue>
```

## Parents

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

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