Skip to content

Commit f6a546e

Browse files
Be more flexible when looking for something by using levenshtein method
1 parent 6f21008 commit f6a546e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/librustdoc/html/static/main.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,30 @@
361361
}
362362

363363
function findArg(obj, val) {
364+
var lev_distance = MAX_LEV_DISTANCE + 1;
364365
if (obj && obj.type && obj.type.inputs.length > 0) {
365366
for (var i = 0; i < obj.type.inputs.length; i++) {
366367
if (obj.type.inputs[i].name === val) {
367-
return true;
368+
// No need to check anything else: we found it. Let's just move on.
369+
return 0;
370+
}
371+
var tmp = levenshtein(obj.type.inputs[i].name, val);
372+
if (tmp < lev_distance) {
373+
lev_distance = tmp;
368374
}
369375
}
370376
}
371-
return false;
377+
return lev_distance;
372378
}
373379

374380
function checkReturned(obj, val) {
375-
return obj && obj.type && obj.type.output &&
376-
obj.type.output.name.toLowerCase() === val;
381+
if (obj && obj.type && obj.type.output) {
382+
if (obj.type.output.name.toLowerCase() === val) {
383+
return 0;
384+
}
385+
return levenshtein(obj.type.output.name.toLowerCase(), val);
386+
}
387+
return MAX_LEV_DISTANCE + 1;
377388
}
378389

379390
function typePassesFilter(filter, type) {
@@ -489,8 +500,7 @@
489500
});
490501
}
491502
} else if (
492-
(lev_distance = levenshtein(searchWords[j], val)) <=
493-
MAX_LEV_DISTANCE) {
503+
(lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
494504
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
495505
results.push({
496506
id: j,
@@ -499,7 +509,8 @@
499509
lev: lev_distance,
500510
});
501511
}
502-
} else if (findArg(searchIndex[j], val)) {
512+
} else if (
513+
(lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) {
503514
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
504515
results.push({
505516
id: j,
@@ -508,7 +519,9 @@
508519
lev: lev_distance,
509520
});
510521
}
511-
} else if (checkReturned(searchIndex[j], val)) {
522+
} else if (
523+
(lev_distance = checkReturned(searchIndex[j], val)) <=
524+
MAX_LEV_DISTANCE) {
512525
if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
513526
results.push({
514527
id: j,

0 commit comments

Comments
 (0)