Bug 504762

Summary: Kate with rust-analyzer messes up code badly on save
Product: [Applications] kate Reporter: kimimaru
Component: applicationAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED NOT A BUG    
Severity: normal CC: waqar.17a
Priority: NOR    
Version First Reported In: 25.04.1   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: A video demonstrating the bug with improperly formatting code

Description kimimaru 2025-05-25 09:55:02 UTC
Created attachment 181726 [details]
A video demonstrating the bug with improperly formatting code

SUMMARY
Kate's LSP client with rust-analyzer regularly messes up code when saving.

STEPS TO REPRODUCE
1. Download the source code at https://codeberg.org/kimimaru/UinputVirtualDevices or clone with Git
2. Open the project in Kate with rust-analyzer installed as an LSP client
3. Open virtual_joystick.rs file and save it without making any changes. You may need to save more than once to trigger it.

OBSERVED RESULT
The code will be formatted in a way that often makes a big mess, constantly switching between two different variants for imports at the top for example. And also towards the bottom of the file, where it'll will leave big gaps for some reason.
The most common formatting problem is that it adds an extra comma at the start of the line after arguments, and sometimes it even duplicates arguments. I find myself having to counterproductivively manually clean up all of the formatting changes the LSP client should be helping me with.

EXPECTED RESULT
The code is formatted properly and doesn't cause compiler errors and require manual cleanup.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: EndeavourOS (Linux kernel 6.14.7-arch2-1 64-bit)
KDE Plasma Version: 6.3.5
KDE Frameworks Version: 6.14.0
Qt Version: 6.9.0
Graphics Platform: Wayland

ADDITIONAL INFORMATION
The problem occurs even with all settings turned off in LSP Client > Client Settings.
Comment 1 Waqar Ahmed 2025-05-26 04:27:24 UTC
1. Is LSPClient -> Client Settings -> Format on save/typing off?
2. Is the "Formatting" plugin enabled?
3. What happens if you run rustfmt from the cmdline?
Comment 2 kimimaru 2025-05-26 11:59:44 UTC
When LSPClient -> Client Settings -> Format on save/typing is off, it behaves as if running rustfmt manually.

The Formatting plugin is on. I noticed the problem occurs so long as Format on save/typing is on during the entire Kate session. When turning it off, I have to restart Kate entirely to disable the behavior - even restarting the LSP server doesn't work.
Comment 3 Waqar Ahmed 2025-05-26 12:24:21 UTC
Then I suggest turning either formatting plugin off or disabling formatting on save in lsp plugin. They can clash, and the resulting behavior is undefined.(In reply to kimimaru from comment #2)