Skip to content

Commit c6b3cf9

Browse files
authored
Merge pull request #1983 from topecongiro/cleanup
Refactoring: clean up source code
2 parents e2c9e92 + 0779962 commit c6b3cf9

25 files changed

+627
-615
lines changed

src/chains.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
/// .qux
7777
/// ```
7878
79-
use Shape;
79+
use shape::Shape;
8080
use config::IndentStyle;
8181
use expr::rewrite_call;
8282
use macros::convert_try_mac;

src/comment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::{self, iter};
1414

1515
use syntax::codemap::Span;
1616

17-
use {Indent, Shape};
1817
use config::Config;
1918
use rewrite::RewriteContext;
19+
use shape::{Indent, Shape};
2020
use string::{rewrite_string, StringFormat};
2121
use utils::{first_line_width, last_line_width};
2222

@@ -928,7 +928,7 @@ fn remove_comment_header(comment: &str) -> &str {
928928
mod test {
929929
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
930930
FindUncommented, FullCodeCharKind};
931-
use {Indent, Shape};
931+
use shape::{Indent, Shape};
932932

933933
#[test]
934934
fn char_classes() {

src/expr.rs

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::{ast, ptr};
1717
use syntax::codemap::{BytePos, CodeMap, Span};
1818
use syntax::parse::classify;
1919

20-
use {Indent, Shape, Spanned};
20+
use spanned::Spanned;
2121
use chains::rewrite_chain;
2222
use codemap::{LineRangeUtils, SpanUtils};
2323
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
@@ -30,12 +30,13 @@ use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_format
3030
use macros::{rewrite_macro, MacroArg, MacroPosition};
3131
use patterns::{can_be_overflowed_pat, TuplePatField};
3232
use rewrite::{Rewrite, RewriteContext};
33+
use shape::{Indent, Shape};
3334
use string::{rewrite_string, StringFormat};
3435
use types::{can_be_overflowed_type, rewrite_path, PathContext};
3536
use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
3637
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes,
3738
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
38-
trimmed_last_line_width, wrap_str};
39+
trimmed_last_line_width};
3940
use vertical::rewrite_with_alignment;
4041
use visitor::FmtVisitor;
4142

@@ -75,11 +76,7 @@ pub fn format_expr(
7576
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
7677
rewrite_string_lit(context, l.span, shape)
7778
}
78-
_ => wrap_str(
79-
context.snippet(expr.span),
80-
context.config.max_width(),
81-
shape,
82-
),
79+
_ => Some(context.snippet(expr.span)),
8380
},
8481
ast::ExprKind::Call(ref callee, ref args) => {
8582
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
@@ -152,11 +149,7 @@ pub fn format_expr(
152149
Some(ident) => format!(" {}", ident.node),
153150
None => String::new(),
154151
};
155-
wrap_str(
156-
format!("continue{}", id_str),
157-
context.config.max_width(),
158-
shape,
159-
)
152+
Some(format!("continue{}", id_str))
160153
}
161154
ast::ExprKind::Break(ref opt_ident, ref opt_expr) => {
162155
let id_str = match *opt_ident {
@@ -167,17 +160,13 @@ pub fn format_expr(
167160
if let Some(ref expr) = *opt_expr {
168161
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
169162
} else {
170-
wrap_str(
171-
format!("break{}", id_str),
172-
context.config.max_width(),
173-
shape,
174-
)
163+
Some(format!("break{}", id_str))
175164
}
176165
}
177166
ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
178167
rewrite_unary_prefix(context, "yield ", &**expr, shape)
179168
} else {
180-
wrap_str("yield".to_string(), context.config.max_width(), shape)
169+
Some("yield".to_string())
181170
},
182171
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
183172
rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
@@ -189,17 +178,10 @@ pub fn format_expr(
189178
ast::ExprKind::Mac(ref mac) => {
190179
// Failure to rewrite a marco should not imply failure to
191180
// rewrite the expression.
192-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
193-
wrap_str(
194-
context.snippet(expr.span),
195-
context.config.max_width(),
196-
shape,
197-
)
198-
})
199-
}
200-
ast::ExprKind::Ret(None) => {
201-
wrap_str("return".to_owned(), context.config.max_width(), shape)
181+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
182+
.or_else(|| Some(context.snippet(expr.span)))
202183
}
184+
ast::ExprKind::Ret(None) => Some("return".to_owned()),
203185
ast::ExprKind::Ret(Some(ref expr)) => {
204186
rewrite_unary_prefix(context, "return ", &**expr, shape)
205187
}
@@ -301,16 +283,14 @@ pub fn format_expr(
301283
};
302284
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
303285
}
304-
(None, None) => wrap_str(delim.into(), context.config.max_width(), shape),
286+
(None, None) => Some(delim.into()),
305287
}
306288
}
307289
// We do not format these expressions yet, but they should still
308290
// satisfy our width restrictions.
309-
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => wrap_str(
310-
context.snippet(expr.span),
311-
context.config.max_width(),
312-
shape,
313-
),
291+
ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => {
292+
Some(context.snippet(expr.span))
293+
}
314294
ast::ExprKind::Catch(ref block) => {
315295
if let rw @ Some(_) = rewrite_single_line_block(context, "do catch ", block, shape) {
316296
rw
@@ -382,7 +362,11 @@ where
382362
.map(|first_line| first_line.ends_with('{'))
383363
.unwrap_or(false);
384364
if !rhs_result.contains('\n') || allow_same_line {
385-
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
365+
let one_line_width = last_line_width(&lhs_result) + infix.len()
366+
+ first_line_width(&rhs_result) + suffix.len();
367+
if one_line_width <= shape.width {
368+
return Some(format!("{}{}{}{}", lhs_result, infix, rhs_result, suffix));
369+
}
386370
}
387371
}
388372

@@ -2231,12 +2215,23 @@ where
22312215
_ if args.len() >= 1 => {
22322216
item_vec[args.len() - 1].item = args.last()
22332217
.and_then(|last_arg| last_arg.rewrite(context, shape));
2234-
tactic = definitive_tactic(
2235-
&*item_vec,
2236-
ListTactic::LimitedHorizontalVertical(args_max_width),
2237-
Separator::Comma,
2238-
one_line_width,
2239-
);
2218+
// Use horizontal layout for a function with a single argument as long as
2219+
// everything fits in a single line.
2220+
if args.len() == 1
2221+
&& args_max_width != 0 // Vertical layout is forced.
2222+
&& !item_vec[0].has_comment()
2223+
&& !item_vec[0].inner_as_ref().contains('\n')
2224+
&& ::lists::total_item_width(&item_vec[0]) <= one_line_width
2225+
{
2226+
tactic = DefinitiveListTactic::Horizontal;
2227+
} else {
2228+
tactic = definitive_tactic(
2229+
&*item_vec,
2230+
ListTactic::LimitedHorizontalVertical(args_max_width),
2231+
Separator::Comma,
2232+
one_line_width,
2233+
);
2234+
}
22402235
}
22412236
_ => (),
22422237
}
@@ -2664,11 +2659,7 @@ pub fn rewrite_field(
26642659
prefix_max_width: usize,
26652660
) -> Option<String> {
26662661
if contains_skip(&field.attrs) {
2667-
return wrap_str(
2668-
context.snippet(field.span()),
2669-
context.config.max_width(),
2670-
shape,
2671-
);
2662+
return Some(context.snippet(field.span()));
26722663
}
26732664
let name = &field.ident.node.to_string();
26742665
if field.is_shorthand {

src/file_lines.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ impl FileLines {
167167
}
168168

169169
/// `FileLines` files iterator.
170-
pub struct Files<'a>(
171-
Option<::std::collections::hash_map::Keys<'a, String, Vec<Range>>>,
172-
);
170+
pub struct Files<'a>(Option<::std::collections::hash_map::Keys<'a, String, Vec<Range>>>);
173171

