markdown: Make table cell checkboxes clickable (#54747)

Closes #53587

Follow-up to #50595.

[#50595](https://github.com/zed-industries/zed/pull/50595) added
display-only checkboxes when `[x]` / `[X]` / `[ ]` appears as the sole
content of a markdown table cell. That PR called out interactivity as a
later step — this PR delivers it.


https://github.com/user-attachments/assets/c666ee4c-7e31-4450-ab33-c07c82949818

## What this does

When a consumer of `MarkdownElement` supplies an `on_checkbox_toggle`
callback, table-cell checkboxes now invoke it on click with the source
range of the `[x]` / `[ ]` marker and the new checked state. This
mirrors the existing list-item checkbox path.

For markdown preview (`markdown_preview_view.rs`) the callback is
already wired to edit the source buffer, so clicking a checkbox in a
previewed `.md` file now flips `[x]` to `[ ]` and back in the file, the
same as it already did for list checkboxes.

## How it works

`replace_pending_checkbox` previously took the cell's outer source range
and always built a visualization-only checkbox. It now takes the
optional toggle callback instead, and reconstructs the marker's exact
source range from `pending_line.source_mappings` — pulldown-cmark emits
`[x]` in a table cell as three separate `Text` events, so the per-chunk
mappings are needed to recover the 3-character range to rewrite. If a
callback was supplied, the checkbox attaches an `on_click` that invokes
it with that range and `!checked`; otherwise it falls back to the prior
visualization-only rendering.

Two free helpers (`source_range_for_rendered` /
`source_index_for_rendered`) were extracted so the mapping logic is
unit-testable.

## Scope

One file: `crates/markdown/src/markdown.rs`. No changes to
`markdown_preview` — since #52008 it renders through the shared
`MarkdownElement`, so its existing `on_checkbox_toggle` wiring picks up
table checkboxes for free.

## Relation to #53587

[#53587](https://github.com/zed-industries/zed/issues/53587) reports
that markdown checkboxes in the **agent panel** can't be clicked. This
PR is a necessary piece of that fix — without it, even if the agent
panel wired `on_checkbox_toggle`, its table checkboxes would stay inert.
It does not fully close #53587 on its own: the agent panel's
`MarkdownElement` instances in `conversation_view.rs` / `thread_view.rs`
don't currently wire `on_checkbox_toggle`, and wiring it there requires
deciding how clicks should mutate the in-memory agent response (no
source file to edit into). That's a follow-up.

## Test plan

**Unit tests** (2 new, both in `crates/markdown/src/markdown.rs`):
- `test_table_checkbox_marker_source_range` — walks parser events for a
table with checkboxes, replays what the builder accumulates, and asserts
the reconstructed source range slices to exactly `[x]` / `[ ]` in the
original markdown (including a padded-whitespace case).
- `test_source_range_for_rendered_handles_split_chunks` — pins the
mapping helper against the three-chunk layout pulldown-cmark produces.

**Automated**:
- [x] `cargo test -p markdown` — 46 tests pass (44 existing + 2 new)
- [x] `cargo test -p markdown_preview` — 3 tests pass
- [x] `./script/clippy -p markdown` — clean

**Manual** (against a preview of a markdown file with checkbox tables):
- [x] Click a checked `[x]` table cell — it becomes `[ ]` in the source
and rerenders as unchecked
- [x] Click an unchecked `[ ]` — becomes `[x]`
- [x] Uppercase `[X]` toggles on click (replacement writes `[x]` / `[
]`, matching the list-item behaviour)
- [x] Padded-whitespace cells still target just the three marker
characters
- [x] Multiple checkbox columns in one table all independently clickable
- [x] Alignment variants (left/center/right) behave the same
- [x] Non-checkbox table text unaffected
- [x] List-item checkboxes continue to work (regression)

Release Notes:

- Made table-cell markdown checkboxes clickable in markdown preview,
matching list-item checkbox behavior

Co-authored-by: Lukas Wirth <lukas@zed.dev>
1b37531a2eiam-liam committed on 4/24/2026, 1:02:06 PM· committed by GitHubparent2595391
1 file changedLine totals unavailable