rust-lang/rustfmt

Wish to have some new options about line break style

zhongxinghong opened this issue · 2 comments

version

rustfmt 1.7.1-nightly (94885bc6 2024-09-01)

rustfmt.toml

max_width = 80
use_small_heuristics = "Max"

example

fn _hincrbyex(
    _a: &mut i64,
    _key: &i64,
    _field: &i64,
    _delta: i64,
    _iexpsec: i64,
    _b0: bool,
) -> bool {
    false
}

fn test() {
    let mut conn: i64 = 0;
    let key: i64 = 0;
    let field: i64 = 0;
    let delta: i64 = 0;
    let iexpsec: i64 = 0;

    let is_xxxxxxxx: bool = false;
    let is_xxxxxxxxx: bool = false;
    let is_xxxxxxxxxxxxxxx: bool = false;
    let is_xxxxxxxxxxxxxxxx: bool = false;

    // single line: exactly 80 ------------------------------------------------|
    let _fut = _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxx);

    // style 1: 81 ------------------------------------------------------------|
    let _fut =
        _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxxx);

    // style 1: exactly 80 for the second line --------------------------------|
    let _fut =
        _hincrbyex(&mut conn, &key, &field, delta, iexpsec, is_xxxxxxxxxxxxxxx);

    // style 2: 81 for the second line in style 1 -----------------------------|
    let _fut = _hincrbyex(
        &mut conn,
        &key,
        &field,
        delta,
        iexpsec,
        is_xxxxxxxxxxxxxxxx,
    );
}

According to the above example, rustfmt have two line break styles for long statement. But I really don't like the style 1 because it looks strange in some cases. I have to make some tricky adjustments such as rename the function name / rename some argument names / ... to change the style 1 to single line or style 2. And it bothers me too in some other situations such as match-arms.
I wish to have some new options in rustfmt.toml to disable the style 1, a statement should directly be formatted to the style 2 from single line if it's too long. Thanks~

Seems like this is related to #3626 and is the opposite of #3514

I see. with #3514, I think these can be roughly summarized into the following situations:

for max_width = 80

line length situations:

  1. len(line) <= 80
  2. len(line) > 80, after breaking into two line, len(line2) <= 80
  3. len(line) > 80, after breaking into two line, len(line2) > 80

line break style:

  1. single line
  2. vertical
  3. single line for line2
  4. vertical for line2

examples for line break style:

// 1. single line
let res = f_abc(a, b, c);

// 2. vertical
let res = f_abc(
    a, 
    b, 
    c,
);

// 3. single line for line2
let res = 
    f_abc(a, b, c);

// 4. vertical for line2
let res = 
    f_abc(
        a,
        b,
        c,
    );

user options:

  1. perfer vertical
  2. prefer single line for line2 (default)
  3. prefer vertical for line2

expected style chooses:

len \ opt 1. prefer vertical 2. prefer single line for line2 3. prefer vertical for line2
1. len(line) <= 80 single line single line single line
2. len(line) > 80 and len(line2) <= 80 vertical single line2 vertical line2
3. len(line) > 80 and len(line2) > 80 vertical vertical (same as now) vertical line2