Some lines deactivate rustfmt in VSCode

When code line reached some length (horizontally) rustfmt stops auto-format in VSCode.
I can't understand where is this line or lines and won't change default settings in rustfmt.toml

Any ideas how to detect where is exactly my code deactivates rustfmt, to reorganize folding / change horizontal length of code?

The rule that rustfmt follows here is that if rustfmt attempts to reformat a statement or block and the result of doing so is over the width limit, it will leave the entire statement unchanged rather than formatting it with some overlong lines.

The usual cause of such things is string literals:

fn foo() {
    let x =
    "    this string literal is really entirely too long and in fact it itself will not fit on a single line and as a consequence rustfmt does nothing with the entire line    "
                         .trim();
}

The workaround is to insert explicit line breaks in the string literal. In particular, if you place \ at the end of a line in a string literal, the line break and all following whitespace will be ignored. (In principle, rustfmt could insert these, but it does not.) So, you can change the above code to this:

fn foo() {
    let x =
    "    this string literal is really entirely too long and in fact it itself \
         will not fit on a single line and as a consequence rustfmt does \
         nothing with the entire line    "
                         .trim();
}

and then when you format it, rustfmt will produce:

fn foo() {
    let x = "    this string literal is really entirely too long and in fact it itself \
         will not fit on a single line and as a consequence rustfmt does \
         nothing with the entire line    "
        .trim();
}

which is formatted and obeys the default 100-column width. If you would like rustfmt to error rather than do nothing, then there is the unstable error_on_unformatted option.

2 Likes

Thanks for explanation, but still can't catch the issue lines in this file for example.

Can I run some debug command for this file?

I don’t know of any ways to debug what rustfmt is thinking, but the code at and around line 388 is far too deeply nested — try to manually break it at valid locations and it will still be ~110 characters wide. This is not just something rustfmt can’t handle — it's not very legible to humans regardless of how it is formatted. I would suggest that the code for the Response::Download and Response::Stream match arms should be moved into separate functions.

1 Like

I'm currently refactoring this file, it was about 1k lines before. Just stuck with formatter again, and asked about possible detection ways.

if there is nothing else but manual detection, well, thanks much for tips, anyway!
p.s. just expected that's maybe VSCode bug