1
Fork 0

Make string lit formatting fallible

This allows expressions containing string lits to wrap their content

Fixes #1282
This commit is contained in:
Nick Cameron 2017-01-27 08:04:13 +13:00
parent 309fb37f23
commit 1f3100d7bb
4 changed files with 39 additions and 18 deletions

View file

@ -64,7 +64,7 @@ fn format_expr(expr: &ast::Expr,
ast::ExprKind::Lit(ref l) => { ast::ExprKind::Lit(ref l) => {
match l.node { match l.node {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => { ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
Some(rewrite_string_lit(context, l.span, width, offset)) rewrite_string_lit(context, l.span, width, offset)
} }
_ => { _ => {
wrap_str(context.snippet(expr.span), wrap_str(context.snippet(expr.span),
@ -586,6 +586,7 @@ fn and_one_line(x: Option<String>) -> Option<String> {
} }
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> { fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
debug!("nop_block_collapse {:?} {}", block_str, budget);
block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 && block_str.map(|block_str| if block_str.starts_with('{') && budget >= 2 &&
(block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() == (block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() ==
block_str.len() - 2) { block_str.len() - 2) {
@ -1219,6 +1220,7 @@ fn arm_comma(config: &Config, arm: &ast::Arm, body: &ast::Expr) -> &'static str
// Match arms. // Match arms.
impl Rewrite for ast::Arm { impl Rewrite for ast::Arm {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
debug!("Arm::rewrite {:?} {} {:?}", self, width, offset);
let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self; let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self;
// FIXME this is all a bit grotty, would be nice to abstract out the // FIXME this is all a bit grotty, would be nice to abstract out the
@ -1310,13 +1312,14 @@ impl Rewrite for ast::Arm {
false false
}; };
let block_sep = match context.config.control_brace_style {
ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
_ => " ",
};
match rewrite { match rewrite {
Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms || Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
is_block => { is_block => {
let block_sep = match context.config.control_brace_style {
ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(),
_ => " ",
};
return Some(format!("{}{} =>{}{}{}", return Some(format!("{}{} =>{}{}{}",
attr_str.trim_left(), attr_str.trim_left(),
pats_str, pats_str,
@ -1477,16 +1480,16 @@ fn rewrite_string_lit(context: &RewriteContext,
span: Span, span: Span,
width: usize, width: usize,
offset: Indent) offset: Indent)
-> String { -> Option<String> {
let string_lit = context.snippet(span); let string_lit = context.snippet(span);
if !context.config.format_strings && !context.config.force_format_strings { if !context.config.format_strings && !context.config.force_format_strings {
return string_lit; return Some(string_lit);
} }
if !context.config.force_format_strings && if !context.config.force_format_strings &&
!string_requires_rewrite(context, span, &string_lit, width, offset) { !string_requires_rewrite(context, span, &string_lit, width, offset) {
return string_lit; return Some(string_lit);
} }
let fmt = StringFormat { let fmt = StringFormat {
@ -1503,7 +1506,7 @@ fn rewrite_string_lit(context: &RewriteContext,
// Remove the quote characters. // Remove the quote characters.
let str_lit = &string_lit[1..string_lit.len() - 1]; let str_lit = &string_lit[1..string_lit.len() - 1];
rewrite_string(str_lit, &fmt).unwrap_or_else(|| string_lit.to_owned()) rewrite_string(str_lit, &fmt)
} }
fn string_requires_rewrite(context: &RewriteContext, fn string_requires_rewrite(context: &RewriteContext,

View file

@ -45,3 +45,13 @@ fn issue716() {
println!("forall x. mult(e(), x) = x /\\ println!("forall x. mult(e(), x) = x /\\
forall x. mult(x, x) = e()"); forall x. mult(x, x) = e()");
} }
fn issue_1282() {
{
match foo {
Permission::AndroidPermissionAccessLocationExtraCommands => {
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
}
}
}
}

View file

@ -10,8 +10,10 @@ fn main() -> &'static str {
let too_many_lines = "Hello"; let too_many_lines = "Hello";
// Make sure we don't break after an escape character. // Make sure we don't break after an escape character.
let odd_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; let odd_length_name =
let even_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
let even_length_name =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
@ -22,7 +24,8 @@ formatting"#;
filename.replace(" ", "\\"); filename.replace(" ", "\\");
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = funktion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
funktion("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
let unicode = "a̐éö̲\r\n"; let unicode = "a̐éö̲\r\n";
let unicode2 = "Löwe 老虎 Léopard"; let unicode2 = "Löwe 老虎 Léopard";
@ -41,3 +44,13 @@ fn issue716() {
println!("forall x. mult(e(), x) = x /\\ println!("forall x. mult(e(), x) = x /\\
forall x. mult(x, x) = e()"); forall x. mult(x, x) = e()");
} }
fn issue_1282() {
{
match foo {
Permission::AndroidPermissionAccessLocationExtraCommands => {
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"
}
}
}
}

View file

@ -1,12 +1,7 @@
fn main() { fn main() {
println!("ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:\ println!("ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:\
Likethisssssssssssss"); Likethisssssssssssss");
format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}", format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",x,y,z,a,b);
x,
y,
z,
a,
b);
println!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaalhijalfhiigjapdighjapdigjapdighdapighapdighpaidhg;\ println!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaalhijalfhiigjapdighjapdigjapdighdapighapdighpaidhg;\
adopgihadoguaadbadgad,qeoihapethae8t0aet8haetadbjtaeg;\ adopgihadoguaadbadgad,qeoihapethae8t0aet8haetadbjtaeg;\
ooeouthaoeutgadlgajduabgoiuadogabudogubaodugbadgadgadga;adoughaoeugbaouea"); ooeouthaoeutgadlgajduabgoiuadogabudogubaodugbadgadgadga;adoughaoeugbaouea");