Merge pull request from topecongiro/macro-trailing-comma

Preserve trailing comma of macro invocation
This commit is contained in:
Nick Cameron 2017-06-23 16:23:32 +12:00 committed by GitHub
commit 63076d7f21
5 changed files with 108 additions and 44 deletions

@ -93,6 +93,7 @@ pub fn format_expr(
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
context,
shape,
false,
)
}
ast::ExprKind::Lit(ref l) => {
@ -435,6 +436,7 @@ pub fn rewrite_array<'a, I>(
span: Span,
context: &RewriteContext,
shape: Shape,
trailing_comma: bool,
) -> Option<String>
where
I: Iterator<Item = &'a ast::Expr>,
@ -507,7 +509,13 @@ where
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
trailing_separator: SeparatorTactic::Never,
trailing_separator: if trailing_comma {
SeparatorTactic::Always
} else if context.inside_macro || context.config.array_layout() == IndentStyle::Visual {
SeparatorTactic::Never
} else {
SeparatorTactic::Vertical
},
shape: nested_shape,
ends_with_newline: false,
config: context.config,
@ -524,7 +532,7 @@ where
}
} else {
format!(
"[\n{}{},\n{}]",
"[\n{}{}\n{}]",
nested_shape.indent.to_string(context.config),
list_str,
shape.block().indent.to_string(context.config)

@ -30,7 +30,7 @@ use syntax::util::ThinVec;
use Shape;
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use expr::{rewrite_call, rewrite_array};
use expr::{rewrite_call_inner, rewrite_array};
use comment::{FindUncommented, contains_comment};
use utils::mk_sp;
@ -110,6 +110,7 @@ pub fn rewrite_macro(
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
let mut expr_vec = Vec::new();
let mut vec_with_semi = false;
let mut trailing_comma = false;
if MacroStyle::Braces != style {
loop {
@ -162,12 +163,8 @@ pub fn rewrite_macro(
parser.bump();
if parser.token == Token::Eof {
// vec! is a special case of bracket macro which should be formated as an array.
if macro_name == "vec!" {
break;
} else {
return None;
}
trailing_comma = true;
break;
}
}
}
@ -176,12 +173,19 @@ pub fn rewrite_macro(
MacroStyle::Parens => {
// Format macro invocation as function call, forcing no trailing
// comma because not all macros support them.
rewrite_call(context, &macro_name, &expr_vec, mac.span, shape).map(
|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
},
)
let rw = rewrite_call_inner(
context,
&macro_name,
&expr_vec.iter().map(|e| &**e).collect::<Vec<_>>()[..],
mac.span,
shape,
context.config.fn_call_width(),
trailing_comma,
);
rw.ok().map(|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
})
}
MacroStyle::Brackets => {
let mac_shape = try_opt!(shape.offset_left(macro_name.len()));
@ -215,7 +219,13 @@ pub fn rewrite_macro(
))
}
} else {
// Format macro invocation as array literal.
// If we are rewriting `vec!` macro or other special macros,
// then we can rewrite this as an usual array literal.
// Otherwise, we must preserve the original existence of trailing comma.
if FORCED_BRACKET_MACROS.contains(&&macro_name.as_str()) {
context.inside_macro = false;
trailing_comma = false;
}
let rewrite = try_opt!(rewrite_array(
expr_vec.iter().map(|x| &**x),
mk_sp(
@ -226,6 +236,7 @@ pub fn rewrite_macro(
),
context,
mac_shape,
trailing_comma,
));
Some(format!("{}{}", macro_name, rewrite))

@ -12,6 +12,8 @@ fn main() {
bar!( a , b , c );
bar!( a , b , c , );
baz!(1+2+3, quux. kaas());
quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB);
@ -85,11 +87,6 @@ fn main() {
10, 11, 12;
20, 21, 22);
// #1577
let json = json!({
"foo": "bar",
});
// #1092
chain!(input, a:take!(max_size), || []);
}
@ -98,11 +95,6 @@ impl X {
empty_invoc!{}
}
gfx_pipeline!(pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
});
fn issue_1279() {
println!("dsfs"); // a comment
}
@ -120,3 +112,30 @@ fn issue1178() {
foo!(#[doc = "bar"] baz);
}
fn issue1739() {
sql_function!(add_rss_item,
add_rss_item_t,
(a: types::Integer,
b: types::Timestamptz,
c: types::Text,
d: types::Text,
e: types::Text));
w.slice_mut(s![.., init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1], ..])
.par_map_inplace(|el| *el = 0.);
}
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
// at the end of the file for now.
// #1577
fn issue1577() {
let json = json!({
"foo": "bar",
});
}
gfx_pipeline!(pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
});

@ -17,6 +17,8 @@ fn main() {
bar!(a, b, c);
bar!(a, b, c,);
baz!(1 + 2 + 3, quux.kaas());
quux!(
@ -30,7 +32,7 @@ fn main() {
b /* another */
);
trailingcomma!( a , b , c , );
trailingcomma!(a, b, c,);
noexpr!( i am not an expression, OK? );
@ -81,9 +83,7 @@ fn main() {
];
vec![a; unsafe { x + 1 }];
unknown_bracket_macro__comma_should_not_be_stripped![
a,
];
unknown_bracket_macro__comma_should_not_be_stripped![a,];
foo(makro!(1, 3));
@ -116,11 +116,6 @@ fn main() {
10, 11, 12;
20, 21, 22);
// #1577
let json = json!({
"foo": "bar",
});
// #1092
chain!(input, a:take!(max_size), || []);
}
@ -129,19 +124,16 @@ impl X {
empty_invoc!{}
}
gfx_pipeline!(pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
});
fn issue_1279() {
println!("dsfs"); // a comment
}
fn issue_1555() {
let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
"65454654654654654654654655464",
"4");
let hello = &format!(
"HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
"65454654654654654654654655464",
"4"
);
}
fn issue1178() {
@ -151,3 +143,37 @@ fn issue1178() {
foo!(#[doc = "bar"] baz);
}
fn issue1739() {
sql_function!(
add_rss_item,
add_rss_item_t,
(
a: types::Integer,
b: types::Timestamptz,
c: types::Text,
d: types::Text,
e: types::Text,
)
);
w.slice_mut(s![
..,
init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1],
..
]).par_map_inplace(|el| *el = 0.);
}
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
// at the end of the file for now.
// #1577
fn issue1577() {
let json = json!({
"foo": "bar",
});
}
gfx_pipeline!(pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
});

@ -69,7 +69,7 @@ fn issue775() {
"b".to_string(),
Array(vec![
mk_object(
&[("c".to_string(), String("\x0c\r".to_string()))]
&[("c".to_string(), String("\x0c\r".to_string()))],
),
mk_object(&[("d".to_string(), String("".to_string()))]),
]),