Skip to content

Commit cd75847

Browse files
committedNov 21, 2014
auto merge of #18822 : scialex/rust/better-rustdoc, r=alexcrichton
Changed `rustdoc` so that if we do not have the `strip-private` pass enabled private modules will be included in the generated documentation I added this because it is useful to be able to read the documentation in the very nice `rustdoc` web interface when doing internal work on a project. In this case we want the `rustdoc` to include modules that are hidden from consumers. Since this is not currently possible I added it here.
2 parents 97c043b + 26107f6 commit cd75847

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed
 

Diff for: ‎src/librustdoc/html/render.rs

+47-39
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub struct Context {
101101
/// real location of an item. This is used to allow external links to
102102
/// publicly reused items to redirect to the right location.
103103
pub render_redirect_pages: bool,
104+
/// All the passes that were run on this crate.
105+
pub passes: HashSet<String>,
104106
}
105107

106108
/// Indicates where an external crate can be found.
@@ -190,6 +192,7 @@ pub struct Cache {
190192
parent_stack: Vec<ast::DefId>,
191193
search_index: Vec<IndexItem>,
192194
privmod: bool,
195+
remove_priv: bool,
193196
public_items: NodeSet,
194197

195198
// In rare case where a structure is defined in one module but implemented
@@ -236,9 +239,13 @@ local_data_key!(pub cache_key: Arc<Cache>)
236239
local_data_key!(pub current_location_key: Vec<String> )
237240

238241
/// Generates the documentation for `crate` into the directory `dst`
239-
pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) -> io::IoResult<()> {
242+
pub fn run(mut krate: clean::Crate,
243+
external_html: &ExternalHtml,
244+
dst: Path,
245+
passes: HashSet<String>) -> io::IoResult<()> {
240246
let mut cx = Context {
241247
dst: dst,
248+
passes: passes,
242249
current: Vec::new(),
243250
root_path: String::new(),
244251
sidebar: HashMap::new(),
@@ -320,6 +327,7 @@ pub fn run(mut krate: clean::Crate, external_html: &ExternalHtml, dst: Path) ->
320327
search_index: Vec::new(),
321328
extern_locations: HashMap::new(),
322329
primitive_locations: HashMap::new(),
330+
remove_priv: cx.passes.contains("strip-private"),
323331
privmod: false,
324332
public_items: public_items,
325333
orphan_methods: Vec::new(),
@@ -767,7 +775,7 @@ impl DocFolder for Cache {
767775
let orig_privmod = match item.inner {
768776
clean::ModuleItem(..) => {
769777
let prev = self.privmod;
770-
self.privmod = prev || item.visibility != Some(ast::Public);
778+
self.privmod = prev || (self.remove_priv && item.visibility != Some(ast::Public));
771779
prev
772780
}
773781
_ => self.privmod,
@@ -1192,7 +1200,7 @@ impl Context {
11921200
// these modules are recursed into, but not rendered normally (a
11931201
// flag on the context).
11941202
if !self.render_redirect_pages {
1195-
self.render_redirect_pages = ignore_private_item(&item);
1203+
self.render_redirect_pages = self.ignore_private_item(&item);
11961204
}
11971205

11981206
match item.inner {
@@ -1211,7 +1219,7 @@ impl Context {
12111219
clean::ModuleItem(m) => m,
12121220
_ => unreachable!()
12131221
};
1214-
this.sidebar = build_sidebar(&m);
1222+
this.sidebar = this.build_sidebar(&m);
12151223
for item in m.items.into_iter() {
12161224
f(this,item);
12171225
}
@@ -1230,6 +1238,40 @@ impl Context {
12301238
_ => Ok(())
12311239
}
12321240
}
1241+
1242+
fn build_sidebar(&self, m: &clean::Module) -> HashMap<String, Vec<String>> {
1243+
let mut map = HashMap::new();
1244+
for item in m.items.iter() {
1245+
if self.ignore_private_item(item) { continue }
1246+
1247+
let short = shortty(item).to_static_str();
1248+
let myname = match item.name {
1249+
None => continue,
1250+
Some(ref s) => s.to_string(),
1251+
};
1252+
let v = match map.entry(short.to_string()) {
1253+
Vacant(entry) => entry.set(Vec::with_capacity(1)),
1254+
Occupied(entry) => entry.into_mut(),
1255+
};
1256+
v.push(myname);
1257+
}
1258+
1259+
for (_, items) in map.iter_mut() {
1260+
items.as_mut_slice().sort();
1261+
}
1262+
return map;
1263+
}
1264+
1265+
fn ignore_private_item(&self, it: &clean::Item) -> bool {
1266+
match it.inner {
1267+
clean::ModuleItem(ref m) => {
1268+
(m.items.len() == 0 && it.doc_value().is_none()) ||
1269+
(self.passes.contains("strip-private") && it.visibility != Some(ast::Public))
1270+
}
1271+
clean::PrimitiveItem(..) => it.visibility != Some(ast::Public),
1272+
_ => false,
1273+
}
1274+
}
12331275
}
12341276

12351277
impl<'a> Item<'a> {
@@ -1443,7 +1485,7 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
14431485
try!(document(w, item));
14441486

14451487
let mut indices = range(0, items.len()).filter(|i| {
1446-
!ignore_private_item(&items[*i])
1488+
!cx.ignore_private_item(&items[*i])
14471489
}).collect::<Vec<uint>>();
14481490

14491491
fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
@@ -2157,29 +2199,6 @@ impl<'a> fmt::Show for Sidebar<'a> {
21572199
}
21582200
}
21592201

2160-
fn build_sidebar(m: &clean::Module) -> HashMap<String, Vec<String>> {
2161-
let mut map = HashMap::new();
2162-
for item in m.items.iter() {
2163-
if ignore_private_item(item) { continue }
2164-
2165-
let short = shortty(item).to_static_str();
2166-
let myname = match item.name {
2167-
None => continue,
2168-
Some(ref s) => s.to_string(),
2169-
};
2170-
let v = match map.entry(short.to_string()) {
2171-
Vacant(entry) => entry.set(Vec::with_capacity(1)),
2172-
Occupied(entry) => entry.into_mut(),
2173-
};
2174-
v.push(myname);
2175-
}
2176-
2177-
for (_, items) in map.iter_mut() {
2178-
items.as_mut_slice().sort();
2179-
}
2180-
return map;
2181-
}
2182-
21832202
impl<'a> fmt::Show for Source<'a> {
21842203
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
21852204
let Source(s) = *self;
@@ -2214,17 +2233,6 @@ fn item_primitive(w: &mut fmt::Formatter,
22142233
render_methods(w, it)
22152234
}
22162235

2217-
fn ignore_private_item(it: &clean::Item) -> bool {
2218-
match it.inner {
2219-
clean::ModuleItem(ref m) => {
2220-
(m.items.len() == 0 && it.doc_value().is_none()) ||
2221-
it.visibility != Some(ast::Public)
2222-
}
2223-
clean::PrimitiveItem(..) => it.visibility != Some(ast::Public),
2224-
_ => false,
2225-
}
2226-
}
2227-
22282236
fn get_basic_keywords() -> &'static str {
22292237
"rust, rustlang, rust-lang"
22302238
}

Diff for: ‎src/librustdoc/lib.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ static DEFAULT_PASSES: &'static [&'static str] = &[
8686

8787
local_data_key!(pub analysiskey: core::CrateAnalysis)
8888

89-
type Output = (clean::Crate, Vec<plugins::PluginJson> );
89+
struct Output {
90+
krate: clean::Crate,
91+
json_plugins: Vec<plugins::PluginJson>,
92+
passes: Vec<String>,
93+
}
9094

9195
pub fn main() {
9296
std::os::set_exit_status(main_args(std::os::args().as_slice()));
@@ -229,24 +233,26 @@ pub fn main_args(args: &[String]) -> int {
229233
(false, false) => {}
230234
}
231235

232-
let (krate, res) = match acquire_input(input, externs, &matches) {
233-
Ok(pair) => pair,
236+
let out = match acquire_input(input, externs, &matches) {
237+
Ok(out) => out,
234238
Err(s) => {
235239
println!("input error: {}", s);
236240
return 1;
237241
}
238242
};
239-
243+
let Output { krate, json_plugins, passes, } = out;
240244
info!("going to format");
241245
match matches.opt_str("w").as_ref().map(|s| s.as_slice()) {
242246
Some("html") | None => {
243-
match html::render::run(krate, &external_html, output.unwrap_or(Path::new("doc"))) {
247+
match html::render::run(krate, &external_html, output.unwrap_or(Path::new("doc")),
248+
passes.into_iter().collect()) {
244249
Ok(()) => {}
245250
Err(e) => panic!("failed to generate documentation: {}", e),
246251
}
247252
}
248253
Some("json") => {
249-
match json_output(krate, res, output.unwrap_or(Path::new("doc.json"))) {
254+
match json_output(krate, json_plugins,
255+
output.unwrap_or(Path::new("doc.json"))) {
250256
Ok(()) => {}
251257
Err(e) => panic!("failed to write json: {}", e),
252258
}
@@ -397,7 +403,8 @@ fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matche
397403

398404
// Run everything!
399405
info!("Executing passes/plugins");
400-
return pm.run_plugins(krate);
406+
let (krate, json) = pm.run_plugins(krate);
407+
return Output { krate: krate, json_plugins: json, passes: passes, };
401408
}
402409

403410
/// This input format purely deserializes the json output file. No passes are
@@ -435,7 +442,7 @@ fn json_input(input: &str) -> Result<Output, String> {
435442
// FIXME: this should read from the "plugins" field, but currently
436443
// Json doesn't implement decodable...
437444
let plugin_output = Vec::new();
438-
Ok((krate, plugin_output))
445+
Ok(Output { krate: krate, json_plugins: plugin_output, passes: Vec::new(), })
439446
}
440447
Ok(..) => {
441448
Err("malformed json input: expected an object at the \

0 commit comments

Comments
 (0)
Please sign in to comment.