Skip to content

markdown_preview: Add search support to markdown preview#52502

Merged
ConradIrwin merged 4 commits into
zed-industries:mainfrom
Tahinli:main
Apr 6, 2026
Merged

markdown_preview: Add search support to markdown preview#52502
ConradIrwin merged 4 commits into
zed-industries:mainfrom
Tahinli:main

Conversation

@Tahinli

@Tahinli Tahinli commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

Context

The markdown preview had no search functionality — pressing Ctrl+F did nothing. This PR implements the SearchableItem trait for MarkdownPreviewView, enabling in-pane text search with match highlighting and navigation.

Changes span four crates:

  • project: Added SearchQuery::search_str() — a synchronous method to search plain &str text, since the existing search() only works on BufferSnapshot.
  • markdown: Added search highlight storage to the Markdown entity and paint_search_highlights to MarkdownElement. Extracted the existing selection painting into a reusable paint_highlight_range helper to avoid duplicating quad-painting logic.
  • markdown_preview: Implemented SearchableItem with full match navigation, active match tracking, and proper SearchEvent emission matching Editor behavior.
  • Keymaps: Added buffer_search::Deploy bindings to the MarkdownPreview context on all three platforms.

The PR hopefully Closes #27154

How to Review

  1. crates/project/src/search.rs — search_str method at the end of impl SearchQuery. Handles both Text (AhoCorasick) and Regex variants with whole-word and multiline support.
  2. crates/markdown/src/markdown.rs — Three areas:
    - New fields and methods on Markdown struct (~line 264, 512-548)
    - paint_highlight_range extraction and paint_search_highlights (~line 1059-1170)
    - The single-line addition in Element::paint (~line 2003)
  3. crates/markdown_preview/src/markdown_preview_view.rs — The main change. Focus on:
    - SearchEvent::MatchesInvalidated emission in schedule_markdown_update (line 384)
    - EventEmitter and as_searchable (lines 723, 748-754)
    - The SearchableItem impl (lines 779-927), especially active_match_index which computes position from old highlights to handle query changes correctly
  4. Keymap files — Two lines each for Linux/Windows, one for macOS.

