Skip to content

Commit 8f071bb

Browse files
committed
rustdoc: Add indexes to native mods. Closes #1963
1 parent cb1efb0 commit 8f071bb

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

src/rustdoc/doc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type moddoc = {
6363

6464
type nmoddoc = {
6565
item: itemdoc,
66-
fns: [fndoc]
66+
fns: [fndoc],
67+
index: option<index>
6768
};
6869

6970
type constdoc = simpleitemdoc;

src/rustdoc/extract.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ fn nmoddoc_from_mod(
125125
fndoc_from_fn(itemdoc)
126126
}
127127
}
128-
}
128+
},
129+
index: none
129130
}
130131
}
131132

src/rustdoc/markdown_index_pass.rs

+45-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ fn run(
1717
config: config::config
1818
) -> doc::doc {
1919
let fold = fold::fold({
20-
fold_mod: fold_mod
21-
with *fold::default_any_fold(config)
20+
fold_mod: fold_mod,
21+
fold_nmod: fold_nmod
22+
with *fold::default_any_fold(config)
2223
});
2324
fold.fold_doc(fold, doc)
2425
}
@@ -31,18 +32,42 @@ fn fold_mod(
3132
let doc = fold::default_any_fold_mod(fold, doc);
3233

3334
{
34-
index: some(build_index(doc, fold.ctxt))
35+
index: some(build_mod_index(doc, fold.ctxt))
3536
with doc
3637
}
3738
}
3839

39-
fn build_index(
40+
fn fold_nmod(
41+
fold: fold::fold<config::config>,
42+
doc: doc::nmoddoc
43+
) -> doc::nmoddoc {
44+
45+
let doc = fold::default_any_fold_nmod(fold, doc);
46+
47+
{
48+
index: some(build_nmod_index(doc, fold.ctxt))
49+
with doc
50+
}
51+
}
52+
53+
fn build_mod_index(
4054
doc: doc::moddoc,
4155
config: config::config
4256
) -> doc::index {
4357
{
44-
entries: par::anymap(doc.items) {|item|
45-
item_to_entry(item, config)
58+
entries: par::anymap(doc.items) {|doc|
59+
item_to_entry(doc, config)
60+
}
61+
}
62+
}
63+
64+
fn build_nmod_index(
65+
doc: doc::nmoddoc,
66+
config: config::config
67+
) -> doc::index {
68+
{
69+
entries: par::anymap(doc.fns) {|doc|
70+
item_to_entry(doc::fntag(doc), config)
4671
}
4772
}
4873
}
@@ -172,6 +197,20 @@ fn should_add_brief_desc_to_index() {
172197
assert option::get(doc.cratemod().index).entries[0].brief == some("test");
173198
}
174199

200+
#[test]
201+
fn should_index_native_mod_contents() {
202+
let doc = test::mk_doc(
203+
config::doc_per_crate,
204+
"native mod a { fn b(); }"
205+
);
206+
assert option::get(doc.cratemod().nmods()[0].index).entries[0] == {
207+
kind: "Function",
208+
name: "b",
209+
brief: none,
210+
link: "#function-b"
211+
};
212+
}
213+
175214
#[cfg(test)]
176215
mod test {
177216
fn mk_doc(output_style: config::output_style, source: str) -> doc::doc {

src/rustdoc/markdown_pass.rs

+12
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,20 @@ fn should_not_write_index_if_no_entries() {
416416
assert !str::contains(markdown, "\n\n\n");
417417
}
418418

419+
#[test]
420+
fn should_write_index_for_native_mods() {
421+
let markdown = test::render("native mod a { fn a(); }");
422+
assert str::contains(
423+
markdown,
424+
"\n\n* [Function `a`](#function-a)\n\n"
425+
);
426+
}
427+
419428
fn write_nmod(ctxt: ctxt, doc: doc::nmoddoc) {
420429
write_common(ctxt, doc.desc(), doc.sections());
430+
if option::is_some(doc.index) {
431+
write_index(ctxt, option::get(doc.index));
432+
}
421433

422434
for fndoc in doc.fns {
423435
write_item_header(ctxt, doc::fntag(fndoc));

0 commit comments

Comments
 (0)