@@ -218,12 +218,14 @@ if (!String.prototype.endsWith) {
218218 //
219219 // So I guess you could say things are getting pretty interoperable.
220220 function getVirtualKey ( ev ) {
221- if ( "key" in ev && typeof ev . key != "undefined" )
221+ if ( "key" in ev && typeof ev . key != "undefined" ) {
222222 return ev . key ;
223+ }
223224
224225 var c = ev . charCode || ev . keyCode ;
225- if ( c == 27 )
226+ if ( c == 27 ) {
226227 return "Escape" ;
228+ }
227229 return String . fromCharCode ( c ) ;
228230 }
229231
@@ -431,12 +433,13 @@ if (!String.prototype.endsWith) {
431433
432434 /**
433435 * Executes the query and builds an index of results
434- * @param {[Object] } query [The user query]
435- * @param {[type] } searchWords [The list of search words to query
436- * against]
437- * @return {[type] } [A search index of results]
436+ * @param {[Object] } query [The user query]
437+ * @param {[type] } searchWords [The list of search words to query
438+ * against]
439+ * @param {[type] } filterCrates [Crate to search in if defined]
440+ * @return {[type] } [A search index of results]
438441 */
439- function execQuery ( query , searchWords ) {
442+ function execQuery ( query , searchWords , filterCrates ) {
440443 function itemTypeFromName ( typename ) {
441444 for ( var i = 0 ; i < itemTypes . length ; ++ i ) {
442445 if ( itemTypes [ i ] === typename ) {
@@ -812,6 +815,9 @@ if (!String.prototype.endsWith) {
812815 {
813816 val = extractGenerics ( val . substr ( 1 , val . length - 2 ) ) ;
814817 for ( var i = 0 ; i < nSearchWords ; ++ i ) {
818+ if ( filterCrates !== undefined && searchIndex [ i ] . crate !== filterCrates ) {
819+ continue ;
820+ }
815821 var in_args = findArg ( searchIndex [ i ] , val , true ) ;
816822 var returned = checkReturned ( searchIndex [ i ] , val , true ) ;
817823 var ty = searchIndex [ i ] ;
@@ -866,6 +872,9 @@ if (!String.prototype.endsWith) {
866872 var output = extractGenerics ( parts [ 1 ] ) ;
867873
868874 for ( var i = 0 ; i < nSearchWords ; ++ i ) {
875+ if ( filterCrates !== undefined && searchIndex [ i ] . crate !== filterCrates ) {
876+ continue ;
877+ }
869878 var type = searchIndex [ i ] . type ;
870879 var ty = searchIndex [ i ] ;
871880 if ( ! type ) {
@@ -937,11 +946,11 @@ if (!String.prototype.endsWith) {
937946 var contains = paths . slice ( 0 , paths . length > 1 ? paths . length - 1 : 1 ) ;
938947
939948 for ( j = 0 ; j < nSearchWords ; ++ j ) {
940- var lev_distance ;
941949 var ty = searchIndex [ j ] ;
942- if ( ! ty ) {
950+ if ( ! ty || ( filterCrates !== undefined && ty . crate !== filterCrates ) ) {
943951 continue ;
944952 }
953+ var lev_distance ;
945954 var lev_add = 0 ;
946955 if ( paths . length > 1 ) {
947956 var lev = checkPath ( contains , paths [ paths . length - 1 ] , ty ) ;
@@ -1326,7 +1335,7 @@ if (!String.prototype.endsWith) {
13261335 return '<div>' + text + ' <div class="count">(' + nbElems + ')</div></div>' ;
13271336 }
13281337
1329- function showResults ( results ) {
1338+ function showResults ( results , filterCrates ) {
13301339 if ( results [ 'others' ] . length === 1 &&
13311340 getCurrentValue ( 'rustdoc-go-to-only-result' ) === "true" ) {
13321341 var elem = document . createElement ( 'a' ) ;
@@ -1344,8 +1353,13 @@ if (!String.prototype.endsWith) {
13441353 var ret_in_args = addTab ( results [ 'in_args' ] , query , false ) ;
13451354 var ret_returned = addTab ( results [ 'returned' ] , query , false ) ;
13461355
1356+ var filter = "" ;
1357+ if ( filterCrates !== undefined ) {
1358+ filter = " (in <b>" + filterCrates + "</b> crate)" ;
1359+ }
1360+
13471361 var output = '<h1>Results for ' + escape ( query . query ) +
1348- ( query . type ? ' (type: ' + escape ( query . type ) + ')' : '' ) + '</h1>' +
1362+ ( query . type ? ' (type: ' + escape ( query . type ) + ')' : '' ) + filter + '</h1>' +
13491363 '<div id="titles">' +
13501364 makeTabHeader ( 0 , "In Names" , ret_others [ 1 ] ) +
13511365 makeTabHeader ( 1 , "In Parameters" , ret_in_args [ 1 ] ) +
@@ -1374,7 +1388,7 @@ if (!String.prototype.endsWith) {
13741388 printTab ( currentTab ) ;
13751389 }
13761390
1377- function execSearch ( query , searchWords ) {
1391+ function execSearch ( query , searchWords , filterCrates ) {
13781392 var queries = query . raw . split ( "," ) ;
13791393 var results = {
13801394 'in_args' : [ ] ,
@@ -1385,7 +1399,7 @@ if (!String.prototype.endsWith) {
13851399 for ( var i = 0 ; i < queries . length ; ++ i ) {
13861400 var query = queries [ i ] . trim ( ) ;
13871401 if ( query . length !== 0 ) {
1388- var tmp = execQuery ( getQuery ( query ) , searchWords ) ;
1402+ var tmp = execQuery ( getQuery ( query ) , searchWords , filterCrates ) ;
13891403
13901404 results [ 'in_args' ] . push ( tmp [ 'in_args' ] ) ;
13911405 results [ 'returned' ] . push ( tmp [ 'returned' ] ) ;
@@ -1447,15 +1461,27 @@ if (!String.prototype.endsWith) {
14471461 }
14481462 }
14491463
1450- function search ( e ) {
1464+ function getFilterCrates ( ) {
1465+ var elem = document . getElementById ( "crate-search" ) ;
1466+
1467+ if ( elem && elem . value !== "All crates" && rawSearchIndex . hasOwnProperty ( elem . value ) ) {
1468+ return elem . value ;
1469+ }
1470+ return undefined ;
1471+ }
1472+
1473+ function search ( e , forced ) {
14511474 var params = getQueryStringParams ( ) ;
14521475 var query = getQuery ( search_input . value . trim ( ) ) ;
14531476
14541477 if ( e ) {
14551478 e . preventDefault ( ) ;
14561479 }
14571480
1458- if ( query . query . length === 0 || query . id === currentResults ) {
1481+ if ( query . query . length === 0 ) {
1482+ return ;
1483+ }
1484+ if ( forced !== true && query . id === currentResults ) {
14591485 if ( query . query . length > 0 ) {
14601486 putBackSearch ( search_input ) ;
14611487 }
@@ -1475,7 +1501,8 @@ if (!String.prototype.endsWith) {
14751501 }
14761502 }
14771503
1478- showResults ( execSearch ( query , index ) ) ;
1504+ var filterCrates = getFilterCrates ( ) ;
1505+ showResults ( execSearch ( query , index , filterCrates ) , filterCrates ) ;
14791506 }
14801507
14811508 function buildIndex ( rawSearchIndex ) {
@@ -1575,6 +1602,13 @@ if (!String.prototype.endsWith) {
15751602 } ;
15761603 search_input . onpaste = search_input . onchange ;
15771604
1605+ var selectCrate = document . getElementById ( 'crate-search' ) ;
1606+ if ( selectCrate ) {
1607+ selectCrate . onchange = function ( ) {
1608+ search ( undefined , true ) ;
1609+ } ;
1610+ }
1611+
15781612 // Push and pop states are used to add search results to the browser
15791613 // history.
15801614 if ( browserSupportsHistoryApi ( ) ) {
@@ -2323,6 +2357,39 @@ if (!String.prototype.endsWith) {
23232357 if ( window . location . hash && window . location . hash . length > 0 ) {
23242358 expandSection ( window . location . hash . replace ( / ^ # / , '' ) ) ;
23252359 }
2360+
2361+ function addSearchOptions ( crates ) {
2362+ var elem = document . getElementById ( 'crate-search' ) ;
2363+
2364+ if ( ! elem ) {
2365+ return ;
2366+ }
2367+ var crates_text = [ ] ;
2368+ for ( var crate in crates ) {
2369+ if ( crates . hasOwnProperty ( crate ) ) {
2370+ crates_text . push ( crate ) ;
2371+ }
2372+ }
2373+ crates_text . sort ( function ( a , b ) {
2374+ var lower_a = a . toLowerCase ( ) ;
2375+ var lower_b = b . toLowerCase ( ) ;
2376+
2377+ if ( lower_a < lower_b ) {
2378+ return - 1 ;
2379+ } else if ( lower_a > lower_b ) {
2380+ return 1 ;
2381+ }
2382+ return 0 ;
2383+ } ) ;
2384+ for ( var i = 0 ; i < crates_text . length ; ++ i ) {
2385+ var option = document . createElement ( "option" ) ;
2386+ option . value = crates_text [ i ] ;
2387+ option . innerText = crates_text [ i ] ;
2388+ elem . appendChild ( option ) ;
2389+ }
2390+ }
2391+
2392+ window . addSearchOptions = addSearchOptions ;
23262393} ( ) ) ;
23272394
23282395// Sets the focus on the search bar at the top of the page
0 commit comments