rustfmt_nightly/config/
lists.rs

1//! Configuration options related to rewriting a list.
2
3use rustfmt_config_proc_macro::config_type;
4
5use crate::config::IndentStyle;
6
7/// The definitive formatting tactic for lists.
8#[derive(Eq, PartialEq, Debug, Copy, Clone)]
9pub enum DefinitiveListTactic {
10    Vertical,
11    Horizontal,
12    Mixed,
13    /// Special case tactic for `format!()`, `write!()` style macros.
14    SpecialMacro(usize),
15}
16
17impl DefinitiveListTactic {
18    pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
19        match indent_style {
20            IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
21            IndentStyle::Visual => false,
22        }
23    }
24}
25
26/// Formatting tactic for lists. This will be cast down to a
27/// `DefinitiveListTactic` depending on the number and length of the items and
28/// their comments.
29#[config_type]
30pub enum ListTactic {
31    /// One item per row.
32    Vertical,
33    /// All items on one row.
34    Horizontal,
35    /// Try Horizontal layout, if that fails then vertical.
36    HorizontalVertical,
37    /// HorizontalVertical with a soft limit of n characters.
38    LimitedHorizontalVertical(usize),
39    /// Pack as many items as possible per row over (possibly) many rows.
40    Mixed,
41}
42
43#[config_type]
44pub enum SeparatorTactic {
45    Always,
46    Never,
47    Vertical,
48}
49
50impl SeparatorTactic {
51    pub fn from_bool(b: bool) -> SeparatorTactic {
52        if b {
53            SeparatorTactic::Always
54        } else {
55            SeparatorTactic::Never
56        }
57    }
58}
59
60/// Where to put separator.
61#[config_type]
62pub enum SeparatorPlace {
63    Front,
64    Back,
65}
66
67impl SeparatorPlace {
68    pub fn is_front(self) -> bool {
69        self == SeparatorPlace::Front
70    }
71
72    pub fn is_back(self) -> bool {
73        self == SeparatorPlace::Back
74    }
75
76    pub fn from_tactic(
77        default: SeparatorPlace,
78        tactic: DefinitiveListTactic,
79        sep: &str,
80    ) -> SeparatorPlace {
81        match tactic {
82            DefinitiveListTactic::Vertical => default,
83            _ => {
84                if sep == "," {
85                    SeparatorPlace::Back
86                } else {
87                    default
88                }
89            }
90        }
91    }
92}