-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathformat_collection.rs
More file actions
176 lines (161 loc) 路 6.47 KB
/
Copy pathformat_collection.rs
File metadata and controls
176 lines (161 loc) 路 6.47 KB
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::{
existing_whitespace::{ExistingWhitespace, TrailingWhitespace},
format::{format_cst, CstExtension, FormattingInfo},
formatted_cst::FormattedCst,
text_edits::TextEdits,
width::{SinglelineWidth, Width},
};
use candy_frontend::{cst::Cst, position::Offset};
use itertools::Itertools;
pub fn format_collection<'a>(
edits: &mut TextEdits,
previous_width: Width,
opening_punctuation: &Cst,
items: &[Cst],
closing_punctuation: &'a Cst,
is_comma_required_for_single_item: bool,
info: &FormattingInfo,
) -> FormattedCst<'a> {
let (info, uses_sandwich_like_multiline_formatting) =
info.resolve_for_sandwich_like(previous_width, SinglelineWidth::PARENTHESIS.into());
let opening_punctuation = format_cst(edits, previous_width, opening_punctuation, &info);
let closing_punctuation = format_cst(
edits,
Width::multiline(None, info.indentation.width()),
closing_punctuation,
&info,
);
let mut min_width = info.indentation.width()
+ opening_punctuation.min_width(info.indentation)
+ closing_punctuation.min_width(info.indentation);
let previous_width_for_items = Width::multiline(None, info.indentation.with_indent().width());
let item_info = info
.with_indent()
.with_trailing_comma_condition(TrailingCommaCondition::Always);
let items = items
.iter()
.enumerate()
.map(|(index, item)| {
let is_single_item = items.len() == 1;
let is_last_item = index == items.len() - 1;
let is_comma_required_due_to_single_item =
is_single_item && is_comma_required_for_single_item;
let is_comma_required =
is_comma_required_due_to_single_item || !is_last_item || item.has_comments();
let info = if !is_comma_required && let Width::Singleline(min_width) = min_width {
// We're looking at the last item and everything might fit in one line.
let max_width = Width::MAX - min_width;
assert!(!max_width.is_empty());
item_info
.with_trailing_comma_condition(TrailingCommaCondition::UnlessFitsIn(max_width))
} else {
item_info.clone()
};
let item = format_cst(edits, previous_width_for_items, item, &info);
if let Width::Singleline(old_min_width) = min_width
&& let Width::Singleline(item_min_width) = item.min_width(info.indentation)
{
let (item_min_width, max_width) = if is_last_item {
(item_min_width, Width::MAX)
} else {
// We need an additional column for the trailing space after the comma.
let item_min_width = item_min_width + SinglelineWidth::from(1);
// The last item needs at least one column of space.
let max_width = Width::MAX - SinglelineWidth::from(1);
(item_min_width, max_width)
};
min_width = Width::from_width_and_max(old_min_width + item_min_width, max_width);
} else {
min_width = Width::multiline(None, None);
}
item
})
.collect_vec();
let (opening_punctuation_trailing, item_trailing, last_item_trailing) =
if min_width.is_singleline() {
(
TrailingWhitespace::None,
TrailingWhitespace::Space,
TrailingWhitespace::None,
)
} else {
(
TrailingWhitespace::Indentation(info.indentation.with_indent()),
TrailingWhitespace::Indentation(info.indentation.with_indent()),
TrailingWhitespace::Indentation(info.indentation),
)
};
let last_item_index = items.len().checked_sub(1);
let (closing_punctuation_width, whitespace) = closing_punctuation.split();
FormattedCst::new_maybe_sandwich_like_multiline_formatting(
opening_punctuation.into_trailing(edits, opening_punctuation_trailing)
+ items
.into_iter()
.enumerate()
.map(|(index, item)| {
item.into_trailing(
edits,
if last_item_index == Some(index) {
last_item_trailing
} else {
item_trailing
},
)
})
.sum::<Width>()
+ closing_punctuation_width,
uses_sandwich_like_multiline_formatting,
uses_sandwich_like_multiline_formatting,
whitespace,
)
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TrailingCommaCondition {
Always,
/// Add a trailing comma if the element fits in a single line and is at most
/// this wide.
UnlessFitsIn(SinglelineWidth),
}
pub fn apply_trailing_comma_condition<'a>(
edits: &mut TextEdits,
previous_width: Width,
comma: Option<&'a Cst>,
fallback_offset: Offset,
info: &FormattingInfo,
min_width_except_comma: Width,
) -> (Width, ExistingWhitespace<'a>) {
let should_have_comma = match info.trailing_comma_condition {
Some(TrailingCommaCondition::Always) => true,
Some(TrailingCommaCondition::UnlessFitsIn(max_width)) => {
!min_width_except_comma.fits_in(max_width)
}
None => comma.is_some(),
};
if should_have_comma {
let whitespace = if let Some(comma) = comma {
let comma = format_cst(edits, previous_width, comma, info);
assert_eq!(comma.child_width(), SinglelineWidth::COMMA.into());
comma.whitespace
} else {
edits.insert(fallback_offset, ",");
ExistingWhitespace::empty(fallback_offset)
};
(SinglelineWidth::COMMA.into(), whitespace)
} else if let Some(comma) = comma {
if comma.has_comments() {
// This last item can't fit on one line, so we do have to keep the comma.
format_cst(edits, previous_width, comma, info).split()
} else {
edits.delete(comma.data.span.clone());
(
Width::default(),
ExistingWhitespace::empty(comma.data.span.end),
)
}
} else {
(Width::default(), ExistingWhitespace::empty(fallback_offset))
}
}
impl SinglelineWidth {
const COMMA: Self = Self::new_const(1);
}