Skip to content

Commit be0b112

Browse files
committed
Replace sort_modules_alphabetically boolean with enum
This fixes the long-standing FIXME there and makes the code easier to understand. The reference to modules in both the old and new names seems potentially wrong since I believe it applies to all items.
1 parent 3fcf43b commit be0b112

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/librustdoc/config.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,9 @@ pub(crate) struct RenderOptions {
218218
///
219219
/// Be aware: This option can come both from the CLI and from crate attributes!
220220
pub(crate) playground_url: Option<String>,
221-
/// Whether to sort modules alphabetically on a module page instead of using declaration order.
222-
/// `true` by default.
223-
//
224-
// FIXME(misdreavus): the flag name is `--sort-modules-by-appearance` but the meaning is
225-
// inverted once read.
226-
pub(crate) sort_modules_alphabetically: bool,
221+
/// What sorting mode to use for module pages.
222+
/// `ModuleSorting::Alphabetical` by default.
223+
pub(crate) module_sorting: ModuleSorting,
227224
/// List of themes to extend the docs with. Original argument name is included to assist in
228225
/// displaying errors if it fails a theme check.
229226
pub(crate) themes: Vec<StylePath>,
@@ -281,6 +278,12 @@ pub(crate) struct RenderOptions {
281278
pub(crate) no_emit_shared: bool,
282279
}
283280

281+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
282+
pub(crate) enum ModuleSorting {
283+
DeclarationOrder,
284+
Alphabetical,
285+
}
286+
284287
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
285288
pub(crate) enum EmitType {
286289
Unversioned,
@@ -650,7 +653,11 @@ impl Options {
650653
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
651654
let playground_url = matches.opt_str("playground-url");
652655
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
653-
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
656+
let module_sorting = if matches.opt_present("sort-modules-by-appearance") {
657+
ModuleSorting::DeclarationOrder
658+
} else {
659+
ModuleSorting::Alphabetical
660+
};
654661
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
655662
let enable_minification = !matches.opt_present("disable-minification");
656663
let markdown_no_toc = matches.opt_present("markdown-no-toc");
@@ -731,7 +738,7 @@ impl Options {
731738
external_html,
732739
id_map,
733740
playground_url,
734-
sort_modules_alphabetically,
741+
module_sorting,
735742
themes,
736743
extension_css,
737744
extern_html_root_urls,

src/librustdoc/html/render/context.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::{
2222
};
2323

2424
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
25-
use crate::config::RenderOptions;
25+
use crate::config::{ModuleSorting, RenderOptions};
2626
use crate::docfs::{DocFS, PathError};
2727
use crate::error::Error;
2828
use crate::formats::cache::Cache;
@@ -95,7 +95,7 @@ pub(crate) struct SharedContext<'tcx> {
9595
created_dirs: RefCell<FxHashSet<PathBuf>>,
9696
/// This flag indicates whether listings of modules (in the side bar and documentation itself)
9797
/// should be ordered alphabetically or in order of appearance (in the source code).
98-
pub(super) sort_modules_alphabetically: bool,
98+
pub(super) module_sorting: ModuleSorting,
9999
/// Additional CSS files to be added to the generated docs.
100100
pub(crate) style_files: Vec<StylePath>,
101101
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
@@ -280,10 +280,13 @@ impl<'tcx> Context<'tcx> {
280280
}
281281
}
282282

283-
if self.shared.sort_modules_alphabetically {
284-
for items in map.values_mut() {
285-
items.sort();
283+
match self.shared.module_sorting {
284+
ModuleSorting::Alphabetical => {
285+
for items in map.values_mut() {
286+
items.sort();
287+
}
286288
}
289+
ModuleSorting::DeclarationOrder => {}
287290
}
288291
map
289292
}
@@ -394,7 +397,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
394397
external_html,
395398
id_map,
396399
playground_url,
397-
sort_modules_alphabetically,
400+
module_sorting,
398401
themes: style_files,
399402
default_settings,
400403
extension_css,
@@ -476,7 +479,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
476479
issue_tracker_base_url,
477480
layout,
478481
created_dirs: Default::default(),
479-
sort_modules_alphabetically,
482+
module_sorting,
480483
style_files,
481484
resource_suffix,
482485
static_root_path,

src/librustdoc/html/render/print_item.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use super::{
2323
AssocItemLink, Context, ImplRenderingParameters,
2424
};
2525
use crate::clean;
26+
use crate::config::ModuleSorting;
2627
use crate::formats::item_type::ItemType;
2728
use crate::formats::{AssocItemRender, Impl, RenderMode};
2829
use crate::html::escape::Escape;
@@ -246,8 +247,11 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
246247
compare_names(lhs.as_str(), rhs.as_str())
247248
}
248249

249-
if cx.shared.sort_modules_alphabetically {
250-
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2, cx.tcx()));
250+
match cx.shared.module_sorting {
251+
ModuleSorting::Alphabetical => {
252+
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2, cx.tcx()));
253+
}
254+
ModuleSorting::DeclarationOrder => {}
251255
}
252256
// This call is to remove re-export duplicates in cases such as:
253257
//

0 commit comments

Comments
 (0)