Skip to content

Commit cc3f435

Browse files
authored
Rollup merge of rust-lang#46787 - varkor:contrib-6, r=QuietMisdreavus
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.
2 parents 1d57459 + b3c6102 commit cc3f435

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/librustdoc/html/render.rs

Lines changed: 13 additions & 4 deletions
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
// ```

src/librustdoc/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ 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 \
258+
program, rather than alphabetically")
259+
}),
256260
]
257261
}
258262

@@ -369,6 +373,7 @@ pub fn main_args(args: &[String]) -> isize {
369373
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
370374
let display_warnings = matches.opt_present("display-warnings");
371375
let linker = matches.opt_str("linker").map(PathBuf::from);
376+
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
372377

373378
match (should_test, markdown_input) {
374379
(true, true) => {
@@ -398,7 +403,8 @@ pub fn main_args(args: &[String]) -> isize {
398403
passes.into_iter().collect(),
399404
css_file_extension,
400405
renderinfo,
401-
render_type)
406+
render_type,
407+
sort_modules_alphabetically)
402408
.expect("failed to generate documentation");
403409
0
404410
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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.
4+
//
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.
10+
11+
// Tests the rustdoc --sort-modules-by-appearance option, that allows module declarations to appear
12+
// in the order they are declared in the source code, rather than only alphabetically.
13+
14+
// compile-flags: -Z unstable-options --sort-modules-by-appearance
15+
16+
pub mod module_b {}
17+
18+
pub mod module_c {}
19+
20+
pub mod module_a {}
21+
22+
// @matches 'sort_modules_by_appearance/index.html' '(?s)module_b.*module_c.*module_a'
23+
// @matches 'sort_modules_by_appearance/sidebar-items.js' '"module_b".*"module_c".*"module_a"'

0 commit comments

Comments
 (0)