rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.98k stars 879 forks source link

Add config and attribute to set the number of array values per line #6325

Open flaviojs opened 3 weeks ago

flaviojs commented 3 weeks ago

In programming, arrays of data are sometimes formatted with X values per line, usually a multiple of 10 or 2. This allows you to easily find and inspect the data of a specific index by counting the lines and values on the target line.

The number of values per line depends on the nature of the data, so a global config is basically a default. You also need a way to set the config of individual arrays.

Currently I have to use #[rustfmt::skip] and format it manually, but I really want it to be automatic.

Example:

const BAD: u8 = 0xff;

#[rustfmt::skip]
static base64val: [u8; 128] = [
    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
    BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
     52, 53, 54, 55,  56, 57, 58, 59,  60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
    BAD,  0,  1,  2,   3,  4,  5,  6,   7,  8,  9, 10,  11, 12, 13, 14,
     15, 16, 17, 18,  19, 20, 21, 22,  23, 24, 25,BAD, BAD,BAD,BAD,BAD,
    BAD, 26, 27, 28,  29, 30, 31, 32,  33, 34, 35, 36,  37, 38, 39, 40,
     41, 42, 43, 44,  45, 46, 47, 48,  49, 50, 51,BAD, BAD,BAD,BAD,BAD
];

If possible I would like an option to set the maximum number of array values per line and an attribute to configure individual arrays. Something like:

#[rustfmt::config(max_array_values_per_line = 16)]
static base64val: [u8; 128] = [
    BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
    BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
    BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, 62, BAD, BAD, BAD, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD, BAD, BAD, BAD, BAD, BAD,
    BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD, BAD, BAD, BAD, BAD,
    BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD, BAD, BAD, BAD, BAD
];

If you feel like it, an option that aligns values "vertically" (to the left, to the right or centered) is welcome. :wink:

ytmimi commented 2 weeks ago

related to #5553

calebcartwright commented 1 week ago

attribute-driven formatting is a risky proposition for rustfmt, particularly given the existing set of features like formatting a single file or a subset of lines within the file and the results needing to be deterministic regardless of scope.

so while it's widely recognized that the existing style rules and corresponding rustfmt formatting behavior are not great for certain constructs like large arrays, matrices, etc., i don't think attribute-based formatting rules will be a viable option.

it's a topic the style team is likely to take under consideration for the 2027 edition, and something i think rustfmt needs to first see if we can format "better" before we consider going to the extreme of allowing user control over one array to the next