Skip to content

Commit 43d26b1

Browse files
committed
Auto merge of #57006 - GuillaumeGomez:no-crate-filter, r=QuietMisdreavus
Add no-crate filter option on rustdoc @onur asked me about it so here it is! r? @QuietMisdreavus
2 parents d9e3edb + dbcf689 commit 43d26b1

File tree

9 files changed

+78
-19
lines changed

9 files changed

+78
-19
lines changed

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ pub struct RenderOptions {
185185
/// If present, playground URL to use in the "Run" button added to code samples generated from
186186
/// standalone Markdown files. If not present, `playground_url` is used.
187187
pub markdown_playground_url: Option<String>,
188+
/// If false, the `select` element to have search filtering by crates on rendered docs
189+
/// won't be generated.
190+
pub generate_search_filter: bool,
188191
}
189192

190193
impl Options {
@@ -427,6 +430,7 @@ impl Options {
427430
let crate_version = matches.opt_str("crate-version");
428431
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
429432
let static_root_path = matches.opt_str("static-root-path");
433+
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
430434

431435
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
432436

@@ -469,6 +473,7 @@ impl Options {
469473
markdown_no_toc,
470474
markdown_css,
471475
markdown_playground_url,
476+
generate_search_filter,
472477
}
473478
})
474479
}

src/librustdoc/html/layout.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ pub struct Page<'a> {
2525
}
2626

2727
pub fn render<T: fmt::Display, S: fmt::Display>(
28-
dst: &mut dyn io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
29-
css_file_extension: bool, themes: &[PathBuf])
30-
-> io::Result<()>
31-
{
28+
dst: &mut dyn io::Write,
29+
layout: &Layout,
30+
page: &Page,
31+
sidebar: &S,
32+
t: &T,
33+
css_file_extension: bool,
34+
themes: &[PathBuf],
35+
generate_search_filter: bool,
36+
) -> io::Result<()> {
3237
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
3338
write!(dst,
3439
"<!DOCTYPE html>\
@@ -81,10 +86,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
8186
<nav class=\"sub\">\
8287
<form class=\"search-form js-only\">\
8388
<div class=\"search-container\">\
84-
<div>\
85-
<select id=\"crate-search\">\
86-
<option value=\"All crates\">All crates</option>\
87-
</select>\
89+
<div>{filter_crates}\
8890
<input class=\"search-input\" name=\"search\" \
8991
autocomplete=\"off\" \
9092
spellcheck=\"false\" \
@@ -214,6 +216,13 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
214216
root_path=page.root_path,
215217
extra_script=e)
216218
}).collect::<String>(),
219+
filter_crates=if generate_search_filter {
220+
"<select id=\"crate-search\">\
221+
<option value=\"All crates\">All crates</option>\
222+
</select>"
223+
} else {
224+
""
225+
},
217226
)
218227
}
219228

src/librustdoc/html/render.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ struct SharedContext {
133133
/// Optional path string to be used to load static files on output pages. If not set, uses
134134
/// combinations of `../` to reach the documentation root.
135135
pub static_root_path: Option<String>,
136+
/// If false, the `select` element to have search filtering by crates on rendered docs
137+
/// won't be generated.
138+
pub generate_search_filter: bool,
136139
}
137140

138141
impl SharedContext {
@@ -500,6 +503,7 @@ pub fn run(mut krate: clean::Crate,
500503
extern_html_root_urls,
501504
resource_suffix,
502505
static_root_path,
506+
generate_search_filter,
503507
..
504508
} = options;
505509

@@ -528,6 +532,7 @@ pub fn run(mut krate: clean::Crate,
528532
themes,
529533
resource_suffix,
530534
static_root_path,
535+
generate_search_filter,
531536
};
532537

533538
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -1099,7 +1104,8 @@ themePicker.onblur = handleThemeButtonsBlur;
10991104
try_err!(layout::render(&mut w, &cx.shared.layout,
11001105
&page, &(""), &content,
11011106
cx.shared.css_file_extension.is_some(),
1102-
&cx.shared.themes), &dst);
1107+
&cx.shared.themes,
1108+
cx.shared.generate_search_filter), &dst);
11031109
try_err!(w.flush(), &dst);
11041110
}
11051111
}
@@ -1372,7 +1378,8 @@ impl<'a> SourceCollector<'a> {
13721378
layout::render(&mut w, &self.scx.layout,
13731379
&page, &(""), &Source(contents),
13741380
self.scx.css_file_extension.is_some(),
1375-
&self.scx.themes)?;
1381+
&self.scx.themes,
1382+
self.scx.generate_search_filter)?;
13761383
w.flush()?;
13771384
self.scx.local_sources.insert(p.clone(), href);
13781385
Ok(())
@@ -1974,7 +1981,8 @@ impl Context {
19741981
try_err!(layout::render(&mut w, &self.shared.layout,
19751982
&page, &sidebar, &all,
19761983
self.shared.css_file_extension.is_some(),
1977-
&self.shared.themes),
1984+
&self.shared.themes,
1985+
self.shared.generate_search_filter),
19781986
&final_file);
19791987

