Move in-pass-function structs and impls outside functions.

Now that each pass has its own module, there's not much of a reason to
keep these structs within the functions.
This commit is contained in:
Corey Farwell 2016-09-25 18:05:58 -04:00
parent bd62c4c83d
commit 99e1b9cfa6
3 changed files with 76 additions and 70 deletions

View File

@ -16,30 +16,32 @@
use fold::DocFolder; use fold::DocFolder;
pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult { pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
struct Collapser;
impl fold::DocFolder for Collapser {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
let mut docstr = String::new();
for attr in &i.attrs {
if let clean::NameValue(ref x, ref s) = *attr {
if "doc" == *x {
docstr.push_str(s);
docstr.push('\n');
}
}
}
let mut a: Vec<clean::Attribute> = i.attrs.iter().filter(|&a| match a {
&clean::NameValue(ref x, _) if "doc" == *x => false,
_ => true
}).cloned().collect();
if !docstr.is_empty() {
a.push(clean::NameValue("doc".to_string(), docstr));
}
i.attrs = a;
self.fold_item_recur(i)
}
}
let mut collapser = Collapser; let mut collapser = Collapser;
let krate = collapser.fold_crate(krate); let krate = collapser.fold_crate(krate);
krate krate
} }
struct Collapser;
impl fold::DocFolder for Collapser {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
let mut docstr = String::new();
for attr in &i.attrs {
if let clean::NameValue(ref x, ref s) = *attr {
if "doc" == *x {
docstr.push_str(s);
docstr.push('\n');
}
}
}
let mut a: Vec<clean::Attribute> = i.attrs.iter().filter(|&a| match a {
&clean::NameValue(ref x, _) if "doc" == *x => false,
_ => true
}).cloned().collect();
if !docstr.is_empty() {
a.push(clean::NameValue("doc".to_string(), docstr));
}
i.attrs = a;
self.fold_item_recur(i)
}
}

View File

@ -25,35 +25,6 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
// strip all #[doc(hidden)] items // strip all #[doc(hidden)] items
let krate = { let krate = {
struct Stripper<'a> {
retained: &'a mut DefIdSet,
update_retained: bool,
}
impl<'a> fold::DocFolder for Stripper<'a> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
if i.attrs.list("doc").has_word("hidden") {
debug!("found one in strip_hidden; removing");
// use a dedicated hidden item for given item type if any
match i.inner {
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
// We need to recurse into stripped modules to
// strip things like impl methods but when doing so
// we must not add any items to the `retained` set.
let old = mem::replace(&mut self.update_retained, false);
let ret = Strip(self.fold_item_recur(i).unwrap()).fold();
self.update_retained = old;
return ret;
}
_ => return None,
}
} else {
if self.update_retained {
self.retained.insert(i.def_id);
}
}
self.fold_item_recur(i)
}
}
let mut stripper = Stripper{ retained: &mut retained, update_retained: true }; let mut stripper = Stripper{ retained: &mut retained, update_retained: true };
stripper.fold_crate(krate) stripper.fold_crate(krate)
}; };
@ -62,3 +33,34 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
let mut stripper = ImplStripper { retained: &retained }; let mut stripper = ImplStripper { retained: &retained };
stripper.fold_crate(krate) stripper.fold_crate(krate)
} }
struct Stripper<'a> {
retained: &'a mut DefIdSet,
update_retained: bool,
}
impl<'a> fold::DocFolder for Stripper<'a> {
fn fold_item(&mut self, i: Item) -> Option<Item> {
if i.attrs.list("doc").has_word("hidden") {
debug!("found one in strip_hidden; removing");
// use a dedicated hidden item for given item type if any
match i.inner {
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
// We need to recurse into stripped modules to
// strip things like impl methods but when doing so
// we must not add any items to the `retained` set.
let old = mem::replace(&mut self.update_retained, false);
let ret = Strip(self.fold_item_recur(i).unwrap()).fold();
self.update_retained = old;
return ret;
}
_ => return None,
}
} else {
if self.update_retained {
self.retained.insert(i.def_id);
}
}
self.fold_item_recur(i)
}
}

View File

@ -17,29 +17,31 @@
use fold::{self, DocFolder}; use fold::{self, DocFolder};
pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult { pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
struct CommentCleaner;
impl fold::DocFolder for CommentCleaner {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
let mut avec: Vec<clean::Attribute> = Vec::new();
for attr in &i.attrs {
match attr {
&clean::NameValue(ref x, ref s)
if "doc" == *x => {
avec.push(clean::NameValue("doc".to_string(),
unindent(s)))
}
x => avec.push(x.clone())
}
}
i.attrs = avec;
self.fold_item_recur(i)
}
}
let mut cleaner = CommentCleaner; let mut cleaner = CommentCleaner;
let krate = cleaner.fold_crate(krate); let krate = cleaner.fold_crate(krate);
krate krate
} }
struct CommentCleaner;
impl fold::DocFolder for CommentCleaner {
fn fold_item(&mut self, mut i: Item) -> Option<Item> {
let mut avec: Vec<clean::Attribute> = Vec::new();
for attr in &i.attrs {
match attr {
&clean::NameValue(ref x, ref s)
if "doc" == *x => {
avec.push(clean::NameValue("doc".to_string(),
unindent(s)))
}
x => avec.push(x.clone())
}
}
i.attrs = avec;
self.fold_item_recur(i)
}
}
fn unindent(s: &str) -> String { fn unindent(s: &str) -> String {
let lines = s.lines().collect::<Vec<&str> >(); let lines = s.lines().collect::<Vec<&str> >();
let mut saw_first_line = false; let mut saw_first_line = false;