Skip to content

Commit 50820c6

Browse files
committed
Merge pull request #883 from marcusklaas/macro-with-name
Properly format macro's with an extra ident
2 parents d3b18d0 + 9e5c039 commit 50820c6

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Rewrite for ast::Expr {
154154
ast::ExprKind::Mac(ref mac) => {
155155
// Failure to rewrite a marco should not imply failure to
156156
// rewrite the expression.
157-
rewrite_macro(mac, context, width, offset).or_else(|| {
157+
rewrite_macro(mac, None, context, width, offset).or_else(|| {
158158
wrap_str(context.snippet(self.span),
159159
context.config.max_width,
160160
width,

src/macros.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ impl MacroStyle {
5151
}
5252

5353
pub fn rewrite_macro(mac: &ast::Mac,
54+
extra_ident: Option<ast::Ident>,
5455
context: &RewriteContext,
5556
width: usize,
5657
offset: Indent)
5758
-> Option<String> {
5859
let original_style = macro_style(mac, context);
59-
let macro_name = format!("{}!", mac.node.path);
60+
let macro_name = match extra_ident {
61+
None | Some(ast::Ident { name: ast::Name(0), .. }) => format!("{}!", mac.node.path),
62+
Some(ident) => format!("{}! {}", mac.node.path, ident),
63+
};
6064
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
6165
MacroStyle::Brackets
6266
} else {

src/visitor.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast;
11+
use syntax::{ast, visit};
1212
use syntax::codemap::{self, CodeMap, Span, BytePos};
1313
use syntax::parse::ParseSess;
14-
use syntax::visit;
1514

1615
use strings::string_buffer::StringBuffer;
1716

@@ -56,7 +55,7 @@ impl<'a> FmtVisitor<'a> {
5655
}
5756
ast::StmtKind::Mac(ref mac, _macro_style, _) => {
5857
self.format_missing_with_indent(stmt.span.lo);
59-
self.visit_mac(mac);
58+
self.visit_mac(mac, None);
6059
}
6160
}
6261
}
@@ -254,7 +253,7 @@ impl<'a> FmtVisitor<'a> {
254253
}
255254
ast::ItemKind::Mac(ref mac) => {
256255
self.format_missing_with_indent(item.span.lo);
257-
self.visit_mac(mac);
256+
self.visit_mac(mac, Some(item.ident));
258257
}
259258
ast::ItemKind::ForeignMod(ref foreign_mod) => {
260259
self.format_missing_with_indent(item.span.lo);
@@ -380,15 +379,15 @@ impl<'a> FmtVisitor<'a> {
380379
}
381380
ast::ImplItemKind::Macro(ref mac) => {
382381
self.format_missing_with_indent(ii.span.lo);
383-
self.visit_mac(mac);
382+
self.visit_mac(mac, Some(ii.ident));
384383
}
385384
}
386385
}
387386

388-
fn visit_mac(&mut self, mac: &ast::Mac) {
387+
fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>) {
389388
// 1 = ;
390389
let width = self.config.max_width - self.block_indent.width() - 1;
391-
let rewrite = rewrite_macro(mac, &self.get_context(), width, self.block_indent);
390+
let rewrite = rewrite_macro(mac, ident, &self.get_context(), width, self.block_indent);
392391

393392
if let Some(res) = rewrite {
394393
self.buffer.push_str(&res);

tests/source/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
44

55
itemmacro!{this, is.bracket().formatted()}
66

7+
peg_file! modname ("mygrammarfile.rustpeg");
8+
79
fn main() {
810
foo! ( );
911

tests/target/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ itemmacro!(really,
77

88
itemmacro!{this, is.bracket().formatted()}
99

10+
peg_file! modname("mygrammarfile.rustpeg");
11+
1012
fn main() {
1113
foo!();
1214

0 commit comments

Comments
 (0)