Skip to content

Commit f8586ba

Browse files
committed
Handle comment at the last element in write_list
1 parent be5c40c commit f8586ba

File tree

5 files changed

+72
-131
lines changed

5 files changed

+72
-131
lines changed

src/expr.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use {Indent, Shape, Spanned};
1616
use codemap::SpanUtils;
1717
use rewrite::{Rewrite, RewriteContext};
1818
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
19-
DefinitiveListTactic, definitive_tactic, ListItem, format_item_list, struct_lit_shape,
19+
DefinitiveListTactic, definitive_tactic, ListItem, struct_lit_shape,
2020
struct_lit_tactic, shape_for_tactic, struct_lit_formatting};
2121
use string::{StringFormat, rewrite_string};
2222
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
2323
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
24-
colon_spaces, contains_skip, mk_sp, last_line_extendable};
24+
colon_spaces, contains_skip, mk_sp, last_line_extendable, paren_overhead};
2525
use visitor::FmtVisitor;
2626
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
2727
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -2217,7 +2217,7 @@ where
22172217
context.config.trailing_comma()
22182218
},
22192219
shape: shape,
2220-
ends_with_newline: false,
2220+
ends_with_newline: context.use_block_indent() && tactic == DefinitiveListTactic::Vertical,
22212221
config: context.config,
22222222
};
22232223

@@ -2436,14 +2436,6 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
24362436
}
24372437
}
24382438

2439-
fn paren_overhead(context: &RewriteContext) -> usize {
2440-
if context.config.spaces_within_parens() {
2441-
4
2442-
} else {
2443-
2
2444-
}
2445-
}
2446-
24472439
pub fn wrap_args_with_parens(
24482440
context: &RewriteContext,
24492441
args_str: &str,
@@ -2813,7 +2805,21 @@ where
28132805
list_lo,
28142806
span.hi - BytePos(1),
28152807
);
2816-
let list_str = try_opt!(format_item_list(items, nested_shape, context.config));
2808+
let item_vec: Vec<_> = items.collect();
2809+
let tactic = definitive_tactic(
2810+
&item_vec,
2811+
ListTactic::HorizontalVertical,
2812+
nested_shape.width,
2813+
);
2814+
let fmt = ListFormatting {
2815+
tactic: tactic,
2816+
separator: ",",
2817+
trailing_separator: SeparatorTactic::Never,
2818+
shape: shape,
2819+
ends_with_newline: false,
2820+
config: context.config,
2821+
};
2822+
let list_str = try_opt!(write_list(&item_vec, &fmt));
28172823

28182824
if context.config.spaces_within_parens() && list_str.len() > 0 {
28192825
Some(format!("( {} )", list_str))
@@ -3023,3 +3029,14 @@ impl<'a> ToExpr for TuplePatField<'a> {
30233029
can_be_overflowed_pat(context, self, len)
30243030
}
30253031
}
3032+
3033+
impl<'a> ToExpr for ast::StructField {
3034+
fn to_expr(&self) -> Option<&ast::Expr> {
3035+
None
3036+
}
3037+
3038+
#[allow(unused_variables)]
3039+
fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
3040+
false
3041+
}
3042+
}

