Format item macros

This commit is contained in:
Kevin Yeh 2016-01-22 13:40:26 -06:00
parent 81516fe8ca
commit f01ed6f507
4 changed files with 21 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,6 +74,7 @@ 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();
if MacroStyle::Braces != style {
loop { loop {
expr_vec.push(match parser.parse_expr() { expr_vec.push(match parser.parse_expr() {
Ok(expr) => expr, Ok(expr) => expr,
@ -94,6 +93,7 @@ pub fn rewrite_macro(mac: &ast::Mac,
return None; return None;
} }
} }
}
match style { match style {
MacroStyle::Parens => { MacroStyle::Parens => {

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,4 @@
itemmacro!(this, is.not() .formatted(yet)); itemmacro!(this, is.now() .formatted(yay));
fn main() { fn main() {
foo! ( ); foo! ( );

View File

@ -1,4 +1,4 @@
itemmacro!(this, is.not() .formatted(yet)); itemmacro!(this, is.now().formatted(yay));
fn main() { fn main() {
foo!(); foo!();