Since 3.16, LSP supports the capability `insertReplaceSupport`. This
allows textEdit to be an `InsertReplaceEdit` see:
(https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#insertReplaceEdit)
This patch adds support for this capability, and uses the `replace`
field of the `InsertReplaceEdit`. Original functionality (ie.
`TextEdit`) is preserved.
The benefits of this were originally discussed here:
https://github.com/joaotavora/eglot/discussions/1456, but this is a summary:
Consider this file:
```
const Foo = struct {
correct_name: u32,
};
fn example(foo: Foo) u32 {
return foo.correct_name;
}
```
1. Place the cursor on 6:22 (the _ in correct_name)
2. Backspace once to delete the t
3. Receive the following LSP message: `<-- textDocument/completion[20] {"jsonrpc":"2.0","id":20,"result":{"isIncomplete":false,"items":[{"label":"correct_name","kind":5,"detail":"u32","documentation":{"kind":"plaintext","value":""},"sortText":"2_correct_name","textEdit":{"range":{"start":{"line":5,"character":15},"end":{"line":5,"character":21}},"newText":"correct_name"}}]}}`
4. Accept the completion
5. The buffer now contains ` return foo.correct_name_name;` on line 6
I expected it to replace the entire token, resulting in ` return foo.correct_name;`.
Indeed with this patch applied (and an LSP that supports the
capability), the behaviour I expected is now what happens.
This is the first real elisp that I've written besides configuration, so
I'm not sure if this is the correct way, but it seems to work for me.
Patch is attached.
Thanks!
Casey
In GNU Emacs 30.0.91 (build 1, x86_64-w64-mingw32) of 2024-10-17 built
on DESKTOP-EK25TL1
Repository revision: 50620e96d763bd21854a95c580b572687eebc319
Repository branch: eglot_insert_replace_edit
Windowing system distributor 'Microsoft Corp.', version 10.0.19045
System Description: Microsoft Windows 10 Pro (v10.0.2009.19045.5011)
Configured using:
'configure -prefix=/e/dev/emacs-src --without-dbus --without-pop
--with-native-compilation --with-xml2 --with-wide-int
--without-compress-install 'CFLAGS=-O2 -mtune=native -march=native
-fomit-frame-pointer -ftree-vectorize'
PKG_CONFIG_PATH=/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig'