# gpui: Add \`closest_index_for_position\` method \(\#23668\) · gitcafe/zed

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

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

Visibility: public

Requested revision: e1af35aa154ddc10faf4e9a5aac37ae543b1f6ac

Requested commit: e1af35aa154ddc10faf4e9a5aac37ae543b1f6ac

Commit: e1af35aa154ddc10faf4e9a5aac37ae543b1f6ac

Tree: d8c3f1f07e4d9890d11e9454d311b89936da84cf

Author: Jason Lee

Committer: GitHub

## Message

```
gpui: Add `closest_index_for_position` method (#23668)

Closes #ISSUE

Release Notes:

- N/A

------------

I just make a little change to improve `index_for_position` to support
return closest index for position.

I need this method to measure for position cursor in multi-line mode
TextInput.

https://github.com/longbridge/gpui-component/pull/583


https://github.com/user-attachments/assets/c69d098e-d2cb-4053-b739-6c7dd666e769

Before this change, GPUI have `LineLayout::closest_index_for_x` method
for unwrapped line case.


https://github.com/zed-industries/zed/blob/d1be419fff415329b38f26aff90488700702c82a/crates/gpui/src/text_system/line_layout.rs#L58-L94

This change is equivalent to making `index_for_position` have a
corresponding method to get the closest index like `index_for_x`.
```

## Parents

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

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