Skip to content

Commit 1f8553d

Browse files
committed
add new attribute rustfmt::skip::macros
add test for function not having attribute
1 parent 393d721 commit 1f8553d

File tree

5 files changed

+103
-13
lines changed

5 files changed

+103
-13
lines changed

src/expr.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,20 @@ pub fn format_expr(
190190
rewrite_chain(expr, context, shape)
191191
}
192192
ast::ExprKind::Mac(ref mac) => {
193-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
194-
wrap_str(
195-
context.snippet(expr.span).to_owned(),
196-
context.config.max_width(),
197-
shape,
198-
)
199-
})
193+
let should_skip = context
194+
.skip_macro_names
195+
.contains(&context.snippet(mac.node.path.span).to_owned());
196+
if should_skip {
197+
None
198+
} else {
199+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
200+
wrap_str(
201+
context.snippet(expr.span).to_owned(),
202+
context.config.max_width(),
203+
shape,
204+
)
205+
})
206+
}
200207
}
201208
ast::ExprKind::Ret(None) => Some("return".to_owned()),
202209
ast::ExprKind::Ret(Some(ref expr)) => {
@@ -1920,6 +1927,7 @@ pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19201927
offset: shape.offset + last_line_width + 1,
19211928
..shape
19221929
});
1930+
// dbg!(
19231931
let rhs = choose_rhs(
19241932
context,
19251933
ex,

src/rewrite.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct RewriteContext<'a> {
3939
// Used for `format_snippet`
4040
pub(crate) macro_rewrite_failure: RefCell<bool>,
4141
pub(crate) report: FormatReport,
42+
pub skip_macro_names: Vec<String>,
4243
}
4344

4445
impl<'a> RewriteContext<'a> {

src/visitor.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::cell::RefCell;
22

3-
use syntax::parse::ParseSess;
3+
use syntax::parse::{token, ParseSess};
44
use syntax::source_map::{self, BytePos, Pos, SourceMap, Span};
5+
use syntax::tokenstream::TokenTree;
56
use syntax::{ast, visit};
67

78
use crate::attr::*;
@@ -66,6 +67,7 @@ pub struct FmtVisitor<'a> {
6667
pub skipped_range: Vec<(usize, usize)>,
6768
pub macro_rewrite_failure: bool,
6869
pub(crate) report: FormatReport,
70+
pub skip_macro_names: Vec<String>,
6971
}
7072

7173
impl<'a> Drop for FmtVisitor<'a> {
@@ -331,6 +333,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
331333
}
332334
}
333335
}
336+
self.get_skip_macros(&attrs);
334337

335338
match item.node {
336339
ast::ItemKind::Use(ref tree) => self.format_import(item, tree),
@@ -437,7 +440,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
437440
);
438441
self.push_rewrite(item.span, rewrite);
439442
}
440-
}
443+
};
444+
self.skip_macro_names.clear();
441445
}
442446

443447
pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
@@ -616,6 +620,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
616620
skipped_range: vec![],
617621
macro_rewrite_failure: false,
618622
report,
623+
skip_macro_names: vec![],
619624
}
620625
}
621626

@@ -640,10 +645,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
640645
ErrorKind::DeprecatedAttr,
641646
)],
642647
);
643-
} else if attr.path.segments[0].ident.to_string() == "rustfmt"
644-
&& (attr.path.segments.len() == 1
645-
|| attr.path.segments[1].ident.to_string() != "skip")
646-
{
648+
} else if self.is_rustfmt_macro_error(&attr.path.segments) {
647649
let file_name = self.source_map.span_to_filename(attr.span).into();
648650
self.report.append(
649651
file_name,
@@ -671,6 +673,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
671673
false
672674
}
673675

676+
fn is_rustfmt_macro_error(&self, segments: &Vec<syntax::ast::PathSegment>) -> bool {
677+
if segments[0].ident.to_string() != "rustfmt" {
678+
return false;
679+
}
680+
681+
match segments.len() {
682+
2 => segments[1].ident.to_string() != "skip",
683+
3 => {
684+
segments[1].ident.to_string() != "skip" || segments[2].ident.to_string() != "macros"
685+
}
686+
_ => false,
687+
}
688+
}
689+
674690
fn walk_mod_items(&mut self, m: &ast::Mod) {
675691
self.visit_items_with_reordering(&ptr_vec_to_ref_vec(&m.items));
676692
}
@@ -817,6 +833,25 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
817833
snippet_provider: self.snippet_provider,
818834
macro_rewrite_failure: RefCell::new(false),
819835
report: self.report.clone(),
836+
skip_macro_names: self.skip_macro_names.clone(),
837+
}
838+
}
839+
840+
pub fn get_skip_macros(&mut self, attrs: &[ast::Attribute]) {
841+
for attr in attrs {
842+
for token in attr.tokens.trees() {
843+
if let TokenTree::Delimited(_, _, stream) = token {
844+
for inner_token in stream.trees() {
845+
if let TokenTree::Token(span, token) = inner_token {
846+
if let token::Token::Ident(_, _) = token {
847+
// FIXME ident.span.lo() and ident.span.hi() are 0
848+
let macro_name = self.get_context().snippet(span).to_owned();
849+
self.skip_macro_names.push(macro_name);
850+
}
851+
}
852+
}
853+
}
854+
}
820855
}
821856
}
822857
}

tests/source/issue-3434.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[rustfmt::skip::macros(html, skip_macro)]
2+
fn main() {
3+
let macro_result1 = html! { <div>
4+
Hello</div>
5+
}.to_string();
6+
7+
let macro_result2 = not_skip_macro! { <div>
8+
Hello</div>
9+
}.to_string();
10+
11+
skip_macro! {
12+
this is a skip_macro here
13+
};
14+
15+
foo();
16+
}
17+
18+
fn foo() {
19+
let macro_result1 = html! { <div>
20+
Hello</div>
21+
}.to_string();
22+
}

tests/target/issue-3434.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[rustfmt::skip::macros(html, skip_macro)]
2+
fn main() {
3+
let macro_result1 = html! { <div>
4+
Hello</div>
5+
}.to_string();
6+
7+
let macro_result2 = not_skip_macro! { <div>
8+
Hello</div>
9+
}
10+
.to_string();
11+
12+
skip_macro! {
13+
this is a skip_macro here
14+
};
15+
16+
foo();
17+
}
18+
19+
fn foo() {
20+
let macro_result1 = html! { <div>
21+
Hello</div>
22+
}
23+
.to_string();
24+
}

0 commit comments

Comments
 (0)