# Always return valid locations when refreshing anchors · gitcafe/zed

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

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

Visibility: public

Requested revision: a284e7140c8f76ca1fd769dfed8cc0b8a41821b8

Requested commit: a284e7140c8f76ca1fd769dfed8cc0b8a41821b8

Commit: a284e7140c8f76ca1fd769dfed8cc0b8a41821b8

Tree: 7b884da2a7b20f82a84b6d523990fa6d493a43da

Author: Antonio Scandurra

Committer: Antonio Scandurra

## Message

```
Always return valid locations when refreshing anchors

Specifically, with this commit:

- We will now refresh the anchor if it escapes the boundaries of the excerpt by
using the `Excerpt::contains` method. This was not the case before, as we were
just checking if the excerpt id and buffer id of the anchors matched the ones
stored on the excerpt.
- We fixed a bug that was causing the anchor to be outside of the excerpt when
resetting it to one of the excerpt's endpoints after we couldn't keep its
position. This would happen because we were using `anchor_at`, which resolved
the anchor to an offset first and then converted it back into an anchor with
the given bias, which is a lossy operation. We now use `Anchor::bias` to
achieve the same goal: note that this could still lead to the anchor escaping
the excerpt's boundary when the bias doesn't match the endpoint's bias, so we
take extra care to avoid that and `min`/`max` the newly-produced anchor with
the other endpoint.

```

## Parents

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

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