19801988
// Generating settings page.
@@ -1994,7 +2002,8 @@ impl Context {
19942002
try_err!(layout::render(&mut w, &layout,
19952003
&page, &sidebar, &settings,
19962004
self.shared.css_file_extension.is_some(),
1997-
&themes),
2005+
&themes,
2006+
self.shared.generate_search_filter),
19982007
&settings_file);
19992008

20002009
Ok(())
@@ -2055,7 +2064,8 @@ impl Context {
20552064
&Sidebar{ cx: self, item: it },
20562065
&Item{ cx: self, item: it },
20572066
self.shared.css_file_extension.is_some(),
2058-
&self.shared.themes)?;
2067+
&self.shared.themes,
2068+
self.shared.generate_search_filter)?;
20592069
} else {
20602070
let mut url = self.root_path();
20612071
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {

src/librustdoc/html/static/main.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2422,9 +2422,11 @@ if (!DOMTokenList.prototype.remove) {
24222422
return;
24232423
}
24242424
var crates_text = [];
2425-
for (var crate in crates) {
2426-
if (crates.hasOwnProperty(crate)) {
2427-
crates_text.push(crate);
2425+
if (crates.length > 1) {
2426+
for (var crate in crates) {
2427+
if (crates.hasOwnProperty(crate)) {
2428+
crates_text.push(crate);
2429+
}
24282430
}
24292431
}
24302432
crates_text.sort(function(a, b) {

src/librustdoc/html/static/rustdoc.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ a {
649649
box-sizing: border-box !important;
650650
outline: none;
651651
border: none;
652-
border-radius: 0 1px 1px 0;
652+
border-radius: 1px;
653653
margin-top: 5px;
654654
padding: 10px 16px;
655655
font-size: 17px;
@@ -659,6 +659,10 @@ a {
659659
width: 100%;
660660
}
661661

662+
#crate-search + .search-input {
663+
border-radius: 0 1px 1px 0;
664+
}
665+
662666
.search-input:focus {
663667
border-radius: 2px;
664668
border: 0;

src/librustdoc/html/static/themes/dark.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,18 @@ a.test-arrow {
182182

183183
.search-input {
184184
color: #111;
185-
box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
185+
box-shadow: 0 0 0 1px #000, 0 0 0 2px transparent;
186186
background-color: #f0f0f0;
187187
}
188188

189189
.search-input:focus {
190190
border-color: #008dfd;
191191
}
192192

193+
#crate-search + .search-input {
194+
box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
195+
}
196+
193197
.stab.unstable { background: #FFF5D6; border-color: #FFC600; color: #404040; }
194198
.stab.internal { background: #FFB9B3; border-color: #B71C1C; color: #404040; }
195199
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; color: #404040; }

src/librustdoc/html/static/themes/light.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,18 @@ a.test-arrow {
183183

184184
.search-input {
185185
color: #555;
186-
box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
186+
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent;
187187
background-color: white;
188188
}
189189

190190
.search-input:focus {
191191
border-color: #66afe9;
192192
}
193193

194+
#crate-search + .search-input {
195+
box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
196+
}
197+
194198
.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
195199
.stab.internal { background: #FFB9B3; border-color: #B71C1C; }
196200
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; }

src/librustdoc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ fn opts() -> Vec<RustcOptGroup> {
336336
If not set, uses combinations of '../' to reach the documentation root.",
337337
"PATH")
338338
}),
339+
unstable("disable-per-crate-search", |o| {
340+
o.optflag("",
341+
"disable-per-crate-search",
342+
"disables generating the crate selector on the search box")
343+
}),
339344
]
340345
}
341346

src/test/rustdoc/no-crate-filter.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
#![crate_name = "foo"]
12+
13+
// compile-flags: -Z unstable-options --disable-per-crate-search
14+
15+
// @!has 'foo/struct.Foo.html' '//*[id="crate-search"]'
16+
pub struct Foo;

0 commit comments

Comments
 (0)