]> Sergey Matveev's repositories - vim-lsp.git/commit
Avoid moving the cursor when applying textedits
authorMagnus Groß <magnus@mggross.com>
Thu, 12 Oct 2023 23:08:06 +0000 (01:08 +0200)
committerMagnus Groß <magnus@mggross.com>
Thu, 12 Oct 2023 23:44:09 +0000 (01:44 +0200)
commitfbe9e8cdbc3d93fcd13cd9e8e0bcbb8a63b15b2c
treebb4a953204d2d3289cf9625cc9278853daac2dbf
parent17b5dcfafdf586f272cd0bf925d81e5a425a3ce8
Avoid moving the cursor when applying textedits

In 33c9dc37bd354391579f669a142699801b13d571 we removed a workaround that
resets the cursor to the previous location after applying an
workspaceEdit, because it interfered with auto-imports.

Turns out the workaround was there for a reason: When we apply general
textedits (e.g. when we rename a variable), then we are always moving
the cursor by accident, because internally (to apply a textedit) we
delete the entire text range and then append the new range.
Obviously this loses the cursor position, hence the reason for the
original manual cursor reset workaround.

Instead of reintroducing that workaround, we now avoid moving the cursor
altogether: We accomplish this by no longer deleting the entire range
and inserting it again. Instead we just remove lines that actually need
to be deleted, add lines that actually need to be added and then handle
the rest with a normal setbufline() call.

This will cause the cursor to never lose the correct position, because
we never remove the entire region where the cursor is located in.

We have to be careful with off-by-one errors, therefore this also adds
an extra test for the scenario where we have to delete some lines. The
two other scenarios already had comprehensive tests (adding lines, and
keeping the amount of lines).
autoload/lsp/textedit.vim
test/clangd_tests.vim