# editor: Keep the cursor before text inserted by on-type formatting \(\#61823\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 770a977c686f5786e0274da7299842e047f7bafa

Requested commit: 770a977c686f5786e0274da7299842e047f7bafa

Commit: 770a977c686f5786e0274da7299842e047f7bafa

Tree: 5a642130e0674fd5d1ab9eb3490866c5863211d3

Author: Ibrahim Khan

Committer: GitHub

## Message

```
editor: Keep the cursor before text inserted by on-type formatting (#61823)

## Why

`textDocument/onTypeFormatting` edits that insert or replace text at an
empty cursor use its right bias and move it past the new text. In paired
tags, pressing Enter can therefore leave the cursor on the closing tag
instead of between the tags.

## What

- Capture a left-biased pin for each empty cursor before requesting
on-type formatting.
- Skip cursor tracking unless a matching language server advertises the
trigger.
- Restore only unchanged empty cursors whose displacement is fully
covered by formatting transaction ranges, so intervening user edits are
preserved.
- Reset vertical movement state when restoring a cursor.

## Testing

- `cargo test -p editor test_on_type_formatting` (5 passed)
- `./script/clippy -p editor`

## References

- Fixes https://github.com/zed-industries/zed/issues/61574

Release Notes:

- Fixed the cursor being moved past text inserted or replaced at its
position during on-type formatting.

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
```

## Parents

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

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