Skip to content

Commit 24d3388

Browse files
Add crate filtering
1 parent eb50e75 commit 24d3388

File tree

6 files changed

+130
-25
lines changed

6 files changed

+130
-25
lines changed

src/librustdoc/html/layout.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ 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-
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
87-
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+
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
91+
type=\"search\">\
92+
</div>\
8893
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
8994
<img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
9095
</a>\

src/librustdoc/html/render.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ themePicker.onblur = handleThemeButtonsBlur;
967967
&[(minifier::js::Keyword::Null, "N")]),
968968
&dst);
969969
}
970-
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst);
970+
try_err!(writeln!(&mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
971971

972972
// Update the list of all implementors for traits
973973
let dst = cx.dst.join("implementors");

src/librustdoc/html/static/main.js

+83-16
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,14 @@
259259
//
260260
// So I guess you could say things are getting pretty interoperable.
261261
function getVirtualKey(ev) {
262-
if ("key" in ev && typeof ev.key != "undefined")
262+
if ("key" in ev && typeof ev.key != "undefined") {
263263
return ev.key;
264+
}
264265

265266
var c = ev.charCode || ev.keyCode;
266-
if (c == 27)
267+
if (c == 27) {
267268
return "Escape";
269+
}
268270
return String.fromCharCode(c);
269271
}
270272

@@ -472,12 +474,13 @@
472474

473475
/**
474476
* Executes the query and builds an index of results
475-
* @param {[Object]} query [The user query]
476-
* @param {[type]} searchWords [The list of search words to query
477-
* against]
478-
* @return {[type]} [A search index of results]
477+
* @param {[Object]} query [The user query]
478+
* @param {[type]} searchWords [The list of search words to query
479+
* against]
480+
* @param {[type]} filterCrates [Crate to search in if defined]
481+
* @return {[type]} [A search index of results]
479482
*/
480-
function execQuery(query, searchWords) {
483+
function execQuery(query, searchWords, filterCrates) {
481484
function itemTypeFromName(typename) {
482485
for (var i = 0; i < itemTypes.length; ++i) {
483486
if (itemTypes[i] === typename) {
@@ -853,6 +856,9 @@
853856
{
854857
val = extractGenerics(val.substr(1, val.length - 2));
855858
for (var i = 0; i < nSearchWords; ++i) {
859+
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
860+
continue;
861+
}
856862
var in_args = findArg(searchIndex[i], val, true);
857863
var returned = checkReturned(searchIndex[i], val, true);
858864
var ty = searchIndex[i];
@@ -907,6 +913,9 @@
907913
var output = extractGenerics(parts[1]);
908914

909915
for (var i = 0; i < nSearchWords; ++i) {
916+
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
917+
continue;
918+
}
910919
var type = searchIndex[i].type;
911920
var ty = searchIndex[i];
912921
if (!type) {
@@ -978,11 +987,11 @@
978987
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
979988

980989
for (j = 0; j < nSearchWords; ++j) {
981-
var lev_distance;
982990
var ty = searchIndex[j];
983-
if (!ty) {
991+
if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
984992
continue;
985993
}
994+
var lev_distance;
986995
var lev_add = 0;
987996
if (paths.length > 1) {
988997
var lev = checkPath(contains, paths[paths.length - 1], ty);
@@ -1358,7 +1367,7 @@
13581367
return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>';
13591368
}
13601369

1361-
function showResults(results) {
1370+
function showResults(results, filterCrates) {
13621371
if (results['others'].length === 1 &&
13631372
getCurrentValue('rustdoc-go-to-only-result') === "true") {
13641373
var elem = document.createElement('a');
@@ -1376,8 +1385,13 @@
13761385
var ret_in_args = addTab(results['in_args'], query, false);
13771386
var ret_returned = addTab(results['returned'], query, false);
13781387

1388+
var filter = "";
1389+
if (filterCrates !== undefined) {
1390+
filter = " (in <b>" + filterCrates + "</b> crate)";
1391+
}
1392+
13791393
var output = '<h1>Results for ' + escape(query.query) +
1380-
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + '</h1>' +
1394+
(query.type ? ' (type: ' + escape(query.type) + ')' : '') + filter + '</h1>' +
13811395
'<div id="titles">' +
13821396
makeTabHeader(0, "In Names", ret_others[1]) +
13831397
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
@@ -1406,7 +1420,7 @@
14061420
printTab(currentTab);
14071421
}
14081422

1409-
function execSearch(query, searchWords) {
1423+
function execSearch(query, searchWords, filterCrates) {
14101424
var queries = query.raw.split(",");
14111425
var results = {
14121426
'in_args': [],
@@ -1417,7 +1431,7 @@
14171431
for (var i = 0; i < queries.length; ++i) {
14181432
var query = queries[i].trim();
14191433
if (query.length !== 0) {
1420-
var tmp = execQuery(getQuery(query), searchWords);
1434+
var tmp = execQuery(getQuery(query), searchWords, filterCrates);
14211435

14221436
results['in_args'].push(tmp['in_args']);
14231437
results['returned'].push(tmp['returned']);
@@ -1479,15 +1493,27 @@
14791493
}
14801494
}
14811495

1482-
function search(e) {
1496+
function getFilterCrates() {
1497+
var elem = document.getElementById("crate-search");
1498+
1499+
if (elem && elem.value !== "All crates" && rawSearchIndex.hasOwnProperty(elem.value)) {
1500+
return elem.value;
1501+
}
1502+
return undefined;
1503+
}
1504+
1505+
function search(e, forced) {
14831506
var params = getQueryStringParams();
14841507
var query = getQuery(search_input.value.trim());
14851508

14861509
if (e) {
14871510
e.preventDefault();
14881511
}
14891512

1490-
if (query.query.length === 0 || query.id === currentResults) {
1513+
if (query.query.length === 0) {
1514+
return;
1515+
}
1516+
if (forced !== true && query.id === currentResults) {
14911517
if (query.query.length > 0) {
14921518
putBackSearch(search_input);
14931519
}
@@ -1507,7 +1533,8 @@
15071533
}
15081534
}
15091535

1510-
showResults(execSearch(query, index));
1536+
var filterCrates = getFilterCrates();
1537+
showResults(execSearch(query, index, filterCrates), filterCrates);
15111538
}
15121539

15131540
function buildIndex(rawSearchIndex) {
@@ -1607,6 +1634,13 @@
16071634
};
16081635
search_input.onpaste = search_input.onchange;
16091636

1637+
var selectCrate = document.getElementById('crate-search');
1638+
if (selectCrate) {
1639+
selectCrate.onchange = function() {
1640+
search(undefined, true);
1641+
};
1642+
}
1643+
16101644
// Push and pop states are used to add search results to the browser
16111645
// history.
16121646
if (browserSupportsHistoryApi()) {
@@ -2283,6 +2317,39 @@
22832317
if (window.location.hash && window.location.hash.length > 0) {
22842318
expandSection(window.location.hash.replace(/^#/, ''));
22852319
}
2320+
2321+
function addSearchOptions(crates) {
2322+
var elem = document.getElementById('crate-search');
2323+
2324+
if (!elem) {
2325+
return;
2326+
}
2327+
var crates_text = [];
2328+
for (var crate in crates) {
2329+
if (crates.hasOwnProperty(crate)) {
2330+
crates_text.push(crate);
2331+
}
2332+
}
2333+
crates_text.sort(function(a, b) {
2334+
var lower_a = a.toLowerCase();
2335+
var lower_b = b.toLowerCase();
2336+
2337+
if (lower_a < lower_b) {
2338+
return -1;
2339+
} else if (lower_a > lower_b) {
2340+
return 1;
2341+
}
2342+
return 0;
2343+
});
2344+
for (var i = 0; i < crates_text.length; ++i) {
2345+
var option = document.createElement("option");
2346+
option.value = crates_text[i];
2347+
option.innerText = crates_text[i];
2348+
elem.appendChild(option);
2349+
}
2350+
}
2351+
2352+
window.addSearchOptions = addSearchOptions;
22862353
}());
22872354

22882355
// 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
@@ -588,27 +588,47 @@ a {
588588
.search-container {
589589
position: relative;
590590
}
591+
.search-container > div {
592+
display: inline-flex;
593+
width: calc(100% - 34px);
594+
}
595+
#crate-search {
596+
margin-top: 5px;
597+
padding: 6px;
598+
padding-right: 12px;
599+
border: 0;
600+
border-right: 0;
601+
border-radius: 4px 0 0 4px;
602+
outline: none;
603+
cursor: pointer;
604+
border-right: 1px solid;
605+
-moz-appearance: none;
606+
-webkit-appearance: none;
607+
/* Removes default arrow from firefox */
608+
text-indent: 0.01px;
609+
text-overflow: "";
610+
}
591611
.search-container > .top-button {
592612
position: absolute;
593613
right: 0;
594614
top: 10px;
595615
}
596616
.search-input {
597-
width: calc(100% - 34px);
598617
/* Override Normalize.css: we have margins and do
599618
not want to overflow - the `moz` attribute is necessary
600619
until Firefox 29, too early to drop at this point */
601620
-moz-box-sizing: border-box !important;
602621
box-sizing: border-box !important;
603622
outline: none;
604623
border: none;
605-
border-radius: 1px;
624+
border-radius: 0 1px 1px 0;
606625
margin-top: 5px;
607626
padding: 10px 16px;
608627
font-size: 17px;
609628
transition: border-color 300ms ease;
610629
transition: border-radius 300ms ease-in-out;
611630
transition: box-shadow 300ms ease-in-out;
631+
width: 100%;
612632
}
613633

614634
.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)