Skip to content

Commit 7cc1261

Browse files
tommilligancalebcartwright
authored andcommitted
feat: nicer skip context for macro/attribute
1 parent a7801aa commit 7cc1261

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

src/attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl Rewrite for ast::Attribute {
337337
} else {
338338
let should_skip = self
339339
.ident()
340-
.map(|s| context.skip_context.skip_attribute(s.name.as_str()))
340+
.map(|s| context.skip_context.attributes.skip(s.name.as_str()))
341341
.unwrap_or(false);
342342
let prefix = attr_prefix(self);
343343

@@ -391,7 +391,7 @@ impl Rewrite for [ast::Attribute] {
391391

392392
// Determine if the source text is annotated with `#[rustfmt::skip::attributes(derive)]`
393393
// or `#![rustfmt::skip::attributes(derive)]`
394-
let skip_derives = context.skip_context.skip_attribute("derive");
394+
let skip_derives = context.skip_context.attributes.skip("derive");
395395

396396
// This is not just a simple map because we need to handle doc comments
397397
// (where we take as many doc comment attributes as possible) and possibly

src/macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ pub(crate) fn rewrite_macro(
157157
) -> Option<String> {
158158
let should_skip = context
159159
.skip_context
160-
.skip_macro(context.snippet(mac.path.span));
160+
.macros
161+
.skip(context.snippet(mac.path.span));
161162
if should_skip {
162163
None
163164
} else {

src/skip.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,60 @@
22
33
use rustc_ast::ast;
44
use rustc_ast_pretty::pprust;
5+
use std::collections::HashSet;
56

6-
/// Take care of skip name stack. You can update it by attributes slice or
7-
/// by other context. Query this context to know if you need skip a block.
7+
/// Track which blocks of code are to be skipped when formatting.
8+
///
9+
/// You can update it by:
10+
///
11+
/// - attributes slice
12+
/// - manually feeding values into the underlying contexts
13+
///
14+
/// Query this context to know if you need skip a block.
815
#[derive(Default, Clone)]
916
pub(crate) struct SkipContext {
10-
pub(crate) all_macros: bool,
11-
macros: Vec<String>,
12-
attributes: Vec<String>,
17+
pub(crate) macros: SkipNameContext,
18+
pub(crate) attributes: SkipNameContext,
1319
}
1420

1521
impl SkipContext {
1622
pub(crate) fn update_with_attrs(&mut self, attrs: &[ast::Attribute]) {
17-
self.macros.append(&mut get_skip_names("macros", attrs));
18-
self.attributes
19-
.append(&mut get_skip_names("attributes", attrs));
23+
self.macros.append(get_skip_names("macros", attrs));
24+
self.attributes.append(get_skip_names("attributes", attrs));
2025
}
2126

22-
pub(crate) fn update(&mut self, mut other: SkipContext) {
23-
self.macros.append(&mut other.macros);
24-
self.attributes.append(&mut other.attributes);
27+
pub(crate) fn update(&mut self, other: SkipContext) {
28+
let SkipContext { macros, attributes } = other;
29+
self.macros.update(macros);
30+
self.attributes.update(attributes);
31+
}
32+
}
33+
34+
/// Track which names to skip.
35+
///
36+
/// Query this context with a string to know whether to skip it.
37+
#[derive(Default, Clone)]
38+
pub(crate) struct SkipNameContext {
39+
all: bool,
40+
values: HashSet<String>,
41+
}
42+
43+
impl SkipNameContext {
44+
pub(crate) fn append(&mut self, values: Vec<String>) {
45+
self.values.extend(values);
2546
}
2647

27-
pub(crate) fn update_macros<T>(&mut self, other: T)
28-
where
29-
T: IntoIterator<Item = String>,
30-
{
31-
self.macros.extend(other.into_iter());
48+
pub(crate) fn update(&mut self, other: Self) {
49+
self.all = self.all || other.all;
50+
self.values.extend(other.values);
3251
}
3352

34-
pub(crate) fn skip_macro(&self, name: &str) -> bool {
35-
self.all_macros || self.macros.iter().any(|n| n == name)
53+
pub(crate) fn skip(&self, name: &str) -> bool {
54+
self.all || self.values.contains(name)
3655
}
3756

38-
pub(crate) fn skip_attribute(&self, name: &str) -> bool {
39-
self.attributes.iter().any(|n| n == name)
57+
pub(crate) fn set_all(&mut self, all: bool) {
58+
self.all = all;
4059
}
4160
}
4261

src/visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
775775
for macro_selector in config.skip_macro_invocations().0 {
776776
match macro_selector {
777777
MacroSelector::Name(name) => macro_names.push(name.to_string()),
778-
MacroSelector::All => skip_context.all_macros = true,
778+
MacroSelector::All => skip_context.macros.set_all(true),
779779
}
780780
}
781-
skip_context.update_macros(macro_names);
781+
skip_context.macros.append(macro_names);
782782
FmtVisitor {
783783
parent_context: None,
784784
parse_sess: parse_session,

0 commit comments

Comments
 (0)