Skip to content

Commit

Permalink
Make current crate aliases go first
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 14, 2020
1 parent 883c177 commit c4d9318
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 34 deletions.
20 changes: 14 additions & 6 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,10 @@ function getSearchElement() {
}

function handleAliases(ret, query, filterCrates) {
// We separate aliases and crate aliases because we want to have current crate
// aliases to be before the others in the displayed results.
var aliases = [];
var crateAliases = [];
var i;
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
Expand All @@ -990,25 +993,28 @@ function getSearchElement() {
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
var pushTo = crate === window.currentCrate ? crateAliases : aliases;
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
pushTo.push(
createAliasFromItem(
searchIndex[ALIASES[crate][query.search][i]]));
}
}
});
}
aliases.sort(function(aaa, bbb) {

var sortFunc = function(aaa, bbb) {
if (aaa.path < bbb.path) {
return 1;
} else if (aaa.path === bbb.path) {
return 0;
}
return -1;
});
for (i = 0; i < aliases.length; ++i) {
var alias = aliases[i];
};
crateAliases.sort(sortFunc);
aliases.sort(sortFunc);

var pushFunc = function(alias) {
alias.alias = query.raw;
var res = buildHrefAndPath(alias);
alias.displayPath = pathSplitter(res[0]);
Expand All @@ -1019,7 +1025,9 @@ function getSearchElement() {
if (ret.others.length > MAX_RESULTS) {
ret.others.pop();
}
}
};
onEach(aliases, pushFunc);
onEach(crateAliases, pushFunc);
}

// quoted values mean literal search
Expand Down
6 changes: 2 additions & 4 deletions src/test/rustdoc-js-std/alias-2.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// ignore-order

const QUERY = '+';

