2014-01-25 01:37:51 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-09-12 20:10:51 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-19 21:29:58 -06:00
|
|
|
use collections::HashSet;
|
2014-04-02 18:54:22 -05:00
|
|
|
use rustc::util::nodemap::NodeSet;
|
|
|
|
use std::cmp;
|
2013-10-12 16:40:41 -05:00
|
|
|
use std::local_data;
|
2014-04-02 18:54:22 -05:00
|
|
|
use std::strbuf::StrBuf;
|
2013-12-03 18:44:16 -06:00
|
|
|
use std::uint;
|
2013-09-24 15:53:09 -05:00
|
|
|
use syntax::ast;
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
use clean;
|
|
|
|
use clean::Item;
|
|
|
|
use plugins;
|
|
|
|
use fold;
|
|
|
|
use fold::DocFolder;
|
|
|
|
|
|
|
|
/// Strip items marked `#[doc(hidden)]`
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
|
2014-02-11 16:42:23 -06:00
|
|
|
let mut stripped = HashSet::new();
|
|
|
|
|
|
|
|
// strip all #[doc(hidden)] items
|
|
|
|
let krate = {
|
|
|
|
struct Stripper<'a> {
|
|
|
|
stripped: &'a mut HashSet<ast::NodeId>
|
|
|
|
};
|
|
|
|
impl<'a> fold::DocFolder for Stripper<'a> {
|
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
|
|
|
for attr in i.attrs.iter() {
|
|
|
|
match attr {
|
2014-03-07 15:15:50 -06:00
|
|
|
&clean::List(ref x, ref l) if "doc" == *x => {
|
2014-02-11 16:42:23 -06:00
|
|
|
for innerattr in l.iter() {
|
|
|
|
match innerattr {
|
|
|
|
&clean::Word(ref s) if "hidden" == *s => {
|
|
|
|
debug!("found one in strip_hidden; removing");
|
|
|
|
self.stripped.insert(i.id);
|
|
|
|
return None;
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2014-02-11 16:42:23 -06:00
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut stripper = Stripper{ stripped: &mut stripped };
|
|
|
|
stripper.fold_crate(krate)
|
|
|
|
};
|
|
|
|
|
|
|
|
// strip any traits implemented on stripped items
|
|
|
|
let krate = {
|
|
|
|
struct ImplStripper<'a> {
|
|
|
|
stripped: &'a mut HashSet<ast::NodeId>
|
|
|
|
};
|
|
|
|
impl<'a> fold::DocFolder for ImplStripper<'a> {
|
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
|
|
|
match i.inner {
|
|
|
|
clean::ImplItem(clean::Impl{ for_: clean::ResolvedPath{ id: for_id, .. },
|
|
|
|
.. }) => {
|
|
|
|
if self.stripped.contains(&for_id) {
|
|
|
|
return None;
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2014-02-11 16:42:23 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2014-02-11 16:42:23 -06:00
|
|
|
self.fold_item_recur(i)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-11 16:42:23 -06:00
|
|
|
let mut stripper = ImplStripper{ stripped: &mut stripped };
|
|
|
|
stripper.fold_crate(krate)
|
|
|
|
};
|
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
(krate, None)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-09-24 15:55:22 -05:00
|
|
|
/// Strip private items from the point of view of a crate or externally from a
|
|
|
|
/// crate, specified by the `xcrate` flag.
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn strip_private(krate: clean::Crate) -> plugins::PluginResult {
|
2013-09-26 14:27:38 -05:00
|
|
|
// This stripper collects all *retained* nodes.
|
2013-10-12 16:40:41 -05:00
|
|
|
let mut retained = HashSet::new();
|
2013-11-21 17:42:55 -06:00
|
|
|
let exported_items = local_data::get(super::analysiskey, |analysis| {
|
2013-11-11 22:17:47 -06:00
|
|
|
analysis.unwrap().exported_items.clone()
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2014-02-05 15:15:24 -06:00
|
|
|
let mut krate = krate;
|
2013-10-12 16:40:41 -05:00
|
|
|
|
|
|
|
// strip all private items
|
|
|
|
{
|
|
|
|
let mut stripper = Stripper {
|
|
|
|
retained: &mut retained,
|
|
|
|
exported_items: &exported_items,
|
|
|
|
};
|
2014-02-05 15:15:24 -06:00
|
|
|
krate = stripper.fold_crate(krate);
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// strip all private implementations of traits
|
|
|
|
{
|
|
|
|
let mut stripper = ImplStripper(&retained);
|
2014-02-05 15:15:24 -06:00
|
|
|
krate = stripper.fold_crate(krate);
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
2014-02-05 15:15:24 -06:00
|
|
|
(krate, None)
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
struct Stripper<'a> {
|
|
|
|
retained: &'a mut HashSet<ast::NodeId>,
|
2014-02-28 16:34:26 -06:00
|
|
|
exported_items: &'a NodeSet,
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> fold::DocFolder for Stripper<'a> {
|
2013-10-12 16:40:41 -05:00
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
|
|
|
match i.inner {
|
|
|
|
// These items can all get re-exported
|
2013-11-28 14:22:53 -06:00
|
|
|
clean::TypedefItem(..) | clean::StaticItem(..) |
|
|
|
|
clean::StructItem(..) | clean::EnumItem(..) |
|
|
|
|
clean::TraitItem(..) | clean::FunctionItem(..) |
|
|
|
|
clean::VariantItem(..) | clean::MethodItem(..) |
|
|
|
|
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => {
|
2013-10-12 16:40:41 -05:00
|
|
|
if !self.exported_items.contains(&i.id) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2014-04-20 00:24:52 -05:00
|
|
|
clean::ViewItemItem(..) => {
|
2014-01-09 07:05:33 -06:00
|
|
|
if i.visibility != Some(ast::Public) {
|
2013-10-19 00:00:08 -05:00
|
|
|
return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 00:24:52 -05:00
|
|
|
clean::StructFieldItem(..) => {
|
|
|
|
if i.visibility != Some(ast::Public) {
|
|
|
|
return Some(clean::Item {
|
|
|
|
inner: clean::StructFieldItem(clean::HiddenStructField),
|
|
|
|
..i
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 18:00:14 -05:00
|
|
|
// handled below
|
|
|
|
clean::ModuleItem(..) => {}
|
|
|
|
|
2014-02-11 16:29:23 -06:00
|
|
|
// trait impls for private items should be stripped
|
|
|
|
clean::ImplItem(clean::Impl{ for_: clean::ResolvedPath{ id: ref for_id, .. }, .. }) => {
|
|
|
|
if !self.exported_items.contains(for_id) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clean::ImplItem(..) => {}
|
|
|
|
|
2014-02-16 23:40:26 -06:00
|
|
|
// tymethods/macros have no control over privacy
|
|
|
|
clean::MacroItem(..) | clean::TyMethodItem(..) => {}
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
let fastreturn = match i.inner {
|
|
|
|
// nothing left to do for traits (don't want to filter their
|
|
|
|
// methods out, visibility controlled by the trait)
|
2013-11-28 14:22:53 -06:00
|
|
|
clean::TraitItem(..) => true,
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
// implementations of traits are always public.
|
|
|
|
clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
|
2013-09-24 15:55:22 -05:00
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let i = if fastreturn {
|
|
|
|
self.retained.insert(i.id);
|
|
|
|
return Some(i);
|
|
|
|
} else {
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
match i {
|
|
|
|
Some(i) => {
|
|
|
|
match i.inner {
|
|
|
|
// emptied modules/impls have no need to exist
|
2014-01-10 01:23:10 -06:00
|
|
|
clean::ModuleItem(ref m)
|
|
|
|
if m.items.len() == 0 &&
|
|
|
|
i.doc_value().is_none() => None,
|
2013-10-12 16:40:41 -05:00
|
|
|
clean::ImplItem(ref i) if i.methods.len() == 0 => None,
|
|
|
|
_ => {
|
|
|
|
self.retained.insert(i.id);
|
|
|
|
Some(i)
|
2013-09-24 15:55:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-12 16:40:41 -05:00
|
|
|
None => None,
|
2013-09-24 15:55:22 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 16:40:41 -05:00
|
|
|
}
|
2013-09-26 14:27:38 -05:00
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
// This stripper discards all private impls of traits
|
2013-12-10 01:16:18 -06:00
|
|
|
struct ImplStripper<'a>(&'a HashSet<ast::NodeId>);
|
|
|
|
impl<'a> fold::DocFolder for ImplStripper<'a> {
|
2013-10-12 16:40:41 -05:00
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
|
|
|
match i.inner {
|
|
|
|
clean::ImplItem(ref imp) => {
|
|
|
|
match imp.trait_ {
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(clean::ResolvedPath{ id, .. }) => {
|
2013-11-01 20:06:31 -05:00
|
|
|
let ImplStripper(s) = *self;
|
|
|
|
if !s.contains(&id) {
|
2013-10-12 16:40:41 -05:00
|
|
|
return None;
|
2013-09-26 14:27:38 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(..) | None => {}
|
2013-09-26 14:27:38 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 16:40:41 -05:00
|
|
|
_ => {}
|
2013-09-26 14:27:38 -05:00
|
|
|
}
|
2013-10-12 16:40:41 -05:00
|
|
|
self.fold_item_recur(i)
|
2013-09-26 14:27:38 -05:00
|
|
|
}
|
2013-09-24 15:55:22 -05:00
|
|
|
}
|
|
|
|
|
2013-10-12 16:40:41 -05:00
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
|
2013-08-15 15:28:54 -05:00
|
|
|
struct CommentCleaner;
|
|
|
|
impl fold::DocFolder for CommentCleaner {
|
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
|
|
|
let mut i = i;
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut avec: Vec<clean::Attribute> = Vec::new();
|
2013-08-15 15:28:54 -05:00
|
|
|
for attr in i.attrs.iter() {
|
|
|
|
match attr {
|
2014-03-07 15:15:50 -06:00
|
|
|
&clean::NameValue(ref x, ref s) if "doc" == *x => avec.push(
|
2014-04-15 20:17:48 -05:00
|
|
|
clean::NameValue("doc".to_owned(), unindent(*s))),
|
2013-08-15 15:28:54 -05:00
|
|
|
x => avec.push(x.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.attrs = avec;
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut cleaner = CommentCleaner;
|
2014-02-05 15:15:24 -06:00
|
|
|
let krate = cleaner.fold_crate(krate);
|
|
|
|
(krate, None)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2014-02-05 15:15:24 -06:00
|
|
|
pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
|
2013-08-15 15:28:54 -05:00
|
|
|
struct Collapser;
|
|
|
|
impl fold::DocFolder for Collapser {
|
|
|
|
fn fold_item(&mut self, i: Item) -> Option<Item> {
|
2014-04-02 18:54:22 -05:00
|
|
|
let mut docstr = StrBuf::new();
|
2013-08-15 15:28:54 -05:00
|
|
|
let mut i = i;
|
|
|
|
for attr in i.attrs.iter() {
|
|
|
|
match *attr {
|
2014-03-07 15:15:50 -06:00
|
|
|
clean::NameValue(ref x, ref s) if "doc" == *x => {
|
2013-08-15 15:28:54 -05:00
|
|
|
docstr.push_str(s.clone());
|
|
|
|
docstr.push_char('\n');
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut a: Vec<clean::Attribute> = i.attrs.iter().filter(|&a| match a {
|
2014-03-07 15:15:50 -06:00
|
|
|
&clean::NameValue(ref x, _) if "doc" == *x => false,
|
2013-08-15 15:28:54 -05:00
|
|
|
_ => true
|
|
|
|
}).map(|x| x.clone()).collect();
|
2014-04-02 18:54:22 -05:00
|
|
|
if docstr.len() > 0 {
|
2014-04-15 20:17:48 -05:00
|
|
|
a.push(clean::NameValue("doc".to_owned(), docstr.into_owned()));
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
i.attrs = a;
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut collapser = Collapser;
|
2014-02-05 15:15:24 -06:00
|
|
|
let krate = collapser.fold_crate(krate);
|
|
|
|
(krate, None)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
pub fn unindent(s: &str) -> ~str {
|
2014-03-05 17:28:08 -06:00
|
|
|
let lines = s.lines_any().collect::<Vec<&str> >();
|
2013-09-19 00:18:38 -05:00
|
|
|
let mut saw_first_line = false;
|
|
|
|
let mut saw_second_line = false;
|
2014-01-25 01:37:51 -06:00
|
|
|
let min_indent = lines.iter().fold(uint::MAX, |min_indent, line| {
|
2013-09-19 00:18:38 -05:00
|
|
|
|
|
|
|
// After we see the first non-whitespace line, look at
|
|
|
|
// the line we have. If it is not whitespace, and therefore
|
|
|
|
// part of the first paragraph, then ignore the indentation
|
|
|
|
// level of the first line
|
|
|
|
let ignore_previous_indents =
|
|
|
|
saw_first_line &&
|
|
|
|
!saw_second_line &&
|
|
|
|
!line.is_whitespace();
|
|
|
|
|
|
|
|
let min_indent = if ignore_previous_indents {
|
2014-01-25 01:37:51 -06:00
|
|
|
uint::MAX
|
2013-09-19 00:18:38 -05:00
|
|
|
} else {
|
|
|
|
min_indent
|
|
|
|
};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
if saw_first_line {
|
|
|
|
saw_second_line = true;
|
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
if line.is_whitespace() {
|
|
|
|
min_indent
|
|
|
|
} else {
|
|
|
|
saw_first_line = true;
|
|
|
|
let mut spaces = 0;
|
2013-11-21 17:42:55 -06:00
|
|
|
line.chars().all(|char| {
|
2013-09-19 00:18:38 -05:00
|
|
|
// Only comparing against space because I wouldn't
|
|
|
|
// know what to do with mixed whitespace chars
|
|
|
|
if char == ' ' {
|
|
|
|
spaces += 1;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2014-02-06 01:34:33 -06:00
|
|
|
cmp::min(min_indent, spaces)
|
2013-09-19 00:18:38 -05:00
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-09-19 00:18:38 -05:00
|
|
|
|
2014-02-13 11:46:46 -06:00
|
|
|
if lines.len() >= 1 {
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut unindented = vec!( lines.get(0).trim() );
|
2014-03-28 14:42:34 -05:00
|
|
|
unindented.push_all(lines.tail().iter().map(|&line| {
|
2014-02-13 11:46:46 -06:00
|
|
|
if line.is_whitespace() {
|
|
|
|
line
|
|
|
|
} else {
|
|
|
|
assert!(line.len() >= min_indent);
|
|
|
|
line.slice_from(min_indent)
|
|
|
|
}
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<_>>().as_slice());
|
2014-02-13 11:46:46 -06:00
|
|
|
unindented.connect("\n")
|
|
|
|
} else {
|
|
|
|
s.to_owned()
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2013-09-19 00:18:38 -05:00
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod unindent_tests {
|
|
|
|
use super::unindent;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[test]
|
|
|
|
fn should_unindent() {
|
2014-04-15 20:17:48 -05:00
|
|
|
let s = " line1\n line2".to_owned();
|
2013-09-19 00:18:38 -05:00
|
|
|
let r = unindent(s);
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(r, "line1\nline2".to_owned());
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[test]
|
|
|
|
fn should_unindent_multiple_paragraphs() {
|
2014-04-15 20:17:48 -05:00
|
|
|
let s = " line1\n\n line2".to_owned();
|
2013-09-19 00:18:38 -05:00
|
|
|
let r = unindent(s);
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(r, "line1\n\nline2".to_owned());
|
2013-09-19 00:18:38 -05:00
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[test]
|
|
|
|
fn should_leave_multiple_indent_levels() {
|
|
|
|
// Line 2 is indented another level beyond the
|
|
|
|
// base indentation and should be preserved
|
2014-04-15 20:17:48 -05:00
|
|
|
let s = " line1\n\n line2".to_owned();
|
2013-09-19 00:18:38 -05:00
|
|
|
let r = unindent(s);
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(r, "line1\n\n line2".to_owned());
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2013-09-12 14:11:06 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[test]
|
|
|
|
fn should_ignore_first_line_indent() {
|
|
|
|
// Thi first line of the first paragraph may not be indented as
|
|
|
|
// far due to the way the doc string was written:
|
|
|
|
//
|
|
|
|
// #[doc = "Start way over here
|
|
|
|
// and continue here"]
|
2014-04-15 20:17:48 -05:00
|
|
|
let s = "line1\n line2".to_owned();
|
2013-09-19 00:18:38 -05:00
|
|
|
let r = unindent(s);
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(r, "line1\nline2".to_owned());
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
2013-09-12 14:11:06 -05:00
|
|
|
|
2013-09-19 00:18:38 -05:00
|
|
|
#[test]
|
|
|
|
fn should_not_ignore_first_line_indent_in_a_single_line_para() {
|
2014-04-15 20:17:48 -05:00
|
|
|
let s = "line1\n\n line2".to_owned();
|
2013-09-19 00:18:38 -05:00
|
|
|
let r = unindent(s);
|
2014-04-15 20:17:48 -05:00
|
|
|
assert_eq!(r, "line1\n\n line2".to_owned());
|
2013-09-19 00:18:38 -05:00
|
|
|
}
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|