Skip to content

Commit ad6324f

Browse files
Fix path search in docs
1 parent 18250b0 commit ad6324f

File tree

1 file changed

+114
-82
lines changed

1 file changed

+114
-82
lines changed

src/librustdoc/html/static/main.js

+114-82
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,10 @@
374374
results = {},
375375
split = valLower.split("::");
376376

377-
// remove empty keywords
378-
for (var j = 0; j < split.length; ++j) {
379-
split[j].toLowerCase();
380-
if (split[j] === "") {
381-
split.splice(j, 1);
377+
for (var z = 0; z < split.length; ++z) {
378+
if (split[z] === "") {
379+
split.slice(z, 1);
380+
z -= 1;
382381
}
383382
}
384383

@@ -405,9 +404,7 @@
405404
if (obj.generics &&
406405
obj.generics.length >= val.generics.length) {
407406
var elems = obj.generics.slice(0);
408-
for (var y = 0;
409-
y < val.generics.length;
410-
++y) {
407+
for (var y = 0; y < val.generics.length; ++y) {
411408
// The point here is to find the type that matches the most.
412409
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
413410
for (var x = 0; x < elems.length; ++x) {
@@ -529,6 +526,22 @@
529526
return literalSearch === true ? false : lev_distance;
530527
}
531528

529+
function checkPath(startsWith, ty) {
530+
var lev_total = 0;
531+
var path = ty.path.split("::");
532+
if (startsWith.length > path.length) {
533+
return MAX_LEV_DISTANCE + 1;
534+
}
535+
for (var i = path.length - 1; i < startsWith.length; ++i) {
536+
var lev = levenshtein(startsWith[i], path[i]);
537+
if (lev > MAX_LEV_DISTANCE) {
538+
return MAX_LEV_DISTANCE + 1;
539+
}
540+
lev_total += lev;
541+
}
542+
return Math.round(lev_total / startsWith.length);
543+
}
544+
532545
function typePassesFilter(filter, type) {
533546
// No filter
534547
if (filter < 0) return true;
@@ -665,85 +678,106 @@
665678
query.search = val;
666679
// gather matching search results up to a certain maximum
667680
val = val.replace(/\_/g, "");
668-
var valGenerics = extractGenerics(val);
681+
669682
var results_length = 0;
670-
for (var i = 0; i < split.length; ++i) {
671-
for (var j = 0; j < nSearchWords; ++j) {
672-
var lev_distance;
673-
var ty = searchIndex[j];
674-
if (!ty) {
683+
var valGenerics = extractGenerics(val);
684+
685+
var paths = valLower.split("::");
686+
var j;
687+
for (j = 0; j < paths.length; ++j) {
688+
if (paths[j] === "") {
689+
paths.splice(j, 1);
690+
j -= 1;
691+
}
692+
}
693+
val = paths[paths.length - 1];
694+
var startsWith = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
695+
696+
for (j = 0; j < nSearchWords; ++j) {
697+
var lev_distance;
698+
var ty = searchIndex[j];
699+
if (!ty) {
700+
continue;
701+
}
702+
var lev_add = 0;
703+
if (paths.length > 1) {
704+
var lev = checkPath(startsWith, ty);
705+
if (lev > MAX_LEV_DISTANCE) {
675706
continue;
707+
} else if (lev > 0) {
708+
lev_add = 1;
676709
}
677-
var returned = false;
678-
var in_args = false;
679-
var index = -1;
680-
// we want lev results to go lower than others
681-
var lev = MAX_LEV_DISTANCE;
682-
var fullId = itemTypes[ty.ty] + ty.path + ty.name;
683-
684-
if (searchWords[j].indexOf(split[i]) > -1 ||
685-
searchWords[j].indexOf(val) > -1 ||
686-
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
687-
{
688-
// filter type: ... queries
689-
if (typePassesFilter(typeFilter, ty) &&
690-
results[fullId] === undefined) {
691-
index = searchWords[j].replace(/_/g, "").indexOf(val);
692-
}
710+
}
711+
712+
var returned = false;
713+
var in_args = false;
714+
var index = -1;
715+
// we want lev results to go lower than others
716+
var lev = MAX_LEV_DISTANCE + 1;
717+
var fullId = itemTypes[ty.ty] + ty.path + ty.name;
718+
719+
if (searchWords[j].indexOf(val) > -1 ||
720+
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
721+
{
722+
// filter type: ... queries
723+
if (typePassesFilter(typeFilter, ty) &&
724+
results[fullId] === undefined) {
725+
index = searchWords[j].replace(/_/g, "").indexOf(val);
693726
}
694-
if ((lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
695-
if (typePassesFilter(typeFilter, ty) &&
696-
(results[fullId] === undefined ||
697-
results[fullId].lev > lev_distance)) {
698-
lev = Math.min(lev, lev_distance);
699-
index = Math.max(0, index);
700-
}
727+
}
728+
if ((lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
729+
if (typePassesFilter(typeFilter, ty) &&
730+
(results[fullId] === undefined ||
731+
results[fullId].lev > lev_distance)) {
732+
lev = Math.min(lev, lev_distance);
733+
index = Math.max(0, index);
701734
}
702-
if ((lev_distance = findArg(searchIndex[j], valGenerics))
703-
<= MAX_LEV_DISTANCE) {
704-
if (typePassesFilter(typeFilter, ty) &&
705-
(results[fullId] === undefined ||
706-
results[fullId].lev > lev_distance)) {
707-
in_args = true;
708-
lev = Math.min(lev_distance, lev);
709-
index = Math.max(0, index);
710-
}
735+
}
736+
if ((lev_distance = findArg(searchIndex[j], valGenerics))
737+
<= MAX_LEV_DISTANCE) {
738+
if (typePassesFilter(typeFilter, ty) &&
739+
(results[fullId] === undefined ||
740+
results[fullId].lev > lev_distance)) {
741+
in_args = true;
742+
lev = Math.min(lev_distance, lev);
743+
index = Math.max(0, index);
711744
}
712-
if ((lev_distance = checkReturned(searchIndex[j], valGenerics)) <=
713-
MAX_LEV_DISTANCE) {
714-
if (typePassesFilter(typeFilter, ty) &&
715-
(results[fullId] === undefined ||
716-
results[fullId].lev > lev_distance)) {
717-
returned = true;
718-
lev = Math.min(lev_distance, lev);
719-
index = Math.max(0, index);
720-
}
745+
}
746+
if ((lev_distance = checkReturned(searchIndex[j], valGenerics)) <=
747+
MAX_LEV_DISTANCE) {
748+
if (typePassesFilter(typeFilter, ty) &&
749+
(results[fullId] === undefined ||
750+
results[fullId].lev > lev_distance)) {
751+
returned = true;
752+
lev = Math.min(lev_distance, lev);
753+
index = Math.max(0, index);
721754
}
722-
if (index !== -1) {
723-
if (results[fullId] === undefined) {
724-
results[fullId] = {
725-
id: j,
726-
index: index,
727-
lev: lev,
728-
in_args: in_args,
729-
returned: returned,
730-
};
731-
results_length += 1;
732-
} else {
733-
if (results[fullId].lev > lev) {
734-
results[fullId].lev = lev;
735-
}
736-
if (in_args === true) {
737-
results[fullId].in_args = true;
738-
}
739-
if (returned === true) {
740-
results[fullId].returned = true;
741-
}
755+
}
756+
lev += lev_add;
757+
if (index !== -1) {
758+
if (results[fullId] === undefined) {
759+
results[fullId] = {
760+
id: j,
761+
index: index,
762+
lev: lev,
763+
in_args: in_args,
764+
returned: returned,
765+
};
766+
results_length += 1;
767+
} else {
768+
if (results[fullId].lev > lev) {
769+
results[fullId].lev = lev;
770+
}
771+
if (in_args === true) {
772+
results[fullId].in_args = true;
773+
}
774+
if (returned === true) {
775+
results[fullId].returned = true;
742776
}
743777
}
744-
if (results_length === max) {
745-
break;
746-
}
778+
}
779+
if (results_length === max) {
780+
break;
747781
}
748782
}
749783
}
@@ -834,16 +868,14 @@
834868
var result = results[i];
835869

836870
// this validation does not make sense when searching by types
837-
if (result.dontValidate) {
871+
if (result.dontValidate || result.returned === true && result.param === true) {
838872
continue;
839873
}
840874
var name = result.item.name.toLowerCase(),
841875
path = result.item.path.toLowerCase(),
842876
parent = result.item.parent;
843877

844-
if (result.returned === false && result.param === false &&
845-
validateResult(name, path, split, parent) === false)
846-
{
878+
if (validateResult(name, path, split, parent) === false) {
847879
result.id = -1;
848880
}
849881
}

0 commit comments

Comments
 (0)