2020-02-10 03:58:12 +00:00
const compat = new Compat ( ) ;
2020-01-12 06:24:54 +00:00
const deminifier = new Deminifier ( mapping ) ;
2019-12-28 15:15:02 +00:00
const crateSearcher = new CrateSearch ( crateIndex ) ;
2020-02-03 08:18:23 +00:00
const attributeSearcher = new AttributeSearch ( ) ;
2020-02-17 03:37:59 +00:00
const bookSearcher = new BookSearch ( booksIndex ) ;
2020-02-09 15:15:18 +00:00
const command = new Command ( ) ;
2020-02-10 03:58:12 +00:00
const defaultSuggestion = ` Search ${ compat . match ( "std docs" ) } , ${ compat . match ( "crates" ) } (!), ${ compat . match ( "builtin attributes" ) } (#), ${ compat . match ( "error codes" ) } in your address bar instantly! ` ;
2020-02-16 09:20:34 +00:00
const omnibox = new Omnibox ( compat . browser , defaultSuggestion , compat . isChrome ? 8 : 6 ) ;
2019-12-28 15:15:02 +00:00
2020-02-09 13:25:21 +00:00
omnibox . bootstrap ( {
onSearch : ( query ) => {
return window . search ( query ) ;
} ,
onFormat : ( index , doc ) => {
2020-02-10 03:58:12 +00:00
let description = doc . displayPath + compat . match ( doc . name ) ;
2020-02-09 13:25:21 +00:00
if ( doc . desc ) {
2020-02-10 03:58:12 +00:00
description += " - " + compat . dim ( compat . escape ( doc . desc ) ) ;
2020-02-09 13:25:21 +00:00
}
return { content : doc . href , description } ;
} ,
onAppend : ( query ) => {
return [ {
content : ` ${ window . rootPath } std/index.html?search= ` + encodeURIComponent ( query ) ,
2020-02-10 03:58:12 +00:00
description : ` Search Rust docs ${ compat . match ( query ) } on ${ settings . isOfflineMode ? "offline mode" : "https://doc.rust-lang.org" } ` ,
2020-02-09 13:25:21 +00:00
} ]
} ,
} ) ;
omnibox . addPrefixQueryEvent ( "!" , {
defaultSearch : true ,
searchPriority : 1 ,
onSearch : ( query ) => {
this . docMode = query . startsWith ( "!!" ) ;
this . rawQuery = query . replace ( /[!\s]/g , "" ) ;
query = this . rawQuery . replace ( /[-_]*/ig , "" ) ;
return crateSearcher . search ( query ) ;
} ,
onFormat : ( index , crate ) => {
return {
// Dash and underscore is unequivalent on docs.rs right now.
// See issue https://github.com/rust-lang/docs.rs/issues/105
2020-02-09 15:15:18 +00:00
content : this . docMode ? ` https://docs.rs/ ${ crate . id . replace ( "_" , "-" ) } ` : ` https://crates.io/crates/ ${ crate . id } ` ,
2020-02-10 03:58:12 +00:00
description : ` ${ this . docMode ? "Docs" : "Crate" } : ${ compat . match ( crate . id ) } v ${ crate . version } - ${ compat . dim ( compat . escape ( crate . description ) ) } ` ,
2020-02-09 13:25:21 +00:00
} ;
} ,
onAppend : ( ) => {
return [ {
content : "https://crates.io/search?q=" + encodeURIComponent ( this . rawQuery ) ,
2020-02-10 03:58:12 +00:00
description : "Search Rust crates for " + compat . match ( this . rawQuery ) + " on https://crates.io" ,
2020-02-09 13:25:21 +00:00
} ] ;
}
} ) ;
omnibox . addPrefixQueryEvent ( "#" , {
defaultSearch : true ,
searchPriority : 2 ,
onSearch : ( query ) => {
return attributeSearcher . search ( query ) ;
} ,
onFormat : ( index , attribute ) => {
return {
2020-02-23 06:24:38 +00:00
content : attribute . href ,
2020-02-10 07:03:25 +00:00
description : ` Attribute: ${ compat . match ( "#[" + attribute . name + "]" ) } ${ compat . dim ( attribute . description ) } ` ,
2020-02-09 13:25:21 +00:00
}
}
} ) ;
omnibox . addRegexQueryEvent ( /e\d{2,4}$/i , {
onSearch : ( query ) => {
let baseIndex = parseInt ( query . slice ( 1 ) . padEnd ( 4 , '0' ) ) ;
let result = [ ] ;
for ( let index = 0 ; index < 10 ; index ++ ) {
let errorIndex = 'E' + String ( baseIndex ++ ) . padStart ( 4 , "0" ) . toUpperCase ( ) ;
result . push ( errorIndex ) ;
}
return result ;
} ,
onFormat : ( index , errorCode ) => {
return {
content : "https://doc.rust-lang.org/error-index.html#" + errorCode ,
2020-02-10 03:58:12 +00:00
description : ` Search Rust error index for ${ compat . match ( errorCode ) } on https://doc.rust-lang.org/error-index.html ` ,
} ;
2020-02-09 13:25:21 +00:00
}
} ) ;
2020-02-08 09:11:10 +00:00
2020-02-09 15:15:18 +00:00
omnibox . addPrefixQueryEvent ( ":" , {
onSearch : ( query ) => {
return command . execute ( query ) ;
} ,
} ) ;
2020-02-17 03:37:59 +00:00
omnibox . addPrefixQueryEvent ( "%" , {
onSearch : ( query ) => {
return bookSearcher . search ( query ) ;
} ,
2020-02-22 08:45:45 +00:00
onFormat : ( index , page ) => {
let parentTitles = page . parentTitles || [ ] ;
2020-02-17 03:37:59 +00:00
return {
2020-02-22 08:45:45 +00:00
content : page . url ,
description : ` ${ [ ... parentTitles , compat . match ( page . title ) ] . join ( " > " ) } - ${ compat . dim ( page . name ) } `
2020-02-17 03:37:59 +00:00
}
}
} ) ;
2020-02-08 09:11:10 +00:00
window . crateSearcher = crateSearcher ;
window . deminifier = deminifier ;