forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust-lang#3665: Implemented (rust-lang#3689)
- Loading branch information
1 parent
66c27c9
commit 76e2ba2
Showing
17 changed files
with
207 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! Module that contains skip related stuffs. | ||
use syntax::ast; | ||
|
||
/// 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: &[ast::Attribute]) { | ||
self.macros.append(&mut get_skip_names("macros", attrs)); | ||
self.attributes | ||
.append(&mut get_skip_names("attributes", attrs)); | ||
} | ||
|
||
pub(crate) fn update(&mut self, mut other: SkipContext) { | ||
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) | ||
} | ||
} | ||
|
||
static RUSTFMT: &'static str = "rustfmt"; | ||
static SKIP: &'static str = "skip"; | ||
|
||
/// Say if you're playing with `rustfmt`'s skip attribute | ||
pub(crate) fn is_skip_attr(segments: &[ast::PathSegment]) -> bool { | ||
if segments.len() < 2 || segments[0].ident.to_string() != RUSTFMT { | ||
return false; | ||
} | ||
match segments.len() { | ||
2 => segments[1].ident.to_string() == SKIP, | ||
3 => { | ||
segments[1].ident.to_string() == SKIP | ||
&& ["macros", "attributes"] | ||
.iter() | ||
.any(|&n| n == &segments[2].ident.name.as_str()) | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
fn get_skip_names(kind: &str, attrs: &[ast::Attribute]) -> Vec<String> { | ||
let mut skip_names = vec![]; | ||
let path = format!("{}::{}::{}", RUSTFMT, SKIP, kind); | ||
for attr in attrs { | ||
// syntax::ast::Path is implemented partialEq | ||
// but it is designed for segments.len() == 1 | ||
if format!("{}", attr.path) != path { | ||
continue; | ||
} | ||
|
||
if let Some(list) = attr.meta_item_list() { | ||
for nested_meta_item in list { | ||
if let Some(name) = nested_meta_item.ident() { | ||
skip_names.push(name.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
skip_names | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#![rustfmt::skip::attributes(skip_mod_attr)] | ||
|
||
mod sub_mod; | ||
|
||
#[rustfmt::skip::attributes(other, skip_attr)] | ||
fn main() { | ||
#[other(should, | ||
skip, | ||
this, format)] | ||
struct S {} | ||
|
||
#[skip_attr(should, skip, | ||
this, format,too)] | ||
fn doesnt_mater() {} | ||
|
||
#[skip_mod_attr(should, skip, | ||
this, format, | ||
enerywhere)] | ||
fn more() {} | ||
|
||
#[not_skip(not, | ||
skip, me)] | ||
struct B {} | ||
} | ||
|
||
#[other(should, not, skip, | ||
this, format, here)] | ||
fn foo() {} | ||
|
||
#[skip_mod_attr(should, skip, | ||
this, format,in, master, | ||
and, sub, module)] | ||
fn bar() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![this::is::not::skip::attribute(ouch)] | ||
|
||
#[ouch(not, skip, me)] | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[rustfmt::skip::attributes(more_skip)] | ||
#[more_skip(should, | ||
skip, | ||
this, format)] | ||
fn foo() {} | ||
|
||
#[skip_mod_attr(should, skip, | ||
this, format,in, master, | ||
and, sub, module)] | ||
fn bar() {} | ||
|
||
#[skip_attr(should, not, | ||
skip, this, attribute, here)] | ||
fn baz() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![rustfmt::skip::attributes(skip_mod_attr)] | ||
|
||
mod sub_mod; | ||
|
||
#[rustfmt::skip::attributes(other, skip_attr)] | ||
fn main() { | ||
#[other(should, | ||
skip, | ||
this, format)] | ||
struct S {} | ||
|
||
#[skip_attr(should, skip, | ||
this, format,too)] | ||
fn doesnt_mater() {} | ||
|
||
#[skip_mod_attr(should, skip, | ||
this, format, | ||
enerywhere)] | ||
fn more() {} | ||
|
||
#[not_skip(not, skip, me)] | ||
struct B {} | ||
} | ||
|
||
#[other(should, not, skip, this, format, here)] | ||
fn foo() {} | ||
|
||
#[skip_mod_attr(should, skip, | ||
this, format,in, master, | ||
and, sub, module)] | ||
fn bar() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![this::is::not::skip::attribute(ouch)] | ||
|
||
#[ouch(not, skip, me)] | ||
fn main() {} |
Oops, something went wrong.