1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use collections::HashSet;
12 use rustc::util::nodemap::NodeSet;
15 use std::strbuf::StrBuf;
25 /// Strip items marked `#[doc(hidden)]`
26 pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
27 let mut stripped = HashSet::new();
29 // strip all #[doc(hidden)] items
32 stripped: &'a mut HashSet<ast::NodeId>
34 impl<'a> fold::DocFolder for Stripper<'a> {
35 fn fold_item(&mut self, i: Item) -> Option<Item> {
36 for attr in i.attrs.iter() {
38 &clean::List(ref x, ref l) if "doc" == *x => {
39 for innerattr in l.iter() {
41 &clean::Word(ref s) if "hidden" == *s => {
42 debug!("found one in strip_hidden; removing");
43 self.stripped.insert(i.id);
53 self.fold_item_recur(i)
56 let mut stripper = Stripper{ stripped: &mut stripped };
57 stripper.fold_crate(krate)
60 // strip any traits implemented on stripped items
62 struct ImplStripper<'a> {
63 stripped: &'a mut HashSet<ast::NodeId>
65 impl<'a> fold::DocFolder for ImplStripper<'a> {
66 fn fold_item(&mut self, i: Item) -> Option<Item> {
68 clean::ImplItem(clean::Impl{ for_: clean::ResolvedPath{ id: for_id, .. },
70 if self.stripped.contains(&for_id) {
76 self.fold_item_recur(i)
79 let mut stripper = ImplStripper{ stripped: &mut stripped };
80 stripper.fold_crate(krate)
86 /// Strip private items from the point of view of a crate or externally from a
87 /// crate, specified by the `xcrate` flag.
88 pub fn strip_private(krate: clean::Crate) -> plugins::PluginResult {
89 // This stripper collects all *retained* nodes.
90 let mut retained = HashSet::new();
91 let exported_items = local_data::get(super::analysiskey, |analysis| {
92 analysis.unwrap().exported_items.clone()
94 let mut krate = krate;
96 // strip all private items
98 let mut stripper = Stripper {
99 retained: &mut retained,
100 exported_items: &exported_items,
102 krate = stripper.fold_crate(krate);
105 // strip all private implementations of traits
107 let mut stripper = ImplStripper(&retained);
108 krate = stripper.fold_crate(krate);
113 struct Stripper<'a> {
114 retained: &'a mut HashSet<ast::NodeId>,
115 exported_items: &'a NodeSet,
118 impl<'a> fold::DocFolder for Stripper<'a> {
119 fn fold_item(&mut self, i: Item) -> Option<Item> {
121 // These items can all get re-exported
122 clean::TypedefItem(..) | clean::StaticItem(..) |
123 clean::StructItem(..) | clean::EnumItem(..) |
124 clean::TraitItem(..) | clean::FunctionItem(..) |
125 clean::VariantItem(..) | clean::MethodItem(..) |
126 clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) => {
127 if !self.exported_items.contains(&i.id) {
132 clean::ViewItemItem(..) | clean::StructFieldItem(..) => {
133 if i.visibility != Some(ast::Public) {
139 clean::ModuleItem(..) => {}
141 // trait impls for private items should be stripped
142 clean::ImplItem(clean::Impl{ for_: clean::ResolvedPath{ id: ref for_id, .. }, .. }) => {
143 if !self.exported_items.contains(for_id) {
147 clean::ImplItem(..) => {}
149 // tymethods/macros have no control over privacy
150 clean::MacroItem(..) | clean::TyMethodItem(..) => {}
153 let fastreturn = match i.inner {
154 // nothing left to do for traits (don't want to filter their
155 // methods out, visibility controlled by the trait)
156 clean::TraitItem(..) => true,
158 // implementations of traits are always public.
159 clean::ImplItem(ref imp) if imp.trait_.is_some() => true,
164 let i = if fastreturn {
165 self.retained.insert(i.id);
168 self.fold_item_recur(i)
174 // emptied modules/impls have no need to exist
175 clean::ModuleItem(ref m)
176 if m.items.len() == 0 &&
177 i.doc_value().is_none() => None,
178 clean::ImplItem(ref i) if i.methods.len() == 0 => None,
180 self.retained.insert(i.id);
190 // This stripper discards all private impls of traits
191 struct ImplStripper<'a>(&'a HashSet<ast::NodeId>);
192 impl<'a> fold::DocFolder for ImplStripper<'a> {
193 fn fold_item(&mut self, i: Item) -> Option<Item> {
195 clean::ImplItem(ref imp) => {
197 Some(clean::ResolvedPath{ id, .. }) => {
198 let ImplStripper(s) = *self;
199 if !s.contains(&id) {
203 Some(..) | None => {}
208 self.fold_item_recur(i)
213 pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
214 struct CommentCleaner;
215 impl fold::DocFolder for CommentCleaner {
216 fn fold_item(&mut self, i: Item) -> Option<Item> {
218 let mut avec: Vec<clean::Attribute> = Vec::new();
219 for attr in i.attrs.iter() {
221 &clean::NameValue(ref x, ref s) if "doc" == *x => avec.push(
222 clean::NameValue(~"doc", unindent(*s))),
223 x => avec.push(x.clone())
227 self.fold_item_recur(i)
230 let mut cleaner = CommentCleaner;
231 let krate = cleaner.fold_crate(krate);
235 pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
237 impl fold::DocFolder for Collapser {
238 fn fold_item(&mut self, i: Item) -> Option<Item> {
239 let mut docstr = StrBuf::new();
241 for attr in i.attrs.iter() {
243 clean::NameValue(ref x, ref s) if "doc" == *x => {
244 docstr.push_str(s.clone());
245 docstr.push_char('\n');
250 let mut a: Vec<clean::Attribute> = i.attrs.iter().filter(|&a| match a {
251 &clean::NameValue(ref x, _) if "doc" == *x => false,
253 }).map(|x| x.clone()).collect();
254 if docstr.len() > 0 {
255 a.push(clean::NameValue(~"doc", docstr.into_owned()));
258 self.fold_item_recur(i)
261 let mut collapser = Collapser;
262 let krate = collapser.fold_crate(krate);
266 pub fn unindent(s: &str) -> ~str {
267 let lines = s.lines_any().collect::<Vec<&str> >();
268 let mut saw_first_line = false;
269 let mut saw_second_line = false;
270 let min_indent = lines.iter().fold(uint::MAX, |min_indent, line| {
272 // After we see the first non-whitespace line, look at
273 // the line we have. If it is not whitespace, and therefore
274 // part of the first paragraph, then ignore the indentation
275 // level of the first line
276 let ignore_previous_indents =
279 !line.is_whitespace();
281 let min_indent = if ignore_previous_indents {
288 saw_second_line = true;
291 if line.is_whitespace() {
294 saw_first_line = true;
296 line.chars().all(|char| {
297 // Only comparing against space because I wouldn't
298 // know what to do with mixed whitespace chars
306 cmp::min(min_indent, spaces)
310 if lines.len() >= 1 {
311 let mut unindented = vec!( lines.get(0).trim() );
312 unindented.push_all(lines.tail().iter().map(|&line| {
313 if line.is_whitespace() {
316 assert!(line.len() >= min_indent);
317 line.slice_from(min_indent)
319 }).collect::<Vec<_>>().as_slice());
320 unindented.connect("\n")
331 fn should_unindent() {
332 let s = ~" line1\n line2";
334 assert_eq!(r, ~"line1\nline2");
338 fn should_unindent_multiple_paragraphs() {
339 let s = ~" line1\n\n line2";
341 assert_eq!(r, ~"line1\n\nline2");
345 fn should_leave_multiple_indent_levels() {
346 // Line 2 is indented another level beyond the
347 // base indentation and should be preserved
348 let s = ~" line1\n\n line2";
350 assert_eq!(r, ~"line1\n\n line2");
354 fn should_ignore_first_line_indent() {
355 // Thi first line of the first paragraph may not be indented as
356 // far due to the way the doc string was written:
358 // #[doc = "Start way over here
359 // and continue here"]
360 let s = ~"line1\n line2";
362 assert_eq!(r, ~"line1\nline2");
366 fn should_not_ignore_first_line_indent_in_a_single_line_para() {
367 let s = ~"line1\n\n line2";
369 assert_eq!(r, ~"line1\n\n line2");