Skip to content

Commit da53e20

Browse files
committed
Merge remote-tracking branch 'upstream/master' into win-test-crlf
2 parents 1a4b988 + 99f14a8 commit da53e20

21 files changed

+347
-218
lines changed

src/comment.rs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl FindUncommented for str {
464464
return Some(i - pat.len());
465465
}
466466
Some(c) => match kind {
467-
FullCodeCharKind::Normal if b == c => {}
467+
FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
468468
_ => {
469469
needle_iter = pat.chars();
470470
}
@@ -487,7 +487,7 @@ impl FindUncommented for str {
487487
pub fn find_comment_end(s: &str) -> Option<usize> {
488488
let mut iter = CharClasses::new(s.char_indices());
489489
for (kind, (i, _c)) in &mut iter {
490-
if kind == FullCodeCharKind::Normal {
490+
if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
491491
return Some(i);
492492
}
493493
}
@@ -505,6 +505,35 @@ pub fn contains_comment(text: &str) -> bool {
505505
CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
506506
}
507507

508+
/// Remove trailing spaces from the specified snippet. We do not remove spaces
509+
/// inside strings or comments.
510+
pub fn remove_trailing_white_spaces(text: &str) -> String {
511+
let mut buffer = String::with_capacity(text.len());
512+
let mut space_buffer = String::with_capacity(128);
513+
for (char_kind, c) in CharClasses::new(text.chars()) {
514+
match c {
515+
'\n' => {
516+
if char_kind == FullCodeCharKind::InString {
517+
buffer.push_str(&space_buffer);
518+
}
519+
space_buffer.clear();
520+
buffer.push('\n');
521+
}
522+
_ if c.is_whitespace() => {
523+
space_buffer.push(c);
524+
}
525+
_ => {
526+
if !space_buffer.is_empty() {
527+
buffer.push_str(&space_buffer);
528+
space_buffer.clear();
529+
}
530+
buffer.push(c);
531+
}
532+
}
533+
}
534+
buffer
535+
}
536+
508537
struct CharClasses<T>
509538
where
510539
T: Iterator,
@@ -568,15 +597,17 @@ enum FullCodeCharKind {
568597
InComment,
569598
/// Last character of a comment, '\n' for a line comment, '/' for a block comment.
570599
EndComment,
600+
/// Inside a string.
601+
InString,
571602
}
572603

573604
impl FullCodeCharKind {
574605
fn is_comment(&self) -> bool {
575606
match *self {
576-
FullCodeCharKind::Normal => false,
577607
FullCodeCharKind::StartComment |
578608
FullCodeCharKind::InComment |
579609
FullCodeCharKind::EndComment => true,
610+
_ => false,
580611
}
581612
}
582613

@@ -612,21 +643,34 @@ where
612643
fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
613644
let item = try_opt!(self.base.next());
614645
let chr = item.get_char();
646+
let mut char_kind = FullCodeCharKind::Normal;
615647
self.status = match self.status {
616648
CharClassesStatus::LitString => match chr {
617649
'"' => CharClassesStatus::Normal,
618-
'\\' => CharClassesStatus::LitStringEscape,
619-
_ => CharClassesStatus::LitString,
650+
'\\' => {
651+
char_kind = FullCodeCharKind::InString;
652+
CharClassesStatus::LitStringEscape
653+
}
654+
_ => {
655+
char_kind = FullCodeCharKind::InString;
656+
CharClassesStatus::LitString
657+
}
620658
},
621-
CharClassesStatus::LitStringEscape => CharClassesStatus::LitString,
659+
CharClassesStatus::LitStringEscape => {
660+
char_kind = FullCodeCharKind::InString;
661+
CharClassesStatus::LitString
662+
}
622663
CharClassesStatus::LitChar => match chr {
623664
'\\' => CharClassesStatus::LitCharEscape,
624665
'\'' => CharClassesStatus::Normal,
625666
_ => CharClassesStatus::LitChar,
626667
},
627668
CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
628669
CharClassesStatus::Normal => match chr {
629-
'"' => CharClassesStatus::LitString,
670+
'"' => {
671+
char_kind = FullCodeCharKind::InString;
672+
CharClassesStatus::LitString
673+
}
630674
'\'' => CharClassesStatus::LitChar,
631675
'/' => match self.base.peek() {
632676
Some(next) if next.get_char() == '*' => {
@@ -680,7 +724,7 @@ where
680724
}
681725
},
682726
};
683-
Some((FullCodeCharKind::Normal, item))
727+
Some((char_kind, item))
684728
}
685729
}
686730

@@ -707,9 +751,12 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
707751
fn next(&mut self) -> Option<Self::Item> {
708752
let (kind, (start_idx, _)) = try_opt!(self.iter.next());
709753
match kind {
710-
FullCodeCharKind::Normal => {
754+
FullCodeCharKind::Normal | FullCodeCharKind::InString => {
711755
// Consume all the Normal code
712-
while let Some(&(FullCodeCharKind::Normal, (_, _))) = self.iter.peek() {
756+
while let Some(&(char_kind, _)) = self.iter.peek() {
757+
if char_kind.is_comment() {
758+
break;
759+
}
713760
let _ = self.iter.next();
714761
}
715762
}
@@ -1032,7 +1079,7 @@ mod test {
10321079
fn uncommented(text: &str) -> String {
10331080
CharClasses::new(text.chars())
10341081
.filter_map(|(s, c)| match s {
1035-
FullCodeCharKind::Normal => Some(c),
1082+
FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
10361083
_ => None,
10371084
})
10381085
.collect()

src/expr.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cmp::{min, Ordering};
11+
use std::cmp::min;
1212
use std::borrow::Cow;
1313
use std::fmt::Write;
1414
use std::iter::{repeat, ExactSizeIterator};
@@ -72,12 +72,7 @@ pub fn format_expr(
7272
shape,
7373
false,
7474
),
75-
ast::ExprKind::Lit(ref l) => match l.node {
76-
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
77-
rewrite_string_lit(context, l.span, shape)
78-
}
79-
_ => Some(context.snippet(expr.span)),
80-
},
75+
ast::ExprKind::Lit(ref l) => rewrite_literal(context, l, shape),
8176
ast::ExprKind::Call(ref callee, ref args) => {
8277
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
8378
let callee_str = try_opt!(callee.rewrite(context, shape));
@@ -1938,6 +1933,13 @@ fn rewrite_pat_expr(
19381933
.map(|expr_rw| format!("\n{}{}", nested_indent_str, expr_rw))
19391934
}
19401935

1936+
pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option<String> {
1937+
match l.node {
1938+
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
1939+
_ => Some(context.snippet(l.span)),
1940+
}
1941+
}
1942+
19411943
fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Option<String> {
19421944
let string_lit = context.snippet(span);
19431945

@@ -1974,20 +1976,10 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
19741976
return Some(string_lit);
19751977
}
19761978

1977-
let fmt = StringFormat {
1978-
opener: "\"",
1979-
closer: "\"",
1980-
line_start: " ",
1981-
line_end: "\\",
1982-
shape: shape,
1983-
trim_end: false,
1984-
config: context.config,
1985-
};
1986-
19871979
// Remove the quote characters.
19881980
let str_lit = &string_lit[1..string_lit.len() - 1];
19891981

1990-
rewrite_string(str_lit, &fmt)
1982+
rewrite_string(str_lit, &StringFormat::new(shape, context.config))
19911983
}
19921984

19931985
fn string_requires_rewrite(
@@ -2033,7 +2025,7 @@ pub fn rewrite_call(
20332025
shape,
20342026
context.config.fn_call_width(),
20352027
force_trailing_comma,
2036-
).ok()
2028+
)
20372029
}
20382030

20392031
pub fn rewrite_call_inner<'a, T>(
@@ -2044,7 +2036,7 @@ pub fn rewrite_call_inner<'a, T>(
20442036
shape: Shape,
20452037
args_max_width: usize,
20462038
force_trailing_comma: bool,
2047-
) -> Result<String, Ordering>
2039+
) -> Option<String>
20482040
where
20492041
T: Rewrite + Spanned + ToExpr + 'a,
20502042
{
@@ -2055,30 +2047,27 @@ where
20552047
1
20562048
};
20572049
let used_width = extra_offset(callee_str, shape);
2058-
let one_line_width = shape
2059-
.width
2060-
.checked_sub(used_width + 2 * paren_overhead)
2061-
.ok_or(Ordering::Greater)?;
2050+
let one_line_width = try_opt!(shape.width.checked_sub(used_width + 2 * paren_overhead));
20622051

2063-
let nested_shape = shape_from_fn_call_style(
2052+
let nested_shape = try_opt!(shape_from_fn_call_style(
20642053
context,
20652054
shape,
20662055
used_width + 2 * paren_overhead,
20672056
used_width + paren_overhead,
2068-
).ok_or(Ordering::Greater)?;
2057+
));
20692058

20702059
let span_lo = context.codemap.span_after(span, "(");
20712060
let args_span = mk_sp(span_lo, span.hi());
20722061

2073-
let (extendable, list_str) = rewrite_call_args(
2062+
let (extendable, list_str) = try_opt!(rewrite_call_args(
20742063
context,
20752064
args,
20762065
args_span,
20772066
nested_shape,
20782067
one_line_width,
20792068
args_max_width,
20802069
force_trailing_comma,
2081-
).ok_or(Ordering::Less)?;
2070+
));
20822071

20832072
if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable {
20842073
let mut new_context = context.clone();
@@ -2094,10 +2083,8 @@ where
20942083
);
20952084
}
20962085

2097-
let args_shape = shape
2098-
.sub_width(last_line_width(callee_str))
2099-
.ok_or(Ordering::Less)?;
2100-
Ok(format!(
2086+
let args_shape = try_opt!(shape.sub_width(last_line_width(callee_str)));
2087+
Some(format!(
21012088
"{}{}",
21022089
callee_str,
21032090
wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape)
@@ -2805,7 +2792,7 @@ where
28052792
shape,
28062793
context.config.fn_call_width(),
28072794
force_trailing_comma,
2808-
).ok()
2795+
)
28092796
} else {
28102797
rewrite_tuple_in_visual_indent_style(context, items, span, shape)
28112798
}
@@ -2895,7 +2882,7 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
28952882
let rhs = try_opt!(choose_rhs(
28962883
context,
28972884
ex,
2898-
shape,
2885+
orig_shape,
28992886
ex.rewrite(context, orig_shape)
29002887
));
29012888
Some(lhs + &rhs)
@@ -2908,7 +2895,9 @@ fn choose_rhs(
29082895
orig_rhs: Option<String>,
29092896
) -> Option<String> {
29102897
match orig_rhs {
2911-
Some(ref new_str) if !new_str.contains('\n') => Some(format!(" {}", new_str)),
2898+
Some(ref new_str) if !new_str.contains('\n') && new_str.len() <= shape.width => {
2899+
Some(format!(" {}", new_str))
2900+
}
29122901
_ => {
29132902
// Expression did not fit on the same line as the identifier.
29142903
// Try splitting the line and see if that works better.

0 commit comments

Comments
 (0)