rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Feature request: Specify priority of small heuristics / line-breaking rules

Open BTOdell opened this issue 1 year ago • 5 comments

I have noticed that rustfmt seems to prioritize breaking chains before it'll break on function arguments.

For example:

let value =
    obj.func(&long_struct.arg1, long_struct.arg2, long_struct.arg3, true);

is preferred instead of:

let value = obj.func(
    &long_struct.arg1,
    long_struct.arg2,
    long_struct.arg3,
    true,
);

I have tried so hard to find configuration settings that will prefer the second style when a line is too long (according to max_width, chain_width, or fn_call_width). A quick way to see this behavior is to set all 3 of these config values to the same number (80).

Could a new config option be introduced (perhaps called small_heuristics_priority) that takes an array of the names of the other options in the order that they should be applied to fix the line?

small_heuristics_priority = [ "fn_call_width", "chain_width" ]

BTOdell avatar Feb 06 '24 04:02 BTOdell

Thanks for reaching out. Would use_small_heuristics=Max be a decent workaround for now?

ytmimi avatar Feb 07 '24 01:02 ytmimi

Thanks for the suggestion. I tried that and the formatter still prioritizes breaking chains first. The only time it will break on function args is if the line is still too long even after breaking chains.

BTOdell avatar Feb 07 '24 18:02 BTOdell

@BTOdell Got it. Maybe I've misread, but it seems that you'd prefer this formatting:

let value =
    obj.func(&long_struct.arg1, long_struct.arg2, long_struct.arg3, true);

Could you provide an example where setting use_small_heuristics=Max doesn't provide you with the expected formatting?

ytmimi avatar Feb 07 '24 19:02 ytmimi

I'm sorry, I can see how my original wording is confusing. I intended to say that the first form (the one you just copied) is the format preferred/prioritized BY rustfmt; but I, personally, would like to see the second style instead of the first.

I don't know if this formatting is actually caused by chain_width:

let value =
    obj.func(&long_struct.arg1, long_struct.arg2, long_struct.arg3, true);

but whether this happens seems to occur when the chain_width value is small enough that the rule triggers.

It just looks extremely weird to me to see an equals sign at the end of a line.

BTOdell avatar Feb 09 '24 17:02 BTOdell

Okay, I think that's where my confusion was. You'd like to have some option to prefer this kind of formatting:

let value = obj.func(
    &long_struct.arg1,
    long_struct.arg2,
    long_struct.arg3,
    true,
);

ytmimi avatar Feb 09 '24 17:02 ytmimi