@@ -56,13 +56,16 @@ const itemTypes = [
56
56
"derive" ,
57
57
"traitalias" , // 25
58
58
"generic" ,
59
+ "crate" ,
59
60
] ;
60
61
61
62
// used for special search precedence
62
63
const TY_PRIMITIVE = itemTypes . indexOf ( "primitive" ) ;
63
64
const TY_GENERIC = itemTypes . indexOf ( "generic" ) ;
64
65
const TY_IMPORT = itemTypes . indexOf ( "import" ) ;
65
66
const TY_TRAIT = itemTypes . indexOf ( "trait" ) ;
67
+ // minor hack to implement the `crate:` syntax
68
+ const TY_CRATE = itemTypes . indexOf ( "crate" ) ;
66
69
const ROOT_PATH = typeof window !== "undefined" ? window . rootPath : "../" ;
67
70
68
71
// Hard limit on how deep to recurse into generics when doing type-driven search.
@@ -291,6 +294,20 @@ function getFilteredNextElem(query, parserState, elems, isInGenerics) {
291
294
parserState . pos += 1 ;
292
295
parserState . totalElems -= 1 ;
293
296
query . literalSearch = false ;
297
+ if ( parserState . typeFilter === "crate" ) {
298
+ while ( parserState . userQuery [ parserState . pos ] === " " ) {
299
+ parserState . pos += 1 ;
300
+ }
301
+ const start = parserState . pos ;
302
+ const foundCrate = consumeIdent ( parserState ) ;
303
+ if ( ! foundCrate ) {
304
+ throw [ "Expected ident after " , "crate:" , ", found " , parserState . userQuery [ start ] ] ;
305
+ }
306
+ const name = parserState . userQuery . substring ( start , parserState . pos ) ;
307
+ elems . push ( makePrimitiveElement ( name , { typeFilter : "crate" } ) ) ;
308
+ parserState . typeFilter = null ;
309
+ return getFilteredNextElem ( query , parserState , elems , isInGenerics ) ;
310
+ }
294
311
getNextElem ( query , parserState , elems , isInGenerics ) ;
295
312
}
296
313
}
@@ -1870,6 +1887,7 @@ class DocSearch {
1870
1887
correction : null ,
1871
1888
proposeCorrectionFrom : null ,
1872
1889
proposeCorrectionTo : null ,
1890
+ filterCrates : null ,
1873
1891
// bloom filter build from type ids
1874
1892
typeFingerprint : new Uint32Array ( 4 ) ,
1875
1893
} ;
@@ -1996,6 +2014,20 @@ class DocSearch {
1996
2014
query . error = err ;
1997
2015
return query ;
1998
2016
}
2017
+
2018
+ function handleCrateFilters ( elem ) {
2019
+ if ( elem . typeFilter === TY_CRATE ) {
2020
+ query . filterCrates = elem . name ;
2021
+ return false ;
2022
+ }
2023
+ return true ;
2024
+
2025
+ }
2026
+ const nonCrateElems = query . elems . filter ( handleCrateFilters ) ;
2027
+ if ( nonCrateElems . length !== query . elems . length ) {
2028
+ query . elems = nonCrateElems ;
2029
+ }
2030
+
1999
2031
if ( ! query . literalSearch ) {
2000
2032
// If there is more than one element in the query, we switch to literalSearch in any
2001
2033
// case.
@@ -4282,7 +4314,7 @@ async function showResults(results, go_to_first, filterCrates) {
4282
4314
results . query = DocSearch . parseQuery ( searchState . input . value ) ;
4283
4315
}
4284
4316
4285
- currentResults = results . query . userQuery ;
4317
+ currentResults = results . query . original ;
4286
4318
4287
4319
// Navigate to the relevant tab if the current tab is empty, like in case users search
4288
4320
// for "-> String". If they had selected another tab previously, they have to click on
@@ -4417,7 +4449,7 @@ async function search(forced) {
4417
4449
const query = DocSearch . parseQuery ( searchState . input . value . trim ( ) ) ;
4418
4450
let filterCrates = getFilterCrates ( ) ;
4419
4451
4420
- if ( ! forced && query . userQuery === currentResults ) {
4452
+ if ( ! forced && query . original === currentResults ) {
4421
4453
if ( query . userQuery . length > 0 ) {
4422
4454
putBackSearch ( ) ;
4423
4455
}
@@ -4428,6 +4460,9 @@ async function search(forced) {
4428
4460
4429
4461
const params = searchState . getQueryStringParams ( ) ;
4430
4462
4463
+ if ( query . filterCrates !== null ) {
4464
+ filterCrates = query . filterCrates ;
4465
+ }
4431
4466
// In case we have no information about the saved crate and there is a URL query parameter,
4432
4467
// we override it with the URL query parameter.
4433
4468
if ( filterCrates === null && params [ "filter-crate" ] !== undefined ) {
0 commit comments