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

Most upvoted comments

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:

The character that has been typed.

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.