@@ -214,12 +214,14 @@ if (!String.prototype.endsWith) {
214214 //
215215 // So I guess you could say things are getting pretty interoperable.
216216 function getVirtualKey ( ev ) {
217- if ( "key" in ev && typeof ev . key != "undefined" )
217+ if ( "key" in ev && typeof ev . key != "undefined" ) {
218218 return ev . key ;
219+ }
219220
220221 var c = ev . charCode || ev . keyCode ;
221- if ( c == 27 )
222+ if ( c == 27 ) {
222223 return "Escape" ;
224+ }
223225 return String . fromCharCode ( c ) ;
224226 }
225227
@@ -427,12 +429,13 @@ if (!String.prototype.endsWith) {
427429
428430 /**
429431 * Executes the query and builds an index of results
430- * @param {[Object] } query [The user query]
431- * @param {[type] } searchWords [The list of search words to query
432- * against]
433- * @return {[type] } [A search index of results]
432+ * @param {[Object] } query [The user query]
433+ * @param {[type] } searchWords [The list of search words to query
434+ * against]
435+ * @param {[type] } filterCrates [Crate to search in if defined]
436+ * @return {[type] } [A search index of results]
434437 */
435- function execQuery ( query , searchWords ) {
438+ function execQuery ( query , searchWords , filterCrates ) {
436439 function itemTypeFromName ( typename ) {
437440 for ( var i = 0 ; i < itemTypes . length ; ++ i ) {
438441 if ( itemTypes [ i ] === typename ) {
@@ -808,6 +811,9 @@ if (!String.prototype.endsWith) {
808811 {
809812 val = extractGenerics ( val . substr ( 1 , val . length - 2 ) ) ;
810813 for ( var i = 0 ; i < nSearchWords ; ++ i ) {
814+ if ( filterCrates !== undefined && searchIndex [ i ] . crate !== filterCrates ) {
815+ continue ;
816+ }
811817 var in_args = findArg ( searchIndex [ i ] , val , true ) ;
812818 var returned = checkReturned ( searchIndex [ i ] , val , true ) ;
813819 var ty = searchIndex [ i ] ;
@@ -862,6 +868,9 @@ if (!String.prototype.endsWith) {
862868 var output = extractGenerics ( parts [ 1 ] ) ;
863869
864870 for ( var i = 0 ; i < nSearchWords ; ++ i ) {
871+ if ( filterCrates !== undefined && searchIndex [ i ] . crate !== filterCrates ) {
872+ continue ;
873+ }
865874 var type = searchIndex [ i ] . type ;
866875 var ty = searchIndex [ i ] ;
867876 if ( ! type ) {
@@ -933,11 +942,11 @@ if (!String.prototype.endsWith) {
933942 var contains = paths . slice ( 0 , paths . length > 1 ? paths . length - 1 : 1 ) ;
934943
935944 for ( j = 0 ; j < nSearchWords ; ++ j ) {
936- var lev_distance ;
937945 var ty = searchIndex [ j ] ;
938- if ( ! ty ) {
946+ if ( ! ty || ( filterCrates !== undefined && ty . crate !== filterCrates ) ) {
939947 continue ;
940948 }
949+ var lev_distance ;
941950 var lev_add = 0 ;
942951 if ( paths . length > 1 ) {
943952 var lev = checkPath ( contains , paths [ paths . length - 1 ] , ty ) ;
@@ -1322,7 +1331,7 @@ if (!String.prototype.endsWith) {
13221331 return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>' ;
13231332 }
13241333
1325- function showResults ( results ) {
1334+ function showResults ( results , filterCrates ) {
13261335 if ( results [ 'others' ] . length === 1 &&
13271336 getCurrentValue ( 'rustdoc-go-to-only-result' ) === "true" ) {
13281337 var elem = document . createElement ( 'a' ) ;
@@ -1340,8 +1349,13 @@ if (!String.prototype.endsWith) {
13401349 var ret_in_args = addTab ( results [ 'in_args' ] , query , false ) ;
13411350 var ret_returned = addTab ( results [ 'returned' ] , query , false ) ;
13421351
1352+ var filter = "" ;
1353+ if ( filterCrates !== undefined ) {
1354+ filter = " (in <b>" + filterCrates + "</b> crate)" ;
1355+ }
1356+
13431357 var output = '<h1>Results for ' + escape ( query . query ) +
1344- ( query . type ? ' (type: ' + escape ( query . type ) + ')' : '' ) + '</h1>' +
1358+ ( query . type ? ' (type: ' + escape ( query . type ) + ')' : '' ) + filter + '</h1>' +
13451359 '<div id="titles">' +
13461360 makeTabHeader ( 0 , "In Names" , ret_others [ 1 ] ) +
13471361 makeTabHeader ( 1 , "In Parameters" , ret_in_args [ 1 ] ) +
@@ -1370,7 +1384,7 @@ if (!String.prototype.endsWith) {
13701384 printTab ( currentTab ) ;
13711385 }
13721386
1373- function execSearch ( query , searchWords ) {
1387+ function execSearch ( query , searchWords , filterCrates ) {
13741388 var queries = query . raw . split ( "," ) ;
13751389 var results = {
13761390 'in_args' : [ ] ,
@@ -1381,7 +1395,7 @@ if (!String.prototype.endsWith) {
13811395 for ( var i = 0 ; i < queries . length ; ++ i ) {
13821396 var query = queries [ i ] . trim ( ) ;
13831397 if ( query . length !== 0 ) {
1384- var tmp = execQuery ( getQuery ( query ) , searchWords ) ;
1398+ var tmp = execQuery ( getQuery ( query ) , searchWords , filterCrates ) ;
13851399
13861400 results [ 'in_args' ] . push ( tmp [ 'in_args' ] ) ;
13871401 results [ 'returned' ] . push ( tmp [ 'returned' ] ) ;
@@ -1443,15 +1457,27 @@ if (!String.prototype.endsWith) {
14431457 }
14441458 }
14451459
1446- function search ( e ) {
1460+ function getFilterCrates ( ) {
1461+ var elem = document . getElementById ( "crate-search" ) ;
1462+
1463+ if ( elem && elem . value !== "All crates" && rawSearchIndex . hasOwnProperty ( elem . value ) ) {
1464+ return elem . value ;
1465+ }
1466+ return undefined ;
1467+ }
1468+
1469+ function search ( e , forced ) {
14471470 var params = getQueryStringParams ( ) ;
14481471 var query = getQuery ( search_input . value . trim ( ) ) ;
14491472
14501473 if ( e ) {
14511474 e . preventDefault ( ) ;
14521475 }
14531476
1454- if ( query . query . length === 0 || query . id === currentResults ) {
1477+ if ( query . query . length === 0 ) {
1478+ return ;
1479+ }
1480+ if ( forced !== true && query . id === currentResults ) {
14551481 if ( query . query . length > 0 ) {
14561482 putBackSearch ( search_input ) ;
14571483 }
@@ -1471,7 +1497,8 @@ if (!String.prototype.endsWith) {
14711497 }
14721498 }
14731499
1474- showResults ( execSearch ( query , index ) ) ;
1500+ var filterCrates = getFilterCrates ( ) ;
1501+ showResults ( execSearch ( query , index , filterCrates ) , filterCrates ) ;
14751502 }
14761503
14771504 function buildIndex ( rawSearchIndex ) {
@@ -1571,6 +1598,13 @@ if (!String.prototype.endsWith) {
15711598 } ;
15721599 search_input . onpaste = search_input . onchange ;
15731600
1601+ var selectCrate = document . getElementById ( 'crate-search' ) ;
1602+ if ( selectCrate ) {
1603+ selectCrate . onchange = function ( ) {
1604+ search ( undefined , true ) ;
1605+ } ;
1606+ }
1607+
15741608 // Push and pop states are used to add search results to the browser
15751609 // history.
15761610 if ( browserSupportsHistoryApi ( ) ) {
@@ -2319,6 +2353,39 @@ if (!String.prototype.endsWith) {
23192353 if ( window . location . hash && window . location . hash . length > 0 ) {
23202354 expandSection ( window . location . hash . replace ( / ^ # / , '' ) ) ;
23212355 }
2356+
2357+ function addSearchOptions ( crates ) {
2358+ var elem = document . getElementById ( 'crate-search' ) ;
2359+
2360+ if ( ! elem ) {
2361+ return ;
2362+ }
2363+ var crates_text = [ ] ;
2364+ for ( var crate in crates ) {
2365+ if ( crates . hasOwnProperty ( crate ) ) {
2366+ crates_text . push ( crate ) ;
2367+ }
2368+ }
2369+ crates_text . sort ( function ( a , b ) {
2370+ var lower_a = a . toLowerCase ( ) ;
2371+ var lower_b = b . toLowerCase ( ) ;
2372+
2373+ if ( lower_a < lower_b ) {
2374+ return - 1 ;
2375+ } else if ( lower_a > lower_b ) {
2376+ return 1 ;
2377+ }
2378+ return 0 ;
2379+ } ) ;
2380+ for ( var i = 0 ; i < crates_text . length ; ++ i ) {
2381+ var option = document . createElement ( "option" ) ;
2382+ option . value = crates_text [ i ] ;
2383+ option . innerText = crates_text [ i ] ;
2384+ elem . appendChild ( option ) ;
2385+ }
2386+ }
2387+
2388+ window . addSearchOptions = addSearchOptions ;
23222389} ( ) ) ;
23232390
23242391// Sets the focus on the search bar at the top of the page
0 commit comments