const EXPECTED = {
'others': [
{ 'path': 'core::ops', 'name': 'AddAssign' },
{ 'path': 'core::ops', 'name': 'Add' },
{ 'path': 'std::ops', 'name': 'AddAssign' },
{ 'path': 'std::ops', 'name': 'Add' },
{ 'path': 'core::ops', 'name': 'AddAssign' },
{ 'path': 'core::ops', 'name': 'Add' },
],
};
68 changes: 47 additions & 21 deletions src/test/rustdoc-js/doc-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Struct',
'alias': 'StructItem',
'href': '../doc_alias/struct.Struct.html'
'href': '../doc_alias/struct.Struct.html',
'is_alias': true
},
],
},
Expand All @@ -42,7 +43,8 @@ const EXPECTED = [
'path': 'doc_alias::Struct',
'name': 'field',
'alias': 'StructFieldItem',
'href': '../doc_alias/struct.Struct.html#structfield.field'
'href': '../doc_alias/struct.Struct.html#structfield.field',
'is_alias': true
},
],
},
Expand All @@ -52,7 +54,8 @@ const EXPECTED = [
'path': 'doc_alias::Struct',
'name': 'method',
'alias': 'StructMethodItem',
'href': '../doc_alias/struct.Struct.html#method.method'
'href': '../doc_alias/struct.Struct.html#method.method',
'is_alias': true
},
],
},
Expand All @@ -65,16 +68,24 @@ const EXPECTED = [
'others': [],
},
{
// ImplTraitFunction
'others': [],
'others': [
{
'path': 'doc_alias::Struct',
'name': 'function',
'alias': 'ImplTraitFunction',
'href': '../doc_alias/struct.Struct.html#method.function',
'is_alias': true
},
],
},
{
'others': [
{
'path': 'doc_alias',
'name': 'Enum',
'alias': 'EnumItem',
'href': '../doc_alias/enum.Enum.html'
'href': '../doc_alias/enum.Enum.html',
'is_alias': true
},
],
},
Expand All @@ -84,7 +95,8 @@ const EXPECTED = [
'path': 'doc_alias::Enum',
'name': 'Variant',
'alias': 'VariantItem',
'href': '../doc_alias/enum.Enum.html#variant.Variant'
'href': '../doc_alias/enum.Enum.html#variant.Variant',
'is_alias': true
},
],
},
Expand All @@ -94,7 +106,8 @@ const EXPECTED = [
'path': 'doc_alias::Enum',
'name': 'method',
'alias': 'EnumMethodItem',
'href': '../doc_alias/enum.Enum.html#method.method'
'href': '../doc_alias/enum.Enum.html#method.method',
'is_alias': true
},
],
},
Expand All @@ -104,7 +117,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Typedef',
'alias': 'TypedefItem',
'href': '../doc_alias/type.Typedef.html'
'href': '../doc_alias/type.Typedef.html',
'is_alias': true
},
],
},
Expand All @@ -114,7 +128,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Trait',
'alias': 'TraitItem',
'href': '../doc_alias/trait.Trait.html'
'href': '../doc_alias/trait.Trait.html',
'is_alias': true
},
],
},
Expand All @@ -124,7 +139,8 @@ const EXPECTED = [
'path': 'doc_alias::Trait',
'name': 'Target',
'alias': 'TraitTypeItem',
'href': '../doc_alias/trait.Trait.html#associatedtype.Target'
'href': '../doc_alias/trait.Trait.html#associatedtype.Target',
'is_alias': true
},
],
},
Expand All @@ -134,7 +150,8 @@ const EXPECTED = [
'path': 'doc_alias::Trait',
'name': 'AssociatedConst',
'alias': 'AssociatedConstItem',
'href': '../doc_alias/trait.Trait.html#associatedconstant.AssociatedConst'
'href': '../doc_alias/trait.Trait.html#associatedconstant.AssociatedConst',
'is_alias': true
},
],
},
Expand All @@ -144,7 +161,8 @@ const EXPECTED = [
'path': 'doc_alias::Trait',
'name': 'function',
'alias': 'TraitFunctionItem',
'href': '../doc_alias/trait.Trait.html#tymethod.function'
'href': '../doc_alias/trait.Trait.html#tymethod.function',
'is_alias': true
},
],
},
Expand All @@ -154,7 +172,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'function',
'alias': 'FunctionItem',
'href': '../doc_alias/fn.function.html'
'href': '../doc_alias/fn.function.html',
'is_alias': true
},
],
},
Expand All @@ -164,7 +183,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Module',
'alias': 'ModuleItem',
'href': '../doc_alias/Module/index.html'
'href': '../doc_alias/Module/index.html',
'is_alias': true
},
],
},
Expand All @@ -174,7 +194,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Const',
'alias': 'ConstItem',
'href': '../doc_alias/constant.Const.html'
'href': '../doc_alias/constant.Const.html',
'is_alias': true
},
],
},
Expand All @@ -184,7 +205,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Static',
'alias': 'StaticItem',
'href': '../doc_alias/static.Static.html'
'href': '../doc_alias/static.Static.html',
'is_alias': true
},
],
},
Expand All @@ -194,7 +216,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Union',
'alias': 'UnionItem',
'href': '../doc_alias/union.Union.html'
'href': '../doc_alias/union.Union.html',
'is_alias': true
},
// Not an alias!
{
Expand All @@ -210,7 +233,8 @@ const EXPECTED = [
'path': 'doc_alias::Union',
'name': 'union_item',
'alias': 'UnionFieldItem',
'href': '../doc_alias/union.Union.html#structfield.union_item'
'href': '../doc_alias/union.Union.html#structfield.union_item',
'is_alias': true
},
],
},
Expand All @@ -220,7 +244,8 @@ const EXPECTED = [
'path': 'doc_alias::Union',
'name': 'method',
'alias': 'UnionMethodItem',
'href': '../doc_alias/union.Union.html#method.method'
'href': '../doc_alias/union.Union.html#method.method',
'is_alias': true
},
],
},
Expand All @@ -230,7 +255,8 @@ const EXPECTED = [
'path': 'doc_alias',
'name': 'Macro',
'alias': 'MacroItem',
'href': '../doc_alias/macro.Macro.html'
'href': '../doc_alias/macro.Macro.html',
'is_alias': true
},
],
},
Expand Down
1 change: 0 additions & 1 deletion src/test/rustdoc-js/doc-alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl Trait for Struct {
#[doc(alias = "ImplAssociatedConstItem")]
const AssociatedConst: i32 = 12;

// Shouldn't be listed in aliases!
#[doc(alias = "ImplTraitFunction")]
fn function() -> Self::Target { 0 }
}
Expand Down
6 changes: 4 additions & 2 deletions src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function lookForEntry(entry, data) {
return null;
}

function loadMainJsAndIndex(mainJs, searchIndex, crate) {
function loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate) {
if (searchIndex[searchIndex.length - 1].length === 0) {
searchIndex.pop();
}
Expand All @@ -241,6 +241,7 @@ function loadMainJsAndIndex(mainJs, searchIndex, crate) {
ALIASES = {};
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
finalJS += 'var rootPath = "../";\n';
finalJS += loadThings(["onEach"], 'function', extractFunction, storageJs);
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
Expand Down Expand Up @@ -338,10 +339,11 @@ function runChecks(testFile, loaded, index) {

function load_files(doc_folder, resource_suffix, crate) {
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
var storageJs = readFile(path.join(doc_folder, "storage" + resource_suffix + ".js"));
var searchIndex = readFile(
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");

return loadMainJsAndIndex(mainJs, searchIndex, crate);
return loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate);
}

function showHelp() {
Expand Down

0 comments on commit c4d9318

Please sign in to comment.