174172
impl<'a> iter::Iterator for Files<'a> {
175173
type Item = &'a String;

src/imports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ use std::cmp::Ordering;
1313
use syntax::ast;
1414
use syntax::codemap::{BytePos, Span};
1515

16-
use {Shape, Spanned};
16+
use spanned::Spanned;
1717
use codemap::SpanUtils;
1818
use comment::combine_strs_with_missing_comments;
1919
use config::IndentStyle;
2020
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
2121
ListItem, Separator, SeparatorPlace, SeparatorTactic};
2222
use rewrite::{Rewrite, RewriteContext};
23+
use shape::Shape;
2324
use types::{rewrite_path, PathContext};
2425
use utils::{format_visibility, mk_sp};
2526
use visitor::{rewrite_extern_crate, FmtVisitor};

src/items.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::{abi, ast, ptr, symbol};
1616
use syntax::ast::ImplItem;
1717
use syntax::codemap::{BytePos, Span};
1818

19-
use {Indent, Shape, Spanned};
19+
use spanned::Spanned;
2020
use codemap::{LineRangeUtils, SpanUtils};
2121
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
2222
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
@@ -26,12 +26,13 @@ use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs
2626
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
2727
ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
2828
use rewrite::{Rewrite, RewriteContext};
29+
use shape::{Indent, Shape};
2930
use types::join_bounds;
3031
use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_abi,
3132
format_constness, format_defaultness, format_mutability, format_unsafety,
3233
format_visibility, is_attributes_extendable, last_line_contains_single_line_comment,
3334
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr,
34-
trim_newlines, trimmed_last_line_width, wrap_str};
35+
trim_newlines, trimmed_last_line_width};
3536
use vertical::rewrite_with_alignment;
3637
use visitor::FmtVisitor;
3738

@@ -1360,8 +1361,7 @@ pub fn rewrite_struct_field(
13601361
lhs_max_width: usize,
13611362
) -> Option<String> {
13621363
if contains_skip(&field.attrs) {
1363-
let span = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi()));
1364-
return wrap_str(span, context.config.max_width(), shape);
1364+
return Some(context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())));
13651365
}
13661366

13671367
let type_annotation_spacing = type_annotation_spacing(context.config);

0 commit comments

Comments
 (0)