Disable formatting macro-def if args do not fit on one line

This commit is contained in:
topecongiro 2018-02-14 04:00:26 +09:00
parent 67d36c7019
commit b49b30746d
3 changed files with 37 additions and 6 deletions

View File

@ -431,7 +431,8 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
// and `(`/`)` have special meaning.
//
// We always try and format on one line.
fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
// FIXME: Use multi-line when every thing does not fit on one line.
fn format_macro_args(toks: ThinTokenStream, shape: Shape) -> Option<String> {
let mut result = String::with_capacity(128);
let mut insert_space = SpaceState::Never;
@ -459,7 +460,7 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
if let SpaceState::Always = insert_space {
result.push(' ');
}
let formatted = format_macro_args(d.tts)?;
let formatted = format_macro_args(d.tts, shape)?;
match d.delim {
DelimToken::Paren => {
result.push_str(&format!("({})", formatted));
@ -482,7 +483,11 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
}
}
Some(result)
if result.len() <= shape.width {
Some(result)
} else {
None
}
}
// We should insert a space if the next token is a:
@ -617,7 +622,7 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
/// a,
/// b,
/// c,
// ),
/// ),
/// }
/// ```
fn indent_macro_snippet(
@ -757,7 +762,8 @@ fn rewrite(
return None;
}
let mut result = format_macro_args(self.args.clone())?;
// 5 = " => {"
let mut result = format_macro_args(self.args.clone(), shape.sub_width(5)?)?;
if multi_branch_style {
result += " =>";
@ -847,7 +853,12 @@ fn format_macro_args_str(s: &str) -> String {
&ParseSess::new(FilePathMapping::empty()),
None,
);
format_macro_args(input.into()).unwrap()
let shape = Shape {
width: 100,
indent: Indent::empty(),
offset: 0,
};
format_macro_args(input.into(), shape).unwrap()
}
#[test]

View File

@ -49,3 +49,13 @@ macro_rules! m {
mod macro_item { struct $item ; }
}
}
// #2439
macro_rules! m {
(
$line0_xxxxxxxxxxxxxxxxx: expr,
$line1_xxxxxxxxxxxxxxxxx: expr,
$line2_xxxxxxxxxxxxxxxxx: expr,
$line3_xxxxxxxxxxxxxxxxx: expr,
) => {};
}

View File

@ -41,3 +41,13 @@ mod macro_item {
}
}
}
// #2439
macro_rules! m {
(
$line0_xxxxxxxxxxxxxxxxx: expr,
$line1_xxxxxxxxxxxxxxxxx: expr,
$line2_xxxxxxxxxxxxxxxxx: expr,
$line3_xxxxxxxxxxxxxxxxx: expr,
) => {};
}