Skip to content

Conversation

@mikayla-maki
Copy link
Member

@mikayla-maki mikayla-maki commented Nov 7, 2025

Supersedes #39910

At some point, these two (index_for_x and closest_index_for_x) methods where separated out and some code paths used one, while other code paths took the other. That said, their behavior is almost identical:

  • index_for_x computes the index behind the pixel offset, and returns None if there's an overshoot
  • closest_index_for_x computes the nearest index to the pixel offset, taking into account whether the offset is over halfway through or not. If there's an overshoot, it returns the length of the line.

Given these two behaviors, closest_index_for_x seems to be a more useful API than index_for_x, and indeed the display map and other core editor features use it extensively. So this PR is an experiment in simply replacing one behavior with the other.

Release Notes:

  • Improved the accuracy of mouse selections in Markdown
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 7, 2025
@mikayla-maki mikayla-maki changed the title unify the index_for_x methods Nov 7, 2025
let fragment_end_x = fragment_start_x + shaped_line.width;
if x < fragment_end_x {
return Some(
fragment_start_index + shaped_line.index_for_x(x - fragment_start_x)?,
Copy link
Member Author

@mikayla-maki mikayla-maki Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, this is the only line of code that could be effected by removing the "overshoot returns None" part of the API. However, I believe this case is impossible to reach due to the x < fragment_end_x check above it, and so we can safely move forward with this API change.

@mikayla-maki mikayla-maki marked this pull request as ready for review November 7, 2025 02:57
@SomeoneToIgnore SomeoneToIgnore merged commit 082b80e into main Nov 7, 2025
28 checks passed
@SomeoneToIgnore SomeoneToIgnore deleted the replace-index-for-x branch November 7, 2025 12:23
tomatitito pushed a commit to tomatitito/zed that referenced this pull request Nov 7, 2025
Supersedes zed-industries#39910

At some point, these two (`index_for_x` and `closest_index_for_x`)
methods where separated out and some code paths used one, while other
code paths took the other. That said, their behavior is almost
identical:

- `index_for_x` computes the index behind the pixel offset, and returns
`None` if there's an overshoot
- `closest_index_for_x` computes the nearest index to the pixel offset,
taking into account whether the offset is over halfway through or not.
If there's an overshoot, it returns the length of the line.

Given these two behaviors, `closest_index_for_x` seems to be a more
useful API than `index_for_x`, and indeed the display map and other core
editor features use it extensively. So this PR is an experiment in
simply replacing one behavior with the other.

Release Notes:

- Improved the accuracy of mouse selections in Markdown
SomeoneToIgnore added a commit that referenced this pull request Nov 12, 2025
SomeoneToIgnore added a commit that referenced this pull request Nov 12, 2025
This reverts commit 082b80e.

This broke clicking, e.g. in snippets like

```rs
let x = vec![
    1, 2, //
    3,
];
```

clicking between `2` and `,` is quite off now.

Release Notes:

- N/A
11happy pushed a commit to 11happy/zed that referenced this pull request Dec 1, 2025
Supersedes zed-industries#39910

At some point, these two (`index_for_x` and `closest_index_for_x`)
methods where separated out and some code paths used one, while other
code paths took the other. That said, their behavior is almost
identical:

- `index_for_x` computes the index behind the pixel offset, and returns
`None` if there's an overshoot
- `closest_index_for_x` computes the nearest index to the pixel offset,
taking into account whether the offset is over halfway through or not.
If there's an overshoot, it returns the length of the line.

Given these two behaviors, `closest_index_for_x` seems to be a more
useful API than `index_for_x`, and indeed the display map and other core
editor features use it extensively. So this PR is an experiment in
simply replacing one behavior with the other.

Release Notes:

- Improved the accuracy of mouse selections in Markdown
11happy pushed a commit to 11happy/zed that referenced this pull request Dec 1, 2025
…ed-industries#42505)

This reverts commit 082b80e.

This broke clicking, e.g. in snippets like

```rs
let x = vec![
    1, 2, //
    3,
];
```

clicking between `2` and `,` is quite off now.

Release Notes:

- N/A
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

3 participants