Skip to content

Commit

Permalink
Move in-pass-function structs and impls outside functions.
Browse files Browse the repository at this point in the history
Now that each pass has its own module, there's not much of a reason to
keep these structs within the functions.
  • Loading branch information
frewsxcv committed Sep 25, 2016
1 parent bd62c4c commit 99e1b9c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 68 deletions.
46 changes: 24 additions & 22 deletions src/librustdoc/passes/collapse_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@ use fold;
use fold::DocFolder;

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 collapser = Collapser;
let krate = collapser.fold_crate(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)
}
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 krate = collapser.fold_crate(krate);
krate
}
60 changes: 31 additions & 29 deletions src/librustdoc/passes/strip_hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,6 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {

// strip all #[doc(hidden)] items
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 };
stripper.fold_crate(krate)
};
Expand All @@ -62,3 +33,34 @@ pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
let mut stripper = ImplStripper { retained: &retained };
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)
}
}
36 changes: 19 additions & 17 deletions src/librustdoc/passes/unindent_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ use plugins;
use fold::{self, DocFolder};

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())
let mut cleaner = CommentCleaner;
let krate = cleaner.fold_crate(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)
}
i.attrs = avec;
self.fold_item_recur(i)
}
let mut cleaner = CommentCleaner;
let krate = cleaner.fold_crate(krate);
krate
}

fn unindent(s: &str) -> String {
Expand Down

0 comments on commit 99e1b9c

Please sign in to comment.