rust-analyzer: Format on type adds bracket incorrectly
Hi,
I am not a rust developer or a active rust-analyzer user but I work on a lsp client (Kate editor) and we got this bug report: https://bugs.kde.org/show_bug.cgi?id=452047
Consider the following simple rust code:
fn main() {
|println!("hello");
}
| indicates caret position. If you have “Format on Type” enabled and ou press backspace enought times so that the lines wraps into the previous line and touches the { of the previous line, rust-analzyer suggests to add a bracket at the end of line resulting in
fn main() {|println!("hello");}
}
This led to an infinite loop in Kate (which has been fixed), but still it doesn’t seem right that the LSP suggests to a bracket in that place.
rust-analyzer version: rust-analyzer bc08b8eff 2022-03-28 stable
rustc version: rustc 1.51.0 (2fd73fabe 2021-03-23)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (16 by maintainers)
Commits related to this issue
- Merge #11904 11904: internal: Wrap macros in expr position in `MacroExpr` node r=jonas-schievink a=jonas-schievink This lets us distinguish them from macros in item position just by looking at the s... — committed to rust-lang/rust-analyzer by bors[bot] 2 years ago
- Merge #11904 11904: internal: Wrap macros in expr position in `MacroExpr` node r=jonas-schievink a=jonas-schievink This lets us distinguish them from macros in item position just by looking at the s... — committed to rust-lang/rust-analyzer by bors[bot] 2 years ago
Maybe we should check whether the braces were balanced before inserting another one.
From https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_onTypeFormatting:
Unless I’m missing something, I wouldn’t interpret deleting an indent as typing the opening brace on the previous line.
Do I understand correctly that the user only types backspaces in the example? If so, the LSP client should never send a textDocument/onTypeFormatting request. According to the specification the client can send this request only after the user types a trigger character. Backspace is not a trigger character for rust-analyzer. At least that’s my understanding. Nevertheless, looking at the code of rust-analyzer it seems it does nothing if the char_typed is not in the trigger_chars.