From 9e5c0390a04910027097c98217fb47d05a1b2ca9 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sun, 27 Mar 2016 14:45:55 +0200 Subject: [PATCH] Properly format macro's with an extra ident --- src/expr.rs | 2 +- src/macros.rs | 6 +++++- src/visitor.rs | 13 ++++++------- tests/source/macros.rs | 2 ++ tests/target/macros.rs | 2 ++ 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index d933f68ce18..96079a29b26 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -154,7 +154,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt ast::ExprKind::Mac(ref mac) => { // Failure to rewrite a marco should not imply failure to // rewrite the expression. - rewrite_macro(mac, context, width, offset).or_else(|| { + rewrite_macro(mac, None, context, width, offset).or_else(|| { wrap_str(context.snippet(self.span), context.config.max_width, width, diff --git a/src/macros.rs b/src/macros.rs index 324c68d750a..7e321366abf 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -51,12 +51,16 @@ fn opener(&self) -> &'static str { } pub fn rewrite_macro(mac: &ast::Mac, + extra_ident: Option, context: &RewriteContext, width: usize, offset: Indent) -> Option { let original_style = macro_style(mac, context); - let macro_name = format!("{}!", mac.node.path); + let macro_name = match extra_ident { + None | Some(ast::Ident { name: ast::Name(0), .. }) => format!("{}!", mac.node.path), + Some(ident) => format!("{}! {}", mac.node.path, ident), + }; let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) { MacroStyle::Brackets } else { diff --git a/src/visitor.rs b/src/visitor.rs index 62fbfd7613f..6ef07e922c0 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -8,10 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use syntax::ast; +use syntax::{ast, visit}; use syntax::codemap::{self, CodeMap, Span, BytePos}; use syntax::parse::ParseSess; -use syntax::visit; use strings::string_buffer::StringBuffer; @@ -56,7 +55,7 @@ fn visit_stmt(&mut self, stmt: &ast::Stmt) { } ast::StmtKind::Mac(ref mac, _macro_style, _) => { self.format_missing_with_indent(stmt.span.lo); - self.visit_mac(mac); + self.visit_mac(mac, None); } } } @@ -254,7 +253,7 @@ fn visit_item(&mut self, item: &ast::Item) { } ast::ItemKind::Mac(ref mac) => { self.format_missing_with_indent(item.span.lo); - self.visit_mac(mac); + self.visit_mac(mac, Some(item.ident)); } ast::ItemKind::ForeignMod(ref foreign_mod) => { self.format_missing_with_indent(item.span.lo); @@ -380,15 +379,15 @@ pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) { } ast::ImplItemKind::Macro(ref mac) => { self.format_missing_with_indent(ii.span.lo); - self.visit_mac(mac); + self.visit_mac(mac, Some(ii.ident)); } } } - fn visit_mac(&mut self, mac: &ast::Mac) { + fn visit_mac(&mut self, mac: &ast::Mac, ident: Option) { // 1 = ; let width = self.config.max_width - self.block_indent.width() - 1; - let rewrite = rewrite_macro(mac, &self.get_context(), width, self.block_indent); + let rewrite = rewrite_macro(mac, ident, &self.get_context(), width, self.block_indent); if let Some(res) = rewrite { self.buffer.push_str(&res); diff --git a/tests/source/macros.rs b/tests/source/macros.rs index 261f02e756f..4d9e0d9ff73 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -4,6 +4,8 @@ itemmacro!{this, is.bracket().formatted()} +peg_file! modname ("mygrammarfile.rustpeg"); + fn main() { foo! ( ); diff --git a/tests/target/macros.rs b/tests/target/macros.rs index 4d99ede9f50..ecc885ec337 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -7,6 +7,8 @@ itemmacro!{this, is.bracket().formatted()} +peg_file! modname("mygrammarfile.rustpeg"); + fn main() { foo!(); -- 2.44.0