rust-lang / rust-lang/rustfmt

Alignment of horizontally-formatted enum struct variants

Open
#6,998 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low S-on-hold
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Feature Request

struct_variant_align_threshold

Summary

For applicable, horizontally-formatted branches of an enum with struct variants, aligns them by adding padding space after each branch name, so all struct variants start at the same column (one space after the longest branch name). This is used in good combination with the stable option struct_variant_width.

When enabled, this is applied to all branches when their name is shorter than threshold, and adding the padding does not exceed max_width. The default value would be 0 to disable it, with any nonnegative integer as a possible value.

Motivation

A good example is an enum where many struct variants have the same or similar fields, allowing for much greater readability of the branch names. This is particularly common on enums that have many shared fields, and many, many variants. An example is the Instruction enum on uutils awk:

Original (snippet):

pub enum Instruction {
    Record { dest: Reg, arg: Arg, ty: ArgTy },
    Negation { dest: Reg, arg: Arg, ty: ArgTy },
    ToInt { dest: Reg, arg: Arg, ty: ArgTy },
    Negative { dest: Reg, arg: Arg, ty: ArgTy },
    ...
    Matches { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    MatchesNot { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    Add { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    Subtract { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    ...
}

Formatted (new):

pub enum Instruction {
    Record        { dest: Reg, arg: Arg, ty: ArgTy },
    Negation      { dest: Reg, arg: Arg, ty: ArgTy },
    ToInt         { dest: Reg, arg: Arg, ty: ArgTy },
    Negative      { dest: Reg, arg: Arg, ty: ArgTy },
    ...
    Matches       { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    MatchesNot    { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    Add           { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    Subtract      { dest: Reg, lhs: Arg, rhs: Arg, tyr: ArgTy, tyl: ArgTy },
    ...
}

Note: I know this particular case can be solved using a newtype or an alias, but we have our reasons for it to be this way in the real codebase. Believe me, this enum was not born this way.

Related configuration options

struct_field_align_threshold, enum_discrim_align_threshold

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by studying how the related options struct_field_align_threshold and enum_discrim_align_threshold are implemented and tested in rustfmt. Trace how horizontally formatted enum struct variants are handled, then define the configuration behavior and tests for threshold limits, max_width, the default disabled value, and aligned output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.