# gpui: Fix panic when hit testing past a wrapped line's width \(\#64672\) · gitcafe/zed

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

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

Visibility: public

Requested revision: e71963a599c64c213aca1c607e7418503a1e787d

Requested commit: e71963a599c64c213aca1c607e7418503a1e787d

Commit: e71963a599c64c213aca1c607e7418503a1e787d

Tree: b42a8f81875a16e6efa1b8b6290497f501b05e7f

Author: Agus Zubiaga

Committer: GitHub

## Message

```
gpui: Fix panic when hit testing past a wrapped line's width (#64672)

# Objective

Fixes ZED-BW8 (``called `Option::unwrap()` on a `None` value`` in
`WrappedLineLayout::_index_for_position`). ZED-75K and ZED-81Z are the
same panic from older releases, and they almost always happen on Linux
when hovering markdown.

Hit testing a wrapped row assumes the next wrap boundary glyph never
sits past the line's width. cosmic-text breaks that assumption. It
computes the width by summing word widths but positions glyphs by
accumulating advances, so a trailing zero-width glyph (zero-width space,
ZWJ, emoji variation selector, combining accent) can land a few ulps
past the width. When that glyph is also a wrap boundary, the row before
it extends slightly past the width. A hit in that sliver made
`index_for_x` return `None`, and we unwrapped it.

## Solution

We now treat `None` from `index_for_x` as past the end of the row and
return `Err` with the row's end index, the same as positions beyond the
row's right edge. `index_for_x` itself is unchanged, so editor click
placement isn't affected.

## Testing

- `index_for_position_past_line_width` shapes `"Warning: this will
delete files\u{200b}"` with the bundled IBM Plex Sans at a 4px wrap
width, then hit tests exactly in the sliver past the width. It panics
without the fix.
- This isn't practical to reproduce by hand, since the sliver is about
1e-5px wide. macOS uses CoreText, which I don't think produces the
overshoot; the web build shares cosmic-text with Linux.

## 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 adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Fixed a rare crash when hovering wrapped markdown text on Linux.
- [GPUI] Fixed a panic in `WrappedLineLayout::index_for_position` when
hit testing past a wrapped line's width.
```

## Parents

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

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