Skip to content

Commit 7590c39

Browse files
Improve doc alias JS code
1 parent d80ac14 commit 7590c39

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/librustdoc/html/static/main.js

+50-23
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,50 @@ function getSearchElement() {
963963
return itemTypes[ty.ty] + ty.path + ty.name;
964964
}
965965

966+
function handleAliases(ret, query, filterCrates) {
967+
if (ALIASES) {
968+
var aliases = [];
969+
if (filterCrates !== undefined &&
970+
ALIASES[filterCrates] &&
971+
ALIASES[filterCrates][query.search]) {
972+
aliases = ALIASES[filterCrates][query.search];
973+
} else {
974+
Object.keys(ALIASES).forEach(function(crate) {
975+
if (ALIASES[crate][query.search]) {
976+
for (var i = 0; i < ALIASES[crate][query.search].length; ++i) {
977+
aliases.push(ALIASES[crate][query.search][i]);
978+
}
979+
}
980+
});
981+
}
982+
aliases.sort(function(aaa, bbb) {
983+
if (aaa.path < bbb.path) {
984+
return 1;
985+
} else if (aaa.path === bbb.path) {
986+
return 0;
987+
}
988+
return -1;
989+
});
990+
for (var i = 0; i < aliases.length; ++i) {
991+
var alias = aliases[i];
992+
alias.is_alias = true;
993+
if (typeof alias.parent === "number") {
994+
alias.parent = rawSearchIndex[alias.crate].p[alias.parent];
995+
}
996+
alias.alias = query.raw;
997+
alias.path = alias.p || alias.crate;
998+
var res = buildHrefAndPath(aliases[i]);
999+
alias.displayPath = pathSplitter(res[0]);
1000+
alias.fullPath = alias.displayPath + alias.name;
1001+
alias.href = res[1];
1002+
ret.others.unshift(alias);
1003+
if (ret.others.length > MAX_RESULTS) {
1004+
ret.others.pop();
1005+
}
1006+
}
1007+
}
1008+
}
1009+
9661010
// quoted values mean literal search
9671011
var nSearchWords = searchWords.length;
9681012
var i;
@@ -1190,23 +1234,7 @@ function getSearchElement() {
11901234
"returned": sortResults(results_returned, true),
11911235
"others": sortResults(results),
11921236
};
1193-
if (ALIASES && ALIASES[window.currentCrate] &&
1194-
ALIASES[window.currentCrate][query.raw]) {
1195-
var aliases = ALIASES[window.currentCrate][query.raw];
1196-
for (i = 0; i < aliases.length; ++i) {
1197-
aliases[i].is_alias = true;
1198-
aliases[i].alias = query.raw;
1199-
aliases[i].path = aliases[i].p;
1200-
var res = buildHrefAndPath(aliases[i]);
1201-
aliases[i].displayPath = pathSplitter(res[0]);
1202-
aliases[i].fullPath = aliases[i].displayPath + aliases[i].name;
1203-
aliases[i].href = res[1];
1204-
ret.others.unshift(aliases[i]);
1205-
if (ret.others.length > MAX_RESULTS) {
1206-
ret.others.pop();
1207-
}
1208-
}
1209-
}
1237+
handleAliases(ret, query, filterCrates);
12101238
return ret;
12111239
}
12121240

@@ -1599,13 +1627,12 @@ function getSearchElement() {
15991627
"returned": mergeArrays(results.returned),
16001628
"others": mergeArrays(results.others),
16011629
};
1602-
} else {
1603-
return {
1604-
"in_args": results.in_args[0],
1605-
"returned": results.returned[0],
1606-
"others": results.others[0],
1607-
};
16081630
}
1631+
return {
1632+
"in_args": results.in_args[0],
1633+
"returned": results.returned[0],
1634+
"others": results.others[0],
1635+
};
16091636
}
16101637

16111638
function getFilterCrates() {

0 commit comments

Comments
 (0)