2020-02-24 06:18:03 +00:00
const c = 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-28 04:53:52 +00:00
const commandManager = new CommandManager ( ) ;
2020-02-10 03:58:12 +00:00
2020-02-26 10:13:21 +00:00
const defaultSuggestion = ` Search std ${ c . match ( "docs" ) } , ${ c . match ( "crates" ) } (!), builtin ${ c . match ( "attributes" ) } (#), official ${ c . match ( "books" ) } (%), and ${ c . match ( "error codes" ) } , etc in your address bar instantly! ` ;
2020-02-24 06:18:03 +00:00
const omnibox = new Omnibox ( c . browser , defaultSuggestion , c . 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-24 06:18:03 +00:00
let description = doc . displayPath + c . match ( doc . name ) ;
2020-02-09 13:25:21 +00:00
if ( doc . desc ) {
2020-02-24 06:18:03 +00:00
description += " - " + c . dim ( c . 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-24 06:18:03 +00:00
description : ` Search Rust docs ${ c . match ( query ) } on ${ settings . isOfflineMode ? "offline mode" : "https://doc.rust-lang.org" } ` ,
2020-02-09 13:25:21 +00:00
} ]
} ,
2020-02-28 04:53:52 +00:00
onSelected : ( query , result ) => {
HistoryCommand . record ( query , result ) ;
}
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-24 06:18:03 +00:00
description : ` ${ this . docMode ? "Docs" : "Crate" } : ${ c . match ( crate . id ) } v ${ crate . version } - ${ c . dim ( c . escape ( crate . description ) ) } ` ,
2020-02-09 13:25:21 +00:00
} ;
} ,
onAppend : ( ) => {
return [ {
content : "https://crates.io/search?q=" + encodeURIComponent ( this . rawQuery ) ,
2020-02-24 06:18:03 +00:00
description : "Search Rust crates for " + c . match ( this . rawQuery ) + " on https://crates.io" ,
2020-02-26 10:13:21 +00:00
} , {
content : "remind" ,
description : ` Remind: ${ c . dim ( "We only indexed the top 20K crates. Sorry for the inconvenience if your desired crate not show." ) } ` ,
2020-02-09 13:25:21 +00:00
} ] ;
}
} ) ;
omnibox . addPrefixQueryEvent ( "#" , {
defaultSearch : true ,
searchPriority : 2 ,
2020-02-23 13:00:55 +00:00
deduplicate : true ,
2020-02-09 13:25:21 +00:00
onSearch : ( query ) => {
return attributeSearcher . search ( query ) ;
} ,
onFormat : ( index , attribute ) => {
return {
2020-02-23 06:24:38 +00:00
content : attribute . href ,
2020-02-24 06:18:03 +00:00
description : ` Attribute: ${ c . match ( "#[" + attribute . name + "]" ) } ${ c . 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-24 06:18:03 +00:00
description : ` Search Rust error index for ${ c . match ( errorCode ) } on https://doc.rust-lang.org/error-index.html ` ,
2020-02-10 03:58:12 +00:00
} ;
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 ) => {
2020-02-28 04:53:52 +00:00
return commandManager . execute ( query ) ;
2020-02-09 15:15:18 +00:00
} ,
} ) ;
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 ,
2020-02-26 10:13:21 +00:00
description : ` ${ [ ... parentTitles . map ( t => c . escape ( t ) ) , c . match ( c . escape ( page . title ) ) ] . join ( " > " ) } - ${ c . 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 ;