Files
zed/crates
MartinYe1234 621a6adf30 markdown: Fix crash when a selection maps into the middle of a character (#67104)
## Summary

Fixes a crash when the platform asks rendered Markdown for its selection
(IME position updates on macOS and Wayland):

```
end byte index 5 is not a char boundary; it is inside 'о' (bytes 4..6 of string)
```

A Markdown selection is stored as source byte offsets.
`RenderedLine::rendered_index_for_source_index` converted one to a
rendered index by adding the source displacement to the rendered offset
and clamping only to the text length, so the result could be inside a
UTF-8 character. `RenderedText::text_for_range` then sliced there and
panicked. Three ways to get such an offset:

- The source changes after the selection was made and a clicked offset
ends up inside syntax that is now hidden, such as a link destination
that finished streaming in an agent message.
- The source is replaced while the selection is kept, such as a tool
call label whose title updates.
- Rendered text is longer than its source. Smart punctuation turns a run
of hyphens into en dashes, and a click late in the run produces a source
offset past the end of the hyphens.

Changes, all in `crates/markdown/src/markdown.rs`:

- A source offset inside hidden syntax maps to the end of the rendered
text before it instead of spilling into the following text.
- An offset before a line's first text no longer underflows.
- `text_for_range` and `surrounding_word_range` snap to a character
boundary of the laid-out text before slicing.
- Rendered text that is longer than its source, such as en dashes made
from a run of hyphens, gets a source mapping per character. Before,
clicking late in a run of en dashes selected the text after it; now
double-clicking selects the dashes and a drag selects one dash at a
time.

Not covered here:

- Source positions inside text the parser rewrites (dash runs, HTML
entities, backslash escapes, code lines with an expanded tab) are
estimates. The parser reports where a rewritten piece starts and ends in
the source, but not which source bytes produced each rendered character.
A run that mixes em and en dashes can be one hyphen off, for example.
This affects Copy as Markdown of part of such text and where a click in
Markdown Preview places the editor cursor; highlighting, selection and
Copy are exact. Making these exact needs the parser to report a
per-character correspondence.
- A selection still survives `Markdown::replace` by byte offset, so it
can highlight unrelated text afterwards.

Closes FR-546

## Testing

Manual reproduction on a build without this change:

1. In the agent panel, start a thread and send the following. The line
contains 40 hyphens.

   ```
Reply with exactly this one line and nothing else, not in a code block:
   Итог ---------------------------------------- 回回回回回回
   ```

The hyphens should render as a run of 20 short en dashes. If the reply
has a hyphen count divisible by 3 they render as wider em dashes and the
steps below do nothing, so ask again.
2. Wait for the reply to finish.
3. Double-click one of the last five dashes at the right end of the run.
Zed crashes with `end byte index … is not a char boundary; it is inside
'回'`.

To get the stack from the crash reports instead, single-click one of
those dashes and then click into an editor tab. The crash comes on the
next frame through `Window::invalidate_character_coordinates` →
`MarkdownInputHandler::selected_text_range`.

With this change neither variant should crash, double-clicking any dash
selects the whole run of dashes, and dragging across the run selects one
dash at a time.

Release Notes:

- Fixed a crash when clicking or selecting text in agent messages and
other rendered Markdown that contains non-ASCII characters
2026-11-18 15:05:04 +00:00
..
2026-11-11 19:41:39 +00:00
…
…
…
…
…
…
…
2026-11-17 12:43:21 +00:00