@@ -113,7 +113,15 @@ window.initSearch = function(rawSearchIndex) {
113
113
var INPUTS_DATA = 0 ;
114
114
var OUTPUT_DATA = 1 ;
115
115
var NO_TYPE_FILTER = - 1 ;
116
- var currentResults , index , searchIndex ;
116
+ /**
117
+ * @type {Array<Row> }
118
+ */
119
+ var searchIndex ;
120
+ /**
121
+ * @type {Array<string> }
122
+ */
123
+ var searchWords ;
124
+ var currentResults ;
117
125
var ALIASES = { } ;
118
126
var params = searchState . getQueryStringParams ( ) ;
119
127
@@ -126,12 +134,15 @@ window.initSearch = function(rawSearchIndex) {
126
134
}
127
135
128
136
/**
129
- * Executes the query and builds an index of results
130
- * @param {[Object] } query [The user query]
131
- * @param {[type] } searchWords [The list of search words to query
132
- * against]
133
- * @param {[type] } filterCrates [Crate to search in if defined]
134
- * @return {[type] } [A search index of results]
137
+ * Executes the query and returns a list of results for each results tab.
138
+ * @param {Object } query - The user query
139
+ * @param {Array<string> } searchWords - The list of search words to query against
140
+ * @param {string } [filterCrates] - Crate to search in
141
+ * @return {{
142
+ * in_args: Array<?>,
143
+ * returned: Array<?>,
144
+ * others: Array<?>
145
+ * }}
135
146
*/
136
147
function execQuery ( query , searchWords , filterCrates ) {
137
148
function itemTypeFromName ( typename ) {
@@ -847,11 +858,11 @@ window.initSearch = function(rawSearchIndex) {
847
858
* This could be written functionally, but I wanted to minimise
848
859
* functions on stack.
849
860
*
850
- * @param {[ string] } name [ The name of the result]
851
- * @param {[ string] } path [ The path of the result]
852
- * @param {[ string] } keys [ The keys to be used (["file", "open"])]
853
- * @param {[object] } parent [ The parent of the result]
854
- * @return {boolean } [ Whether the result is valid or not]
861
+ * @param {string } name - The name of the result
862
+ * @param {string } path - The path of the result
863
+ * @param {string } keys - The keys to be used (["file", "open"])
864
+ * @param {Object } parent - The parent of the result
865
+ * @return {boolean } - Whether the result is valid or not
855
866
*/
856
867
function validateResult ( name , path , keys , parent ) {
857
868
for ( var i = 0 , len = keys . length ; i < len ; ++ i ) {
@@ -872,8 +883,14 @@ window.initSearch = function(rawSearchIndex) {
872
883
return true ;
873
884
}
874
885
886
+ /**
887
+ * Parse a string into a query object.
888
+ *
889
+ * @param {string } raw - The text that the user typed.
890
+ * @returns {ParsedQuery }
891
+ */
875
892
function getQuery ( raw ) {
876
- var matches , type , query ;
893
+ var matches , type = "" , query ;
877
894
query = raw ;
878
895
879
896
matches = query . match ( / ^ ( f n | m o d | s t r u c t | e n u m | t r a i t | t y p e | c o n s t | m a c r o ) \s * : \s * / i) ;
@@ -974,6 +991,12 @@ window.initSearch = function(rawSearchIndex) {
974
991
return tmp ;
975
992
}
976
993
994
+ /**
995
+ * Render a set of search results for a single tab.
996
+ * @param {Array<?> } array - The search results for this tab
997
+ * @param {ParsedQuery } query
998
+ * @param {boolean } display - True if this is the active tab
999
+ */
977
1000
function addTab ( array , query , display ) {
978
1001
var extraClass = "" ;
979
1002
if ( display === true ) {
@@ -1083,7 +1106,7 @@ window.initSearch = function(rawSearchIndex) {
1083
1106
1084
1107
currentResults = query . id ;
1085
1108
1086
- var ret_others = addTab ( results . others , query ) ;
1109
+ var ret_others = addTab ( results . others , query , true ) ;
1087
1110
var ret_in_args = addTab ( results . in_args , query , false ) ;
1088
1111
var ret_returned = addTab ( results . returned , query , false ) ;
1089
1112
@@ -1253,6 +1276,12 @@ window.initSearch = function(rawSearchIndex) {
1253
1276
return undefined ;
1254
1277
}
1255
1278
1279
+ /**
1280
+ * Perform a search based on the current state of the search input element
1281
+ * and display the results.
1282
+ * @param {Event } [e] - The event that triggered this search, if any
1283
+ * @param {boolean } [forced]
1284
+ */
1256
1285
function search ( e , forced ) {
1257
1286
var params = searchState . getQueryStringParams ( ) ;
1258
1287
var query = getQuery ( searchState . input . value . trim ( ) ) ;
@@ -1287,11 +1316,14 @@ window.initSearch = function(rawSearchIndex) {
1287
1316
}
1288
1317
1289
1318
var filterCrates = getFilterCrates ( ) ;
1290
- showResults ( execSearch ( query , index , filterCrates ) , params . go_to_first ) ;
1319
+ showResults ( execSearch ( query , searchWords , filterCrates ) , params [ " go_to_first" ] ) ;
1291
1320
}
1292
1321
1293
1322
function buildIndex ( rawSearchIndex ) {
1294
1323
searchIndex = [ ] ;
1324
+ /**
1325
+ * @type {Array<string> }
1326
+ */
1295
1327
var searchWords = [ ] ;
1296
1328
var i , word ;
1297
1329
var currentIndex = 0 ;
@@ -1304,6 +1336,38 @@ window.initSearch = function(rawSearchIndex) {
1304
1336
1305
1337
var crateSize = 0 ;
1306
1338
1339
+ /**
1340
+ * The raw search data for a given crate. `n`, `t`, `d`, and `q`, `i`, and `f`
1341
+ * are arrays with the same length. n[i] contains the name of an item.
1342
+ * t[i] contains the type of that item (as a small integer that represents an
1343
+ * offset in `itemTypes`). d[i] contains the description of that item.
1344
+ *
1345
+ * q[i] contains the full path of the item, or an empty string indicating
1346
+ * "same as q[i-1]".
1347
+ *
1348
+ * i[i], f[i] are a mystery.
1349
+ *
1350
+ * `a` defines aliases with an Array of pairs: [name, offset], where `offset`
1351
+ * points into the n/t/d/q/i/f arrays.
1352
+ *
1353
+ * `doc` contains the description of the crate.
1354
+ *
1355
+ * `p` is a mystery and isn't the same length as n/t/d/q/i/f.
1356
+ *
1357
+ * @type {{
1358
+ * doc: string,
1359
+ * a: Object,
1360
+ * n: Array<string>,
1361
+ * t: Array<Number>,
1362
+ * d: Array<string>,
1363
+ * q: Array<string>,
1364
+ * i: Array<Number>,
1365
+ * f: Array<Array<?>>,
1366
+ * p: Array<Object>,
1367
+ * }}
1368
+ */
1369
+ var crateCorpus = rawSearchIndex [ crate ] ;
1370
+
1307
1371
searchWords . push ( crate ) ;
1308
1372
// This object should have exactly the same set of fields as the "row"
1309
1373
// object defined below. Your JavaScript runtime will thank you.
@@ -1313,7 +1377,7 @@ window.initSearch = function(rawSearchIndex) {
1313
1377
ty : 1 , // == ExternCrate
1314
1378
name : crate ,
1315
1379
path : "" ,
1316
- desc : rawSearchIndex [ crate ] . doc ,
1380
+ desc : crateCorpus . doc ,
1317
1381
parent : undefined ,
1318
1382
type : null ,
1319
1383
id : id ,
@@ -1324,23 +1388,23 @@ window.initSearch = function(rawSearchIndex) {
1324
1388
currentIndex += 1 ;
1325
1389
1326
1390
// an array of (Number) item types
1327
- var itemTypes = rawSearchIndex [ crate ] . t ;
1391
+ var itemTypes = crateCorpus . t ;
1328
1392
// an array of (String) item names
1329
- var itemNames = rawSearchIndex [ crate ] . n ;
1393
+ var itemNames = crateCorpus . n ;
1330
1394
// an array of (String) full paths (or empty string for previous path)
1331
- var itemPaths = rawSearchIndex [ crate ] . q ;
1395
+ var itemPaths = crateCorpus . q ;
1332
1396
// an array of (String) descriptions
1333
- var itemDescs = rawSearchIndex [ crate ] . d ;
1397
+ var itemDescs = crateCorpus . d ;
1334
1398
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
1335
- var itemParentIdxs = rawSearchIndex [ crate ] . i ;
1399
+ var itemParentIdxs = crateCorpus . i ;
1336
1400
// an array of (Object | null) the type of the function, if any
1337
- var itemFunctionSearchTypes = rawSearchIndex [ crate ] . f ;
1401
+ var itemFunctionSearchTypes = crateCorpus . f ;
1338
1402
// an array of [(Number) item type,
1339
1403
// (String) name]
1340
- var paths = rawSearchIndex [ crate ] . p ;
1404
+ var paths = crateCorpus . p ;
1341
1405
// an array of [(String) alias name
1342
1406
// [Number] index to items]
1343
- var aliases = rawSearchIndex [ crate ] . a ;
1407
+ var aliases = crateCorpus . a ;
1344
1408
1345
1409
// convert `rawPaths` entries into object form
1346
1410
var len = paths . length ;
@@ -1406,6 +1470,16 @@ window.initSearch = function(rawSearchIndex) {
1406
1470
return searchWords ;
1407
1471
}
1408
1472
1473
+ /**
1474
+ * Callback for when the search form is submitted.
1475
+ * @param {Event } [e] - The event that triggered this call, if any
1476
+ */
1477
+ function onSearchSubmit ( e ) {
1478
+ e . preventDefault ( ) ;
1479
+ searchState . clearInputTimeout ( ) ;
1480
+ search ( ) ;
1481
+ }
1482
+
1409
1483
function registerSearchEvents ( ) {
1410
1484
var searchAfter500ms = function ( ) {
1411
1485
searchState . clearInputTimeout ( ) ;
@@ -1421,11 +1495,7 @@ window.initSearch = function(rawSearchIndex) {
1421
1495
} ;
1422
1496
searchState . input . onkeyup = searchAfter500ms ;
1423
1497
searchState . input . oninput = searchAfter500ms ;
1424
- document . getElementsByClassName ( "search-form" ) [ 0 ] . onsubmit = function ( e ) {
1425
- e . preventDefault ( ) ;
1426
- searchState . clearInputTimeout ( ) ;
1427
- search ( ) ;
1428
- } ;
1498
+ document . getElementsByClassName ( "search-form" ) [ 0 ] . onsubmit = onSearchSubmit ;
1429
1499
searchState . input . onchange = function ( e ) {
1430
1500
if ( e . target !== document . activeElement ) {
1431
1501
// To prevent doing anything when it's from a blur event.
@@ -1546,7 +1616,7 @@ window.initSearch = function(rawSearchIndex) {
1546
1616
} ;
1547
1617
}
1548
1618
1549
- index = buildIndex ( rawSearchIndex ) ;
1619
+ searchWords = buildIndex ( rawSearchIndex ) ;
1550
1620
registerSearchEvents ( ) ;
1551
1621
// If there's a search term in the URL, execute the search now.
1552
1622
if ( searchState . getQueryStringParams ( ) . search ) {
0 commit comments