Skip to content

Commit dcd343b

Browse files
committed
Auto merge of #45617 - GuillaumeGomez:search-fixes, r=QuietMisdreavus
Search fixes Fixes #45608. r? @QuietMisdreavus
2 parents a6885cb + ee7e372 commit dcd343b

File tree

1 file changed

+112
-42
lines changed

1 file changed

+112
-42
lines changed

src/librustdoc/html/static/main.js

+112-42
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
var valLower = query.query.toLowerCase(),
350350
val = valLower,
351351
typeFilter = itemTypeFromName(query.type),
352-
results = [],
352+
results = {},
353353
split = valLower.split("::");
354354

355355
// remove empty keywords
@@ -360,15 +360,52 @@
360360
}
361361
}
362362

363+
function min(a, b) {
364+
if (a < b) {
365+
return a;
366+
}
367+
return b;
368+
}
369+
370+
function nbElements(obj) {
371+
var size = 0, key;
372+
for (key in obj) {
373+
if (obj.hasOwnProperty(key)) {
374+
size += 1;
375+
}
376+
}
377+
return size;
378+
}
379+
363380
function findArg(obj, val) {
381+
var lev_distance = MAX_LEV_DISTANCE + 1;
364382
if (obj && obj.type && obj.type.inputs.length > 0) {
365383
for (var i = 0; i < obj.type.inputs.length; i++) {
366384
if (obj.type.inputs[i].name === val) {
367-
return true;
385+
// No need to check anything else: we found it. Let's just move on.
386+
return 0;
387+
}
388+
lev_distance = min(levenshtein(obj.type.inputs[i].name, val), lev_distance);
389+
if (lev_distance === 0) {
390+
return 0;
368391
}
369392
}
370393
}
371-
return false;
394+
return lev_distance;
395+
}
396+
397+
function checkReturned(obj, val) {
398+
var lev_distance = MAX_LEV_DISTANCE + 1;
399+
if (obj && obj.type && obj.type.output) {
400+
if (obj.type.output.name.toLowerCase() === val) {
401+
return 0;
402+
}
403+
lev_distance = min(levenshtein(obj.type.output.name, val));
404+
if (lev_distance === 0) {
405+
return 0;
406+
}
407+
}
408+
return lev_distance;
372409
}
373410

374411
function typePassesFilter(filter, type) {
@@ -398,22 +435,27 @@
398435
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
399436
val.charAt(val.length - 1) === val.charAt(0))
400437
{
401-
val = val.substr(1, val.length - 2);
438+
val = val.substr(1, val.length - 2).toLowerCase();
402439
for (var i = 0; i < nSearchWords; ++i) {
440+
var ty = searchIndex[i];
403441
if (searchWords[i] === val) {
404442
// filter type: ... queries
405443
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
406-
results.push({id: i, index: -1});
444+
results[ty.path + ty.name] = {id: i, index: -1};
407445
}
408-
} else if (findArg(searchIndex[i], val.toLowerCase()) ||
409-
(searchIndex[i].type &&
410-
searchIndex[i].type.output &&
411-
searchIndex[i].type.output.name === val.toLowerCase())) {
446+
} else if (findArg(searchIndex[i], val) ||
447+
(ty.type &&
448+
ty.type.output &&
449+
ty.type.output.name === val)) {
412450
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
413-
results.push({id: i, index: -1, dontValidate: true});
451+
results[ty.path + ty.name] = {
452+
id: i,
453+
index: -1,
454+
dontValidate: true,
455+
};
414456
}
415457
}
416-
if (results.length === max) {
458+
if (nbElements(results) === max) {
417459
break;
418460
}
419461
}
@@ -431,6 +473,7 @@
431473