Self-Review Checklist

  • [ x ] I've reviewed my own diff for quality, security, and reliability
  • [ x ] Unsafe blocks (if any) have justifying comments (no unsafe)
  • [ x ] The content is consistent with the UI/UX checklist (should be 😄 )
  • [ - ] Tests cover the new/changed behavior (not sure)
  • [ - ] Performance impact has been considered and is acceptable (I'm not sure about it and it would be nice to see experienced people to test)

Release Notes:

  • Added search support (Ctrl+F / Cmd+F) to the markdown preview

@cla-bot

cla-bot Bot commented Mar 26, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @Tahinli 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 Mar 26, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator Bot requested review from a team, ConradIrwin and mikayla-maki and removed request for a team March 26, 2026 15:00
@Tahinli

Tahinli commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

We require contributors to sign our Contributor License Agreement, and we don't have @Tahinli 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 check

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

cla-bot Bot commented Mar 26, 2026

Copy link
Copy Markdown

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

@ConradIrwin

Copy link
Copy Markdown
Member

Thanks for this! It's an obviously missing feature.

When the markdown source changes, I think we race the search and the re-rendering; is it possible for that race to cause a crash if the search highlights now point in the middle of a multi-byte character?

I think we should ideally do the search based on the rendered text; but happy to leave that to a follow-up (we'd need a way to calculate the visible text, which I don't think we do yet?).

For some reason in the default "One Dark" theme, I see an extra line at the far right that doesn't show up when searching the buffer text.
Screenshot 2026-03-31 at 9 07 23 PM

And finally, I think we should hide the select-all button as markdown doesn't support multiple selections.

…ch bar

Hide the select-all button for searchable items that don't support
multiple selections by adding a `select_all` field to `SearchOptions`.
Set it to false for markdown preview and terminal.

Fix a spurious right border on the close button by returning
`ItemBufferKind::Singleton` from `MarkdownPreviewView::buffer_kind()`,
preventing the search bar from treating the preview as a multibuffer.
@Tahinli

Tahinli commented Apr 4, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ConradIrwin,

I think I addressed the review feedbacks. I didn't do anything for rendered text feature but hoping that other issues are fixed.

@Tahinli

Tahinli commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

I'm seeing that cargo fmt check fails. Do you want me to recommit?

@ConradIrwin

Copy link
Copy Markdown
Member

@Tahinli please do!

Clear stale search highlight byte ranges in `reset()` so they cannot
reference old source text while a new parse and search are in progress.
@Tahinli

Tahinli commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

@Tahinli please do!

@ConradIrwin Done.

@ConradIrwin ConradIrwin enabled auto-merge (squash) April 6, 2026 16:29
@ConradIrwin

Copy link
Copy Markdown
Member

Thanks again for this. If you felt excited to take a pass at searching the rendered markdown instead of the source, I think that would make this feature truly excellent; but already great to have a v1 pushed.

@Tahinli

Tahinli commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks again for this. If you felt excited to take a pass at searching the rendered markdown instead of the source, I think that would make this feature truly excellent; but already great to have a v1 pushed.

I will be looking at this. I'm trying to understand gpui more deeply in my free time and definitely revisit for that feature.

@ConradIrwin ConradIrwin merged commit fd4d844 into zed-industries:main Apr 6, 2026
30 checks passed
@tredondo

Copy link
Copy Markdown
Contributor

Any chance to add search in the rendered Markdown from the Agent panel?

@Tahinli

Tahinli commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Any chance to add search in the rendered Markdown from the Agent panel?

I couldn't understand your wish. What do you want exactly? Also you can mention me, if you open a feature request issue or discussion. I'm happy to help.

@ConradIrwin

Copy link
Copy Markdown
Member

I think it'd be awesome to have cmd-f work in the agent panel (which uses markdown). It's probably a bit more complicated because that view is a stack of things that all need searching, but doesn't seem unsolvable if someone wants to take a go at it.

@tredondo

Copy link
Copy Markdown
Contributor

Any chance to add search in the rendered Markdown from the Agent panel?

I couldn't understand your wish. What do you want exactly?

Thanks @ConradIrwin for the explanation.

Also you can mention me, if you open a feature request issue or discussion. I'm happy to help.

@Tahinli I tagged you in #39338 (comment)

piper-of-dawn pushed a commit to piper-of-dawn/zed that referenced this pull request Apr 25, 2026
…ies#52502)

Context

The markdown preview had no search functionality — pressing Ctrl+F did
nothing. This PR implements the SearchableItem trait for
MarkdownPreviewView, enabling in-pane text search with match
highlighting and navigation.

  Changes span four crates:

- project: Added SearchQuery::search_str() — a synchronous method to
search plain &str text, since the existing search() only works on
BufferSnapshot.
- markdown: Added search highlight storage to the Markdown entity and
paint_search_highlights to MarkdownElement. Extracted the existing
selection painting into a reusable paint_highlight_range helper to avoid
duplicating quad-painting logic.
- markdown_preview: Implemented SearchableItem with full match
navigation, active match tracking, and proper SearchEvent emission
matching Editor behavior.
- Keymaps: Added buffer_search::Deploy bindings to the MarkdownPreview
context on all three platforms.
  
The PR hopefully Closes
zed-industries#27154

  How to Review

1. crates/project/src/search.rs — search_str method at the end of impl
SearchQuery. Handles both Text (AhoCorasick) and Regex variants with
whole-word and multiline support.
  2. crates/markdown/src/markdown.rs — Three areas:
    - New fields and methods on Markdown struct (~line 264, 512-548)
- paint_highlight_range extraction and paint_search_highlights (~line
1059-1170)
    - The single-line addition in Element::paint (~line 2003)
3. crates/markdown_preview/src/markdown_preview_view.rs — The main
change. Focus on:
- SearchEvent::MatchesInvalidated emission in schedule_markdown_update
(line 384)
    - EventEmitter<SearchEvent> and as_searchable (lines 723, 748-754)
- The SearchableItem impl (lines 779-927), especially active_match_index
which computes position from old highlights to handle query changes
correctly
  4. Keymap files — Two lines each for Linux/Windows, one for macOS.

  Self-Review Checklist

- [ x ] I've reviewed my own diff for quality, security, and reliability
- [ x ] Unsafe blocks (if any) have justifying comments (no unsafe)
- [ x ] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
(should be 😄 )
- [ - ] Tests cover the new/changed behavior (not sure)
- [ - ] Performance impact has been considered and is acceptable (I'm
not sure about it and it would be nice to see experienced people to
test)


Release Notes:
- Added search support (Ctrl+F / Cmd+F) to the markdown preview

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
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 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.

Support Search in Markdown Preview / Release Notes

3 participants