src/items.rs

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ use codemap::SpanUtils;
1515
use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str,
1616
last_line_width, format_unsafety, trim_newlines, stmt_expr, semicolon_for_expr,
1717
trimmed_last_line_width, colon_spaces, mk_sp};
18-
use lists::{write_list, itemize_list, definitive_tactic, ListItem, ListFormatting,
19-
SeparatorTactic, DefinitiveListTactic, ListTactic};
20-
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, ExprType};
18+
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic,
19+
DefinitiveListTactic, ListTactic, definitive_tactic};
20+
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
21+
rewrite_call_inner, ExprType};
2122
use comment::{FindUncommented, contains_comment, rewrite_comment, recover_comment_removed};
2223
use visitor::FmtVisitor;
2324
use rewrite::{Rewrite, RewriteContext};
@@ -1216,91 +1217,19 @@ fn format_tuple_struct(
12161217
}
12171218
result.push(')');
12181219
} else {
1219-
let (tactic, item_indent) = match context.config.fn_args_layout() {
1220-
IndentStyle::Visual => {
1221-
// 1 = `(`
1222-
(
1223-
ListTactic::HorizontalVertical,
1224-
offset.block_only() + result.len() + 1,
1225-
)
1226-
}
1227-
IndentStyle::Block => {
1228-
(
1229-
if result.contains('\n') {
1230-
ListTactic::Vertical
1231-
} else {
1232-
ListTactic::HorizontalVertical
1233-
},
1234-
offset.block_only().block_indent(&context.config),
1235-
)
1236-
}
1237-
};
12381220
// 3 = `();`
1239-
let item_budget = try_opt!(
1240-
context
1241-
.config
1242-
.max_width()
1243-
.checked_sub(item_indent.width() + 3)
1244-
);
1245-
1246-
let items = itemize_list(
1247-
context.codemap,
1248-
fields.iter(),
1249-
")",
1250-
|field| {
1251-
// Include attributes and doc comments, if present
1252-
if !field.attrs.is_empty() {
1253-
field.attrs[0].span.lo
1254-
} else {
1255-
field.span.lo
1256-
}
1257-
},
1258-
|field| field.ty.span.hi,
1259-
|field| {
1260-
rewrite_struct_field(context, field, Shape::legacy(item_budget, item_indent), 0)
1261-
},
1262-
context.codemap.span_after(span, "("),
1263-
span.hi,
1264-
);
1265-
let body_budget = try_opt!(
1266-
context
1267-
.config
1268-
.max_width()
1269-
.checked_sub(offset.block_only().width() + last_line_width(&result) + 3)
1221+
let body = try_opt!(
1222+
rewrite_call_inner(
1223+
context,
1224+
"",
1225+
&fields.iter().map(|field| field).collect::<Vec<_>>()[..],
1226+
span,
1227+
Shape::legacy(context.budget(last_line_width(&result) + 3), offset),
1228+
context.config.fn_call_width(),
1229+
false,
1230+
).ok()
12701231
);
1271-
1272-
let item_vec: Vec<_> = items.collect();
1273-
let tactic = definitive_tactic(&item_vec, tactic, body_budget);
1274-
let fmt = ListFormatting {
1275-
tactic: tactic,
1276-
separator: ",",
1277-
trailing_separator: context.config.trailing_comma(),
1278-
shape: Shape::indented(item_indent, context.config),
1279-
ends_with_newline: false,
1280-
config: context.config,
1281-
};
1282-
let body = try_opt!(write_list(&item_vec, &fmt));
1283-
1284-
if context.config.fn_args_layout() == IndentStyle::Visual || !body.contains('\n') {
1285-
result.push('(');
1286-
if context.config.spaces_within_parens() && body.len() > 0 {
1287-
result.push(' ');
1288-
}
1289-
1290-
result.push_str(&body);
1291-
1292-
if context.config.spaces_within_parens() && body.len() > 0 {
1293-
result.push(' ');
1294-
}
1295-
result.push(')');
1296-
} else {
1297-
result.push_str("(\n");
1298-
result.push_str(&item_indent.to_string(&context.config));
1299-
result.push_str(&body);
1300-
result.push('\n');
1301-
result.push_str(&offset.block_only().to_string(&context.config));
1302-
result.push(')');
1303-
}
1232+
result.push_str(&body);
13041233
}
13051234

13061235
if !where_clause_str.is_empty() && !where_clause_str.contains('\n') &&
@@ -2422,7 +2351,7 @@ fn rewrite_generics(
24222351
span: Span,
24232352
) -> Option<String> {
24242353
let g_shape = try_opt!(generics_shape_from_config(context.config, shape, 0));
2425-
let one_line_width = try_opt!(shape.width.checked_sub(2));
2354+
let one_line_width = shape.width.checked_sub(2).unwrap_or(0);
24262355
rewrite_generics_inner(context, generics, g_shape, one_line_width, span).or_else(|| {
24272356
rewrite_generics_inner(context, generics, g_shape, 0, span)
24282357
})
@@ -2497,16 +2426,19 @@ where
24972426
{
24982427
let item_vec = items.collect::<Vec<_>>();
24992428

2429+
let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, one_line_budget);
2430+
let ends_with_newline = context.config.generics_indent() == IndentStyle::Block &&
2431+
tactic == DefinitiveListTactic::Vertical;
25002432
let fmt = ListFormatting {
2501-
tactic: definitive_tactic(&item_vec, ListTactic::HorizontalVertical, one_line_budget),
2433+
tactic: tactic,
25022434
separator: ",",
25032435
trailing_separator: if context.config.generics_indent() == IndentStyle::Visual {
25042436
SeparatorTactic::Never
25052437
} else {
25062438
context.config.trailing_comma()
25072439
},
25082440
shape: shape,
2509-
ends_with_newline: false,
2441+
ends_with_newline: ends_with_newline,
25102442
config: context.config,
25112443
};
25122444

@@ -2735,8 +2667,9 @@ fn format_generics(
27352667
force_same_line_brace: bool,
27362668
offset: Indent,
27372669
span: Span,
2670+
used_width: usize,
27382671
) -> Option<String> {
2739-
let shape = Shape::indented(offset, context.config);
2672+
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
27402673
let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
27412674

27422675
if !generics.where_clause.predicates.is_empty() || result.contains('\n') {

src/lists.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::iter::Peekable;
1414
use syntax::codemap::{CodeMap, BytePos};
1515

1616
use {Indent, Shape};
17-
use comment::{FindUncommented, rewrite_comment, find_comment_end};
17+
use comment::{find_comment_end, rewrite_comment, FindUncommented};
1818
use config::{Config, IndentStyle};
1919
use rewrite::RewriteContext;
2020
use utils::mk_sp;
@@ -68,31 +68,6 @@ pub struct ListFormatting<'a> {
6868
pub config: &'a Config,
6969
}
7070

71-
pub fn format_item_list<I>(items: I, shape: Shape, config: &Config) -> Option<String>
72-
where
73-
I: Iterator<Item = ListItem>,
74-
{
75-
list_helper(items, shape, config, ListTactic::HorizontalVertical)
76-
}
77-
78-
pub fn list_helper<I>(items: I, shape: Shape, config: &Config, tactic: ListTactic) -> Option<String>
79-
where
80-
I: Iterator<Item = ListItem>,
81-
{
82-
let item_vec: Vec<_> = items.collect();
83-
let tactic = definitive_tactic(&item_vec, tactic, shape.width);
84-
let fmt = ListFormatting {
85-
tactic: tactic,
86-
separator: ",",
87-
trailing_separator: SeparatorTactic::Never,
88-
shape: shape,
89-
ends_with_newline: false,
90-
config: config,
91-
};
92-
93-
write_list(&item_vec, &fmt)
94-
}
95-
9671
impl AsRef<ListItem> for ListItem {
9772
fn as_ref(&self) -> &ListItem {
9873
self
@@ -118,10 +93,13 @@ impl ListItem {
11893
.map_or(false, |s| s.contains('\n'))
11994
}
12095

121-
pub fn has_line_pre_comment(&self) -> bool {
96+
pub fn has_comment(&self) -> bool {
12297
self.pre_comment
12398
.as_ref()
124-
.map_or(false, |comment| comment.starts_with("//"))
99+
.map_or(false, |comment| comment.starts_with("//")) ||
100+
self.post_comment
101+
.as_ref()
102+
.map_or(false, |comment| comment.starts_with("//"))
125103
}
126104

127105
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
@@ -150,7 +128,7 @@ where
150128
let pre_line_comments = items
151129
.clone()
152130
.into_iter()
153-
.any(|item| item.as_ref().has_line_pre_comment());
131+
.any(|item| item.as_ref().has_comment());
154132

155133
let limit = match tactic {
156134
_ if pre_line_comments => return DefinitiveListTactic::Vertical,

src/rewrite.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ impl<'a> RewriteContext<'a> {
4545
pub fn use_block_indent(&self) -> bool {
4646
self.config.fn_call_style() == IndentStyle::Block || self.use_block
4747
}
48+
49+
pub fn budget(&self, used_width: usize) -> usize {
50+
self.config.max_width().checked_sub(used_width).unwrap_or(0)
51+
}
4852
}

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ pub fn colon_spaces(before: bool, after: bool) -> &'static str {
393393
}
394394
}
395395

396+
#[inline]
397+
pub fn paren_overhead(context: &RewriteContext) -> usize {
398+
if context.config.spaces_within_parens() {
399+
4
400+
} else {
401+
2
402+
}
403+
}
404+
396405
#[test]
397406
fn bin_search_test() {
398407
let closure = |i| match i {

0 commit comments

Comments
 (0)