|
2 | 2 |
|
3 | 3 | use rustc_ast::ast;
|
4 | 4 | use rustc_ast_pretty::pprust;
|
| 5 | +use std::collections::HashSet; |
5 | 6 |
|
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. |
8 | 15 | #[derive(Default, Clone)]
|
9 | 16 | 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, |
13 | 19 | }
|
14 | 20 |
|
15 | 21 | impl SkipContext {
|
16 | 22 | 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)); |
20 | 25 | }
|
21 | 26 |
|
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); |
25 | 46 | }
|
26 | 47 |
|
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); |
32 | 51 | }
|
33 | 52 |
|
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) |
36 | 55 | }
|
37 | 56 |
|
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; |
40 | 59 | }
|
41 | 60 | }
|
42 | 61 |
|
|
0 commit comments