Avoid moving the cursor when applying textedits #407
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently the
ApplyTextEdits()
code loses the cursor position, because it applies the text edits by deleting the entire range withdeletebufline()
and then adding the new range withappendbufline()
.Obviously the cursor will move, if it happens to be inside this range, which is often the case (e.g. when renaming a variable).
This is also apparently the reason, why in the past there was a workaround that saved the cursor position before applying any edits, and then restored it afterwards.
That workaround was already (correctly) removed with: #389 (comment)
Instead of reintroducing the workaround, this patch avoids losing the cursor position altogether by not deleting the entire range, but only deleting lines that actually need to be deleted (and adding new lines that actually need to be added).
In my opinion this solution is even simpler code-wise than it was before where we removed the entire region.
I also added a new test case for the scenario where lines have to be removed, all other cases already have test cases.
Demo
The old behaviour is on the left side: After renaming the variable, the cursor jumps to the end of the range.
The new behaviour is on the right side: After renaming the variable, the cursor does not move.
demo.mp4