Merge pull request #781 from kyeah/itemmac

Format item macros
This commit is contained in:
Nick Cameron 2016-01-26 06:54:52 +13:00
commit 3b24f6f7c4
4 changed files with 32 additions and 25 deletions

View file

@ -33,7 +33,7 @@ use utils::{wrap_str, span_after};
static FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"]; static FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
// FIXME: use the enum from libsyntax? // FIXME: use the enum from libsyntax?
#[derive(Clone, Copy)] #[derive(Clone, Copy, PartialEq, Eq)]
enum MacroStyle { enum MacroStyle {
Parens, Parens,
Brackets, Brackets,
@ -63,9 +63,7 @@ pub fn rewrite_macro(mac: &ast::Mac,
original_style original_style
}; };
if let MacroStyle::Braces = style { if mac.node.tts.is_empty() {
return None;
} else if mac.node.tts.is_empty() {
return if let MacroStyle::Parens = style { return if let MacroStyle::Parens = style {
Some(format!("{}()", macro_name)) Some(format!("{}()", macro_name))
} else { } else {
@ -76,22 +74,24 @@ pub fn rewrite_macro(mac: &ast::Mac,
let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone(), Vec::new()); let mut parser = tts_to_parser(context.parse_session, mac.node.tts.clone(), Vec::new());
let mut expr_vec = Vec::new(); let mut expr_vec = Vec::new();
loop { if MacroStyle::Braces != style {
expr_vec.push(match parser.parse_expr() { loop {
Ok(expr) => expr, expr_vec.push(match parser.parse_expr() {
Err(..) => return None, Ok(expr) => expr,
}); Err(..) => return None,
});
match parser.token { match parser.token {
Token::Eof => break, Token::Eof => break,
Token::Comma => (), Token::Comma => (),
_ => return None, _ => return None,
} }
let _ = parser.bump(); let _ = parser.bump();
if parser.token == Token::Eof { if parser.token == Token::Eof {
return None; return None;
}
} }
} }

View file

@ -253,13 +253,9 @@ impl<'a> FmtVisitor<'a> {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
self.format_mod(module, item.vis, item.span, item.ident); self.format_mod(module, item.vis, item.span, item.ident);
} }
ast::Item_::ItemMac(..) => { ast::Item_::ItemMac(ref mac) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);
let snippet = self.snippet(item.span); self.visit_mac(mac);
self.buffer.push_str(&snippet);
self.last_pos = item.span.hi;
// FIXME: we cannot format these yet, because of a bad span.
// See rust lang issue #28424.
} }
ast::Item_::ItemForeignMod(ref foreign_mod) => { ast::Item_::ItemForeignMod(ref foreign_mod) => {
self.format_missing_with_indent(item.span.lo); self.format_missing_with_indent(item.span.lo);

View file

@ -1,4 +1,8 @@
itemmacro!(this, is.not() .formatted(yet)); itemmacro!(this, is.now() .formatted(yay));
itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());
itemmacro!{this, is.bracket().formatted()}
fn main() { fn main() {
foo! ( ); foo! ( );

View file

@ -1,4 +1,11 @@
itemmacro!(this, is.not() .formatted(yet)); itemmacro!(this, is.now().formatted(yay));
itemmacro!(really,
long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb()
.is
.formatted());
itemmacro!{this, is.bracket().formatted()}
fn main() { fn main() {
foo!(); foo!();