Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit baa20df

Browse files
committed
Format a macro call with a single item-like argument
1 parent a925bdf commit baa20df

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/macros.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use shape::{Indent, Shape};
4444
use source_map::SpanUtils;
4545
use spanned::Spanned;
4646
use utils::{format_visibility, mk_sp, rewrite_ident, wrap_str};
47+
use visitor::FmtVisitor;
4748

4849
const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
4950

@@ -271,6 +272,24 @@ pub fn rewrite_macro_inner(
271272
}
272273
}
273274

275+
if !arg_vec.is_empty() && arg_vec.iter().all(|arg| {
276+
if let MacroArg::Item(..) = arg {
277+
true
278+
} else {
279+
false
280+
}
281+
}) {
282+
return rewrite_macro_with_items(
283+
context,
284+
&arg_vec,
285+
&macro_name,
286+
shape,
287+
style,
288+
position,
289+
mac.span,
290+
);
291+
}
292+
274293
match style {
275294
DelimToken::Paren => {
276295
// Format macro invocation as function call, preserve the trailing
@@ -1428,3 +1447,45 @@ fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream)
14281447

14291448
Some(result)
14301449
}
1450+
1451+
fn rewrite_macro_with_items(
1452+
context: &RewriteContext,
1453+
items: &[MacroArg],
1454+
macro_name: &str,
1455+
shape: Shape,
1456+
style: DelimToken,
1457+
position: MacroPosition,
1458+
span: Span,
1459+
) -> Option<String> {
1460+
let (opener, closer) = match style {
1461+
DelimToken::Paren => ("(", ")"),
1462+
DelimToken::Bracket => ("[", "]"),
1463+
DelimToken::Brace => (" {", "}"),
1464+
_ => return None,
1465+
};
1466+
let trailing_semicolon = match style {
1467+
DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
1468+
_ => "",
1469+
};
1470+
1471+
let mut visitor = FmtVisitor::from_context(context);
1472+
visitor.block_indent = shape.indent.block_indent(context.config);
1473+
visitor.last_pos = context.snippet_provider.span_after(span, opener.trim());
1474+
for item in items {
1475+
let item = match item {
1476+
MacroArg::Item(item) => item,
1477+
_ => return None,
1478+
};
1479+
visitor.visit_item(&item);
1480+
}
1481+
1482+
let mut result = String::with_capacity(256);
1483+
result.push_str(&macro_name);
1484+
result.push_str(opener);
1485+
result.push_str(&visitor.block_indent.to_string_with_newline(context.config));
1486+
result.push_str(visitor.buffer.trim());
1487+
result.push_str(&shape.indent.to_string_with_newline(context.config));
1488+
result.push_str(closer);
1489+
result.push_str(trailing_semicolon);
1490+
return Some(result);
1491+
}

0 commit comments

Comments
 (0)