Skip to content

Commit ee7e372

Browse files
Remove duplicated results in the search
1 parent e8db5ad commit ee7e372

File tree

1 file changed

+88
-47
lines changed

1 file changed

+88
-47
lines changed

src/librustdoc/html/static/main.js

+88-47
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,6 +360,23 @@
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) {
364381
var lev_distance = MAX_LEV_DISTANCE + 1;
365382
if (obj && obj.type && obj.type.inputs.length > 0) {
@@ -368,23 +385,27 @@
368385
// No need to check anything else: we found it. Let's just move on.
369386
return 0;
370387
}
371-
var tmp = levenshtein(obj.type.inputs[i].name, val);
372-
if (tmp < lev_distance) {
373-
lev_distance = tmp;
388+
lev_distance = min(levenshtein(obj.type.inputs[i].name, val), lev_distance);
389+
if (lev_distance === 0) {
390+
return 0;
374391
}
375392
}
376393
}
377394
return lev_distance;
378395
}
379396

380397
function checkReturned(obj, val) {
398+
var lev_distance = MAX_LEV_DISTANCE + 1;
381399
if (obj && obj.type && obj.type.output) {
382400
if (obj.type.output.name.toLowerCase() === val) {
383401
return 0;
384402
}
385-
return levenshtein(obj.type.output.name.toLowerCase(), val);
403+
lev_distance = min(levenshtein(obj.type.output.name, val));
404+
if (lev_distance === 0) {
405+
return 0;
406+
}
386407
}
387-
return MAX_LEV_DISTANCE + 1;
408+
return lev_distance;
388409
}
389410

390411
function typePassesFilter(filter, type) {
@@ -414,22 +435,27 @@
414435
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
415436
val.charAt(val.length - 1) === val.charAt(0))
416437
{
417-
val = val.substr(1, val.length - 2);
438+
val = val.substr(1, val.length - 2).toLowerCase();
418439
for (var i = 0; i < nSearchWords; ++i) {
440+
var ty = searchIndex[i];
419441
if (searchWords[i] === val) {
420442
// filter type: ... queries
421443
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
422-
results.push({id: i, index: -1});
444+
results[ty.path + ty.name] = {id: i, index: -1};
423445
}
424-
} else if (findArg(searchIndex[i], val.toLowerCase()) ||
425-
(searchIndex[i].type &&
426-
searchIndex[i].type.output &&
427-
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)) {
428450
if (typePassesFilter(typeFilter, searchIndex[i].ty)) {
429-
results.push({id: i, index: -1, dontValidate: true});
451+
results[ty.path + ty.name] = {
452+
id: i,
453+
index: -1,
454+
dontValidate: true,
455+
};
430456
}
431457
}
432-
if (results.length === max) {
458+
if (nbElements(results) === max) {
433459
break;
434460
}
435461
}
@@ -447,6 +473,7 @@
447473

448474
for (var i = 0; i < nSearchWords; ++i) {
449475
var type = searchIndex[i].type;
476+
var ty = searchIndex[i];
450477
if (!type) {
451478
continue;
452479
}
@@ -460,7 +487,7 @@
460487
var typeOutput = type.output ? type.output.name : "";
461488
if (output === "*" || output == typeOutput) {
462489
if (input === "*") {
463-
results.push({id: i, index: -1, dontValidate: true});
490+
results[ty.path + ty.name] = {id: i, index: -1, dontValidate: true};
464491
} else {
465492
var allFound = true;
466493
for (var it = 0; allFound === true && it < inputs.length; it++) {
@@ -471,7 +498,11 @@
471498
allFound = found;
472499
}
473500
if (allFound === true) {
474-
results.push({id: i, index: -1, dontValidate: true});
501+
results[ty.path + ty.name] = {
502+
id: i,
503+
index: -1,
504+
dontValidate: true,
505+
};
475506
}
476507
}
477508
}
@@ -487,57 +518,77 @@
487518
for (var i = 0; i < split.length; ++i) {
488519
for (var j = 0; j < nSearchWords; ++j) {
489520
var lev_distance;
521+
var ty = searchIndex[j];
522+
if (!ty) {
523+
continue;
524+
}
490525
if (searchWords[j].indexOf(split[i]) > -1 ||
491526
searchWords[j].indexOf(val) > -1 ||
492527
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
493528
{
494529
// filter type: ... queries
495530
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
496-
results.push({
531+
results[ty.path + ty.name] = {
497532
id: j,
498533
index: searchWords[j].replace(/_/g, "").indexOf(val),
499534
lev: 0,
500-
});
535+
};
501536
}
502537
} else if (
503538
(lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
504539
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
505-
results.push({
506-
id: j,
507-
index: 0,
508-
// we want lev results to go lower than others
509-
lev: lev_distance,
510-
});
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+
}
511549
}
512550
} else if (
513551
(lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) {
514552
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
515-
results.push({
516-
id: j,
517-
index: 0,
518-
// we want lev results to go lower than others
519-
lev: lev_distance,
520-
});
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+
}
521562
}
522563
} else if (
523564
(lev_distance = checkReturned(searchIndex[j], val)) <=
524565
MAX_LEV_DISTANCE) {
525566
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
526-
results.push({
527-
id: j,
528-
index: 0,
529-
// we want lev results to go lower than others
530-
lev: lev_distance,
531-
});
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+
}
532576
}
533577
}
534-
if (results.length === max) {
578+
if (nbElements(results) === max) {
535579
break;
536580
}
537581
}
538582
}
539583
}
540584

585+
var ar = [];
586+
for (var entry in results) {
587+
if (results.hasOwnProperty(entry)) {
588+
ar.push(results[entry]);
589+
}
590+
}
591+
results = ar;
541592
var nresults = results.length;
542593
for (var i = 0; i < nresults; ++i) {
543594
results[i].word = searchWords[results[i].id];
@@ -613,16 +664,6 @@
613664
return 0;
614665
});
615666

616-
// remove duplicates, according to the data provided
617-
for (var i = results.length - 1; i > 0; i -= 1) {
618-
if (results[i].word === results[i - 1].word &&
619-
results[i].item.ty === results[i - 1].item.ty &&
620-
results[i].item.path === results[i - 1].item.path &&
621-
(results[i].item.parent || {}).name === (results[i - 1].item.parent || {}).name)
622-
{
623-
results[i].id = -1;
624-
}
625-
}
626667
for (var i = 0; i < results.length; ++i) {
627668
var result = results[i],
628669
name = result.item.name.toLowerCase(),

0 commit comments

Comments
 (0)