Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flaviojs opened this issue Sep 13, 2024 · 2 comments
Open

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

flaviojs opened this issue Sep 13, 2024 · 2 comments

Comments

@flaviojs
Copy link

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. 😉

@ytmimi
Copy link
Contributor

ytmimi commented Sep 13, 2024

related to #5553

@calebcartwright
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants