Skip to content

Commit 7104e8f

Browse files
committed
Add an option to allow rustdoc to list modules by appearance
The `--sort-modules-by-appearance` option will list modules in the order that they appear in the source, rather than sorting them alphabetically (as is the default). This resolves rust-lang#8552.
1 parent 53a6d14 commit 7104e8f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ pub struct SharedContext {
129129
/// The directories that have already been created in this doc run. Used to reduce the number
130130
/// of spurious `create_dir_all` calls.
131131
pub created_dirs: RefCell<FxHashSet<PathBuf>>,
132+
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
133+
/// should be ordered alphabetically or in order of appearance (in the source code).
134+
pub sort_modules_alphabetically: bool,
132135
}
133136

134137
impl SharedContext {
@@ -491,7 +494,8 @@ pub fn run(mut krate: clean::Crate,
491494
passes: FxHashSet<String>,
492495
css_file_extension: Option<PathBuf>,
493496
renderinfo: RenderInfo,
494-
render_type: RenderType) -> Result<(), Error> {
497+
render_type: RenderType,
498+
sort_modules_alphabetically: bool) -> Result<(), Error> {
495499
let src_root = match krate.src {
496500
FileName::Real(ref p) => match p.parent() {
497501
Some(p) => p.to_path_buf(),
@@ -514,6 +518,7 @@ pub fn run(mut krate: clean::Crate,
514518
css_file_extension: css_file_extension.clone(),
515519
markdown_warnings: RefCell::new(vec![]),
516520
created_dirs: RefCell::new(FxHashSet()),
521+
sort_modules_alphabetically,
517522
};
518523

519524
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -1654,8 +1659,10 @@ impl Context {
16541659
.push((myname, Some(plain_summary_line(item.doc_value()))));
16551660
}
16561661

1657-
for (_, items) in &mut map {
1658-
items.sort();
1662+
if self.shared.sort_modules_alphabetically {
1663+
for (_, items) in &mut map {
1664+
items.sort();
1665+
}
16591666
}
16601667
map
16611668
}
@@ -2013,7 +2020,9 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
20132020
name_key(lhs).cmp(&name_key(rhs))
20142021
}
20152022

2016-
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
2023+
if cx.shared.sort_modules_alphabetically {
2024+
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
2025+
}
20172026
// This call is to remove reexport duplicates in cases such as:
20182027
//
20192028
// ```

Diff for: src/librustdoc/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ pub fn opts() -> Vec<RustcOptGroup> {
253253
unstable("linker", |o| {
254254
o.optopt("", "linker", "linker used for building executable test code", "PATH")
255255
}),
256+
unstable("sort-modules-by-appearance", |o| {
257+
o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the program, rather than alphabetically")
258+
}),
256259
]
257260
}
258261

@@ -369,6 +372,7 @@ pub fn main_args(args: &[String]) -> isize {
369372
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
370373
let display_warnings = matches.opt_present("display-warnings");
371374
let linker = matches.opt_str("linker").map(PathBuf::from);
375+
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
372376

373377
match (should_test, markdown_input) {
374378
(true, true) => {
@@ -398,7 +402,8 @@ pub fn main_args(args: &[String]) -> isize {
398402
passes.into_iter().collect(),
399403
css_file_extension,
400404
renderinfo,
401-
render_type)
405+
render_type,
406+
sort_modules_alphabetically)
402407
.expect("failed to generate documentation");
403408
0
404409
}

0 commit comments

Comments
 (0)