Skip to content

Commit dd717de

Browse files
Add crate filtering
1 parent b76ee83 commit dd717de

File tree

6 files changed

+131
-26
lines changed

6 files changed

+131
-26
lines changed

src/librustdoc/html/layout.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
8181
<nav class=\"sub\">\
8282
<form class=\"search-form js-only\">\
8383
<div class=\"search-container\">\
84-
<input class=\"search-input\" name=\"search\" \
85-
autocomplete=\"off\" \
86-
spellcheck=\"false\" \
87-
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
88-
type=\"search\">\
84+
<div>\
85+
<select id=\"crate-search\">\
86+
<option value=\"All crates\">All crates</option>\
87+
</select>\
88+
<input class=\"search-input\" name=\"search\" \
89+
autocomplete=\"off\" \
90+
spellcheck=\"false\" \
91+
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
92+
type=\"search\">\
93+
</div>\
8994
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
9095
<img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
9196
</a>\

src/librustdoc/html/render.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ themePicker.onblur = handleThemeButtonsBlur;
983983
&[(minifier::js::Keyword::Null, "N")]),
984984
&dst);
985985
}
986-
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst);
986+
try_err!(writeln!(&mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
987987

988988
if options.enable_index_page {
989989
if let Some(index_page) = options.index_page.clone() {

src/librustdoc/html/static/main.js

+83-16
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,14 @@
254254
//
255255
// So I guess you could say things are getting pretty interoperable.
256256
function getVirtualKey(ev) {
257-
if ("key" in ev && typeof ev.key != "undefined")
257+
if ("key" in ev && typeof ev.key != "undefined") {
258258
return ev.key;
259+
}
259260

260261
var c = ev.charCode || ev.keyCode;
261-
if (c == 27)
262+
if (c == 27) {
262263
return "Escape";
264+
}
263265
return String.fromCharCode(c);
264266
}
265267

@@ -467,12 +469,13 @@
467469

468470
/**
469471
* Executes the query and builds an index of results
470-
* @param {[Object]} query [The user query]
471-
* @param {[type]} searchWords [The list of search words to query
472-
* against]
473-
* @return {[type]} [A search index of results]
472+
* @param {[Object]} query [The user query]
473+
* @param {[type]} searchWords [The list of search words to query
474+
* against]
475+
* @param {[type]} filterCrates [Crate to search in if defined]
476+
* @return {[type]} [A search index of results]
474477
*/
475-
function execQuery(query, searchWords) {
478+
function execQuery(query, searchWords, filterCrates) {
476479
function itemTypeFromName(typename) {
477480
for (var i = 0; i < itemTypes.length; ++i) {
478481
if (itemTypes[i] === typename) {
@@ -848,6 +851,9 @@
848851
{
849852
val = extractGenerics(val.substr(1, val.length - 2));
850853
for (var i = 0; i < nSearchWords; ++i) {
854+
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
855+
continue;
856+
}
851857
var in_args = findArg(searchIndex[i], val, true);
852858
var returned = checkReturned(searchIndex[i], val, true);
853859
var ty = searchIndex[i];
@@ -902,6 +908,9 @@
902908
var output = extractGenerics(parts[1]);
903909

904910
for (var i = 0; i < nSearchWords; ++i) {
911+
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
912+
continue;
913+
}
905914
var type = searchIndex[i].type;
906915
var ty = searchIndex[i];
907916
if (!type) {
@@ -973,11 +982,11 @@
973982
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
974983

975984
for (j = 0; j < nSearchWords; ++j) {
976-
var lev_distance;
977985
var ty = searchIndex[j];
978-
if (!ty) {
986+
if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
979987
continue;
980988
}
989+
var lev_distance;
981990
var lev_add = 0;
982991
if (paths.length > 1) {
983992
var lev = checkPath(contains, paths[paths.length - 1], ty);
@@ -1353,7 +1362,7 @@
13531362
return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>';
13541363
}
13551364

1356-
function showResults(results) {
1365+
function showResults(results, filterCrates) {
13571366
if (results['others'].length === 1 &&
13581367
getCurrentValue('rustdoc-go-to-only-result') === "true") {
13591368
var elem = document.createElement('a');
@@ -1371,8 +1380,13 @@
13711380
var ret_in_args = addTab(results['in_args'], query, false);
13721381
var ret_returned = addTab(results['returned'], query, false);
13731382

1383+
var filter = "";
1384+
if (filterCrates !== undefined) {
1385+
filter = " (in <b>" + filterCrates + "</b> crate)";
1386+
}
1387+
13741388
var output = '<h1>Results for ' + escape(query.query) +
1375-
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' +
1389+
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + filter + '</h1>' +
13761390
'<div id="titles">' +
13771391
makeTabHeader(0, "In Names", ret_others[1]) +
13781392
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
@@ -1401,7 +1415,7 @@
14011415
printTab(currentTab);
14021416
}
14031417

1404-
function execSearch(query, searchWords) {
1418+
function execSearch(query, searchWords, filterCrates) {
14051419
var queries = query.raw.split(",");
14061420
var results = {
14071421
'in_args': [],
@@ -1412,7 +1426,7 @@
14121426
for (var i = 0; i < queries.length; ++i) {
14131427
var query = queries[i].trim();
14141428
if (query.length !== 0) {
1415-
var tmp = execQuery(getQuery(query), searchWords);
1429+
var tmp = execQuery(getQuery(query), searchWords, filterCrates);
14161430

14171431
results['in_args'].push(tmp['in_args']);
14181432
results['returned'].push(tmp['returned']);
@@ -1474,15 +1488,27 @@
14741488
}
14751489
}
14761490

1477-
function search(e) {
1491+
function getFilterCrates() {
1492+
var elem = document.getElementById("crate-search");
1493+
1494+
if (elem && elem.value !== "All crates" && rawSearchIndex.hasOwnProperty(elem.value)) {
1495+
return elem.value;
1496+
}
1497+
return undefined;
1498+
}
1499+
1500+
function search(e, forced) {
14781501
var params = getQueryStringParams();
14791502
var query = getQuery(search_input.value.trim());
14801503

14811504
if (e) {
14821505
e.preventDefault();
14831506
}
14841507

1485-
if (query.query.length === 0 || query.id === currentResults) {
1508+
if (query.query.length === 0) {
1509+
return;
1510+
}
1511+
if (forced !== true && query.id === currentResults) {
14861512
if (query.query.length > 0) {
14871513
putBackSearch(search_input);
14881514
}
@@ -1502,7 +1528,8 @@
15021528
}
15031529
}
15041530

1505-
showResults(execSearch(query, index));
1531+
var filterCrates = getFilterCrates();
1532+
showResults(execSearch(query, index, filterCrates), filterCrates);
15061533
}
15071534

15081535
function buildIndex(rawSearchIndex) {
@@ -1602,6 +1629,13 @@
16021629
};
16031630
search_input.onpaste = search_input.onchange;
16041631

1632+
var selectCrate = document.getElementById('crate-search');
1633+
if (selectCrate) {
1634+
selectCrate.onchange = function() {
1635+
search(undefined, true);
1636+
};
1637+
}
1638+
16051639
// Push and pop states are used to add search results to the browser
16061640
// history.
16071641
if (browserSupportsHistoryApi()) {
@@ -2350,6 +2384,39 @@
23502384
if (window.location.hash && window.location.hash.length > 0) {
23512385
expandSection(window.location.hash.replace(/^#/, ''));
23522386
}
2387+
2388+
function addSearchOptions(crates) {
2389+
var elem = document.getElementById('crate-search');
2390+
2391+
if (!elem) {
2392+
return;
2393+
}
2394+
var crates_text = [];
2395+
for (var crate in crates) {
2396+
if (crates.hasOwnProperty(crate)) {
2397+
crates_text.push(crate);
2398+
}
2399+
}
2400+
crates_text.sort(function(a, b) {
2401+
var lower_a = a.toLowerCase();
2402+
var lower_b = b.toLowerCase();
2403+
2404+
if (lower_a < lower_b) {
2405+
return -1;
2406+
} else if (lower_a > lower_b) {
2407+
return 1;
2408+
}
2409+
return 0;
2410+
});
2411+
for (var i = 0; i < crates_text.length; ++i) {
2412+
var option = document.createElement("option");
2413+
option.value = crates_text[i];
2414+
option.innerText = crates_text[i];
2415+
elem.appendChild(option);
2416+
}
2417+
}
2418+
2419+
window.addSearchOptions = addSearchOptions;
23532420
}());
23542421

23552422
// Sets the focus on the search bar at the top of the page

src/librustdoc/html/static/rustdoc.css

+22-2
Original file line numberDiff line numberDiff line change
@@ -614,27 +614,47 @@ a {
614614
.search-container {
615615
position: relative;
616616
}
617+
.search-container > div {
618+
display: inline-flex;
619+
width: calc(100% - 34px);
620+
}
621+
#crate-search {
622+
margin-top: 5px;
623+
padding: 6px;
624+
padding-right: 12px;
625+
border: 0;
626+
border-right: 0;
627+
border-radius: 4px 0 0 4px;
628+
outline: none;
629+
cursor: pointer;
630+
border-right: 1px solid;
631+
-moz-appearance: none;
632+
-webkit-appearance: none;
633+
/* Removes default arrow from firefox */
634+
text-indent: 0.01px;
635+
text-overflow: "";
636+
}
617637
.search-container > .top-button {
618638
position: absolute;
619639
right: 0;
620640
top: 10px;
621641
}
622642
.search-input {
623-
width: calc(100% - 34px);
624643
/* Override Normalize.css: we have margins and do
625644
not want to overflow - the `moz` attribute is necessary
626645
until Firefox 29, too early to drop at this point */
627646
-moz-box-sizing: border-box !important;
628647
box-sizing: border-box !important;
629648
outline: none;
630649
border: none;
631-
border-radius: 1px;
650+
border-radius: 0 1px 1px 0;
632651
margin-top: 5px;
633652
padding: 10px 16px;
634653
font-size: 17px;
635654
transition: border-color 300ms ease;
636655
transition: border-radius 300ms ease-in-out;
637656
transition: box-shadow 300ms ease-in-out;
657+
width: 100%;
638658
}
639659

640660
.search-input:focus {

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,15 @@ a.test-arrow {
182182
color: #999;
183183
}
184184

185+
#crate-search {
186+
color: #111;
187+
background-color: #f0f0f0;
188+
border-color: #000;
189+
}
190+
185191
.search-input {
186192
color: #111;
187-
box-shadow: 0 0 0 1px #000, 0 0 0 2px transparent;
193+
box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
188194
background-color: #f0f0f0;
189195
}
190196

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,16 @@ a.test-arrow {
182182
color: #999;
183183
}
184184

185+
#crate-search {
186+
color: #555;
187+
background-color: white;
188+
border-color: #e0e0e0;
189+
box-shadow: 0px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
190+
}
191+
185192
.search-input {
186193
color: #555;
187-
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent;
194+
box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
188195
background-color: white;
189196
}
190197

0 commit comments

Comments
 (0)