Skip to content

Commit

Permalink
Merge pull request rust-lang#3080 from topecongiro/issue-3031
Browse files Browse the repository at this point in the history
Format macro calls with item-like arguments
  • Loading branch information
nrc authored Oct 8, 2018
2 parents 1b2e727 + 70177a0 commit ceac483
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
65 changes: 65 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use shape::{Indent, Shape};
use source_map::SpanUtils;
use spanned::Spanned;
use utils::{format_visibility, mk_sp, rewrite_ident, wrap_str};
use visitor::FmtVisitor;

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

Expand All @@ -63,6 +64,15 @@ pub enum MacroArg {
Item(ptr::P<ast::Item>),
}

impl MacroArg {
fn is_item(&self) -> bool {
match self {
MacroArg::Item(..) => true,
_ => false,
}
}
}

impl Rewrite for ast::Item {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
let mut visitor = ::visitor::FmtVisitor::from_context(context);
Expand Down Expand Up @@ -259,6 +269,7 @@ pub fn rewrite_macro_inner(
}
return return_original_snippet_with_failure_marked(context, mac.span);
}
_ if arg_vec.last().map_or(false, MacroArg::is_item) => continue,
_ => return return_original_snippet_with_failure_marked(context, mac.span),
}

Expand All @@ -271,6 +282,18 @@ pub fn rewrite_macro_inner(
}
}

if !arg_vec.is_empty() && arg_vec.iter().all(MacroArg::is_item) {
return rewrite_macro_with_items(
context,
&arg_vec,
&macro_name,
shape,
style,
position,
mac.span,
);
}

match style {
DelimToken::Paren => {
// Format macro invocation as function call, preserve the trailing
Expand Down Expand Up @@ -1428,3 +1451,45 @@ fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream)

Some(result)
}

fn rewrite_macro_with_items(
context: &RewriteContext,
items: &[MacroArg],
macro_name: &str,
shape: Shape,
style: DelimToken,
position: MacroPosition,
span: Span,
) -> Option<String> {
let (opener, closer) = match style {
DelimToken::Paren => ("(", ")"),
DelimToken::Bracket => ("[", "]"),
DelimToken::Brace => (" {", "}"),
_ => return None,
};
let trailing_semicolon = match style {
DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
_ => "",
};

let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = shape.indent.block_indent(context.config);
visitor.last_pos = context.snippet_provider.span_after(span, opener.trim());
for item in items {
let item = match item {
MacroArg::Item(item) => item,
_ => return None,
};
visitor.visit_item(&item);
}

let mut result = String::with_capacity(256);
result.push_str(&macro_name);
result.push_str(opener);
result.push_str(&visitor.block_indent.to_string_with_newline(context.config));
result.push_str(visitor.buffer.trim());
result.push_str(&shape.indent.to_string_with_newline(context.config));
result.push_str(closer);
result.push_str(trailing_semicolon);
return Some(result);
}
26 changes: 26 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,29 @@ named!(

// #2857
convert_args!(vec!(1, 2, 3));

// #3031
thread_local!(
/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new()) ;
) ;

thread_local![
/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new()) ;

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(0)) ;

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(), xxx, yyy) ;

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(1234)) ;

] ;
8 changes: 5 additions & 3 deletions tests/target/issue-2523.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
// Format items that appear as arguments of macro call.
//! ```rust
//! let x = 3;
//! some_macro!(pub fn foo() {
//! println!("Don't unindent me!");
//! });
//! some_macro!(
//! pub fn foo() {
//! println!("Don't unindent me!");
//! }
//! );
//! ```
23 changes: 23 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,26 @@ named!(

// #2857
convert_args!(vec!(1, 2, 3));

// #3031
thread_local!(
/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> = RefCell::new(RootedTraceableSet::new());
);

thread_local![
/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> = RefCell::new(RootedTraceableSet::new());

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(0));

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(), xxx, yyy);

/// TLV Holds a set of JSTraceables that need to be rooted
static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
RefCell::new(RootedTraceableSet::new(1234));
];

0 comments on commit ceac483

Please sign in to comment.