Bug 479779 - textDocument/formatting LSP message causes duplication of buffer text
Summary: textDocument/formatting LSP message causes duplication of buffer text
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: application (show other bugs)
Version: 23.08.4
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-14 01:11 UTC by jade
Modified: 2024-05-25 19:19 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
debug log from kate, with lsp logs (12.05 KB, text/x-log)
2024-01-14 01:11 UTC, jade
Details

Note You need to log in before you can comment on or make changes to this bug.
Description jade 2024-01-14 01:11:49 UTC
Created attachment 164889 [details]
debug log from kate, with lsp logs

SUMMARY
***
NOTE: If you are reporting a crash, please try to attach a backtrace with debug symbols.
See https://community.kde.org/Guidelines_and_HOWTOs/Debugging/How_to_create_useful_crash_reports
***

Given the following document of Nix code:

let x = 5;
in 1

When the formatting button is pressed using the nixd language server, the following becomes the buffer content:

in 1
let x = 5;
in 1

This is essentially pasting the content of the file a second time, which is a bug. This does not happen in neovim with coc, and I strongly suspect this is a bug in kate's LSP client.

This is what the language server responds to the format request with:

"{\"id\":4,\"jsonrpc\":\"2.0\",\"result\":[{\"newText\":\"let x = 5;\\nin 1\\n\",\"range\":{\"end\":{\"character\":2147483647,\"line\":2147483647},\"start\":{\"character\":0,\"line\":0}}}]}"

This message is intending to overwrite the entire contents of the file with the provided text. However, kate seems to do something different with it.

I have attached a full debug log of the LSP interaction.

I ran kate with the following:

LSPCLIENT_DEBUG=1 QT_FORCE_STDERR_LOGGING=1 QT_LOGGING_RULES='katelspclientplugin=true;default.debug=true' kate --startanon -b bad.nix

STEPS TO REPRODUCE
0. Acquire nixd and nixpkgs-fmt from nixpkgs. If you have nix, this can be done with `nix-shell -p nixd nixpkgs-fmt`, then running kate from the resulting shell.
1. Configure kate with nixd like so:
{
    "servers": {
        "nix": {
            "command": ["nixd"],
            "highlightingModeRegex": "^Nix$"
        }
    }
}

2.  Write this into bad.nix:
let x = 5;
in 1

3. Press the LSP format button
4. Get mangled text

OBSERVED RESULT

Text is mangled.

EXPECTED RESULT

Text should be replaced with the new text from the language server.

SOFTWARE/OS VERSIONS
Operating System: NixOS 24.05
KDE Plasma Version: 5.27.10
KDE Frameworks Version: 5.112.0
Qt Version: 5.15.11
Kernel Version: 6.1.69 (64-bit)
Graphics Platform: Wayland
Processors: 8 × Intel® Core™ i7-7700HQ CPU @ 2.80GHz
Memory: 31.2 GiB of RAM
Graphics Processor: Mesa Intel® HD Graphics 630

ADDITIONAL INFORMATION
Comment 1 Waqar Ahmed 2024-02-19 06:09:35 UTC
> result\":[{\"newText\":\"let x = 5;\\nin 1\\n\",\"range\":{\"end\":{\"character\":2147483647,\"line\":2147483647

The server seems to be sending invalid end-line, end-character
Comment 2 Christoph Cullmann 2024-05-25 19:19:01 UTC
https://invent.kde.org/utilities/kate/-/merge_requests/1505