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-17 12:43:21 +00:00
…
2026-09-11 09:13:44 +00:00
…
…
…

Zed

Zed CI

Welcome to Zed, a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.


Installation

On macOS, Linux, and Windows you can download Zed directly or install Zed via your local package manager (macOS/Linux/Windows).

Other platforms are not yet available:

Developing Zed

Contributing

See CONTRIBUTING.md for ways you can contribute to Zed.

Also... we're hiring! Check out our jobs page for open roles.

Licensing

Zed source code is licensed primarily under GPL-3.0-or-later, with Apache-2.0 components where marked.

License information for third party dependencies must be correctly provided for CI to pass.

We use cargo-about to automatically comply with open source licenses. If CI is failing, check the following:

  • Is it showing a no license specified error for a crate you've created? If so, add publish = false under [package] in your crate's Cargo.toml.
  • Is the error failed to satisfy license requirements for a dependency? If so, first determine what license the project has and whether this system is sufficient to comply with this license's requirements. If you're unsure, ask a lawyer. Once you've verified that this system is acceptable add the license's SPDX identifier to the accepted array in script/licenses/zed-licenses.toml.
  • Is cargo-about unable to find the license for a dependency? If so, add a clarification field at the end of script/licenses/zed-licenses.toml, as specified in the cargo-about book.

Sponsorship

Zed is developed by Zed Industries, Inc., a for-profit company.

If you’d like to financially support the project, you can do so via GitHub Sponsors. Sponsorships go directly to Zed Industries and are used as general company revenue. There are no perks or entitlements associated with sponsorship.

S
Description
No description provided
Readme 1.6 GiB
Languages
Rust 95.4%
JSON-with-Comments 2.4%
Python 0.8%
Inno Setup 0.3%
Tree-sitter Query 0.3%
Other 0.4%