forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip.rs
70 lines (61 loc) · 2.31 KB
/
skip.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Module that contains skip related stuffs.
use rustc_span::symbol::{sym, Symbol};
use syntax::ast::{Attribute, PathSegment};
macro_rules! sym {
($tt:tt) => {
Symbol::intern(stringify!($tt))
};
}
/// Take care of skip name stack. You can update it by attributes slice or
/// by other context. Query this context to know if you need skip a block.
#[derive(Default, Clone)]
pub(crate) struct SkipContext {
macros: Vec<String>,
attributes: Vec<String>,
}
impl SkipContext {
pub(crate) fn update_with_attrs(&mut self, attrs: &[Attribute]) {
fn get_skip_names(vs: &mut Vec<String>, attr: &Attribute) {
if let Some(list) = attr.meta_item_list() {
for nested_meta_item in list {
if let Some(name) = nested_meta_item.ident() {
vs.push(name.to_string());
}
}
}
}
for attr in attrs {
if let syntax::ast::AttrKind::Normal(ref attr_item) = &attr.kind {
if is_skip_attr_with(&attr_item.path.segments, |s| s == sym!(macros)) {
get_skip_names(&mut self.macros, attr)
} else if is_skip_attr_with(&attr_item.path.segments, |s| s == sym::attributes) {
get_skip_names(&mut self.attributes, attr)
}
}
}
}
pub(crate) fn update(&mut self, mut other: Self) {
self.macros.append(&mut other.macros);
self.attributes.append(&mut other.attributes);
}
pub(crate) fn skip_macro(&self, name: &str) -> bool {
self.macros.iter().any(|n| n == name)
}
pub(crate) fn skip_attribute(&self, name: &str) -> bool {
self.attributes.iter().any(|n| n == name)
}
}
/// Say if you're playing with `rustfmt`'s skip attribute
pub(crate) fn is_skip_attr(segments: &[PathSegment]) -> bool {
is_skip_attr_with(segments, |s| s == sym!(macros) || s == sym::attributes)
}
fn is_skip_attr_with(segments: &[PathSegment], pred: impl FnOnce(Symbol) -> bool) -> bool {
if segments.len() < 2 || segments[0].ident.name != sym::rustfmt {
return false;
}
match segments.len() {
2 => segments[1].ident.name == sym!(skip),
3 => segments[1].ident.name == sym!(skip) && pred(segments[2].ident.name),
_ => false,
}
}