432474
for (var i = 0; i < nSearchWords; ++i) {
433475
var type = searchIndex[i].type;
476+
var ty = searchIndex[i];
434477
if (!type) {
435478
continue;
436479
}
@@ -444,7 +487,7 @@
444487
var typeOutput = type.output ? type.output.name : "";
445488
if (output === "*" || output == typeOutput) {
446489
if (input === "*") {
447-
results.push({id: i, index: -1, dontValidate: true});
490+
results[ty.path + ty.name] = {id: i, index: -1, dontValidate: true};
448491
} else {
449492
var allFound = true;
450493
for (var it = 0; allFound === true && it < inputs.length; it++) {
@@ -455,7 +498,11 @@
455498
allFound = found;
456499
}
457500
if (allFound === true) {
458-
results.push({id: i, index: -1, dontValidate: true});
501+
results[ty.path + ty.name] = {
502+
id: i,
503+
index: -1,
504+
dontValidate: true,
505+
};
459506
}
460507
}
461508
}
@@ -471,46 +518,77 @@
471518
for (var i = 0; i < split.length; ++i) {
472519
for (var j = 0; j < nSearchWords; ++j) {
473520
var lev_distance;
521+
var ty = searchIndex[j];
522+
if (!ty) {
523+
continue;
524+
}
474525
if (searchWords[j].indexOf(split[i]) > -1 ||
475526
searchWords[j].indexOf(val) > -1 ||
476527
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
477528
{
478529
// filter type: ... queries
479530
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
480-
results.push({
531+
results[ty.path + ty.name] = {
481532
id: j,
482533
index: searchWords[j].replace(/_/g, "").indexOf(val),
483534
lev: 0,
484-
});
535+
};
485536
}
486537
} else if (
487-
(lev_distance = levenshtein(searchWords[j], val)) <=
488-
MAX_LEV_DISTANCE) {
538+
(lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
489539
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
490-
results.push({
491-
id: j,
492-
index: 0,
493-
// we want lev results to go lower than others
494-
lev: lev_distance,
495-
});
540+
if (results[ty.path + ty.name] === undefined ||
541+
results[ty.path + ty.name].lev > lev_distance) {
542+
results[ty.path + ty.name] = {
543+
id: j,
544+
index: 0,
545+
// we want lev results to go lower than others
546+
lev: lev_distance,
547+
};
548+
}
496549
}
497-
} else if (findArg(searchIndex[j], val)) {
550+
} else if (
551+
(lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) {
498552
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
499-
results.push({
500-
id: j,
501-
index: 0,
502-
// we want lev results to go lower than others
503-
lev: lev_distance,
504-
});
553+
if (results[ty.path + ty.name] === undefined ||
554+
results[ty.path + ty.name].lev > lev_distance) {
555+
results[ty.path + ty.name] = {
556+
id: j,
557+
index: 0,
558+
// we want lev results to go lower than others
559+
lev: lev_distance,
560+
};
561+
}
562+
}
563+
} else if (
564+
(lev_distance = checkReturned(searchIndex[j], val)) <=
565+
MAX_LEV_DISTANCE) {
566+
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
567+
if (results[ty.path + ty.name] === undefined ||
568+
results[ty.path + ty.name].lev > lev_distance) {
569+
results[ty.path + ty.name] = {
570+
id: j,
571+
index: 0,
572+
// we want lev results to go lower than others
573+
lev: lev_distance,
574+
};
575+
}
505576
}
506577
}
507-
if (results.length === max) {
578+
if (nbElements(results) === max) {
508579
break;
509580
}
510581
}
511582
}
512583
}
513584

585+
var ar = [];
586+
for (var entry in results) {
587+
if (results.hasOwnProperty(entry)) {
588+
ar.push(results[entry]);
589+
}
590+
}
591+
results = ar;
514592
var nresults = results.length;
515593
for (var i = 0; i < nresults; ++i) {
516594
results[i].word = searchWords[results[i].id];
@@ -586,16 +664,6 @@
586664
return 0;
587665
});
588666

589-
// remove duplicates, according to the data provided
590-
for (var i = results.length - 1; i > 0; i -= 1) {
591-
if (results[i].word === results[i - 1].word &&
592-
results[i].item.ty === results[i - 1].item.ty &&
593-
results[i].item.path === results[i - 1].item.path &&
594-
(results[i].item.parent || {}).name === (results[i - 1].item.parent || {}).name)
595-
{
596-
results[i].id = -1;
597-
}
598-
}
599667
for (var i = 0; i < results.length; ++i) {
600668
var result = results[i],
601669
name = result.item.name.toLowerCase(),
@@ -884,6 +952,7 @@
884952
elems[0].onclick = function() { printTab(0); };
885953
elems[1].onclick = function() { printTab(1); };
886954
elems[2].onclick = function() { printTab(2); };
955+
printTab(currentTab);
887956
}
888957

889958
function search(e) {
@@ -951,7 +1020,8 @@
9511020
}
9521021
}
9531022
if (results['others'].length < maxResults &&
954-
((query.search && obj.name.indexOf(query.search)) || added === false)) {
1023+
((query.search && obj.name.indexOf(query.search) !== -1) ||
1024+
added === false)) {
9551025
results['others'].push(obj);
9561026
}
9571027
}

0 commit comments

Comments
 (0)