rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

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

Open flaviojs opened this issue 1 year ago • 2 comments

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:

flaviojs avatar Sep 13 '24 17:09 flaviojs

related to #5553

ytmimi avatar Sep 13 '24 20:09 ytmimi

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

calebcartwright avatar Sep 23 '24 16:09 calebcartwright

@calebcartwright I'd be curious to hear more about the concerns you have about attributes. I would naively expect things to work just like rustfmt::skip, which is an attribute too.

polwel avatar Jun 21 '25 09:06 polwel