Alignment of horizontally-formatted enum struct variants

オープン
#6,998 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
48/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
静か
技術スタック
rust
領域
tooling

調査の方向性

まず、関連するオプション struct_field_align_threshold と enum_discrim_align_threshold が rustfmt でどのように実装・テストされているかを調べます。水平方向にフォーマットされた enum の struct バリアントがどのように処理されるかを追跡し、その後、しきい値の上限・下限、max_width、デフォルトで無効な値、整列された出力について、設定の動作とテストを定義します。

索引モデルが issue の本文から書いたものです。

説明

C-feature-request P-low S-on-hold

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

主要言語
Rust
スター
7.1k
フォーク
1.2k
平均マージ
1日 4時間
マージ済み PR(30日)
20

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rustfmt のほかの issue

rust-lang/rustfmt の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。