Skip to content

Commit f581cf7

Browse files
Merge aliases and search-index
1 parent 9697c46 commit f581cf7

File tree

7 files changed

+118
-108
lines changed

7 files changed

+118
-108
lines changed

src/librustdoc/html/layout.rs

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub fn render<T: Print, S: Print>(
114114
window.rootPath = \"{root_path}\";\
115115
window.currentCrate = \"{krate}\";\
116116
</script>\
117-
<script src=\"{root_path}aliases{suffix}.js\"></script>\
118117
<script src=\"{static_root_path}main{suffix}.js\"></script>\
119118
{static_extra_scripts}\
120119
{extra_scripts}\

src/librustdoc/html/render.rs

-36
Original file line numberDiff line numberDiff line change
@@ -825,42 +825,6 @@ themePicker.onblur = handleThemeButtonsBlur;
825825
Ok((ret, krates))
826826
}
827827

828-
fn show_item(item: &IndexItem, krate: &str) -> String {
829-
format!(
830-
"{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}",
831-
krate,
832-
item.ty as usize,
833-
item.name,
834-
item.desc.replace("'", "\\'"),
835-
item.path,
836-
if let Some(p) = item.parent_idx { format!(",'parent':{}", p) } else { String::new() }
837-
)
838-
}
839-
840-
let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
841-
{
842-
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
843-
let mut output = String::with_capacity(100);
844-
for (alias, items) in cx.cache.get_aliases() {
845-
if items.is_empty() {
846-
continue;
847-
}
848-
output.push_str(&format!(
849-
"\"{}\":[{}],",
850-
alias,
851-
items.iter().map(|v| show_item(v, &krate.name)).collect::<Vec<_>>().join(",")
852-
));
853-
}
854-
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
855-
all_aliases.sort();
856-
let mut v = Buffer::html();
857-
writeln!(&mut v, "var ALIASES = {{}};");
858-
for aliases in &all_aliases {
859-
writeln!(&mut v, "{}", aliases);
860-
}
861-
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
862-
}
863-
864828
use std::ffi::OsString;
865829

866830
#[derive(Debug)]

src/librustdoc/html/render/cache.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -503,27 +503,6 @@ impl DocFolder for Cache {
503503
}
504504
}
505505

506-
impl Cache {
507-
pub fn get_aliases<'a>(&'a self) -> FxHashMap<String, Vec<&'a IndexItem>> {
508-
self.aliases
509-
.iter()
510-
.map(|(k, values)| {
511-
(
512-
k.clone(),
513-
values
514-
.iter()
515-
.filter(|v| {
516-
let x = &self.search_index[**v];
517-
x.parent_idx.is_some() == x.parent.is_some()
518-
})
519-
.map(|v| &self.search_index[*v])
520-
.collect::<Vec<_>>(),
521-
)
522-
})
523-
.collect()
524-
}
525-
}
526-
527506
/// Attempts to find where an external crate is located, given that we're
528507
/// rendering in to the specified source destination.
529508
fn extern_location(
@@ -640,13 +619,35 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
640619
.map(|module| shorten(plain_summary_line(module.doc_value())))
641620
.unwrap_or(String::new());
642621

622+
let crate_aliases = aliases
623+
.iter()
624+
.map(|(k, values)| {
625+
(
626+
k.clone(),
627+
values
628+
.iter()
629+
.filter_map(|v| {
630+
let x = &crate_items[*v];
631+
if x.parent_idx.is_some() == x.parent.is_some() { Some(*v) } else { None }
632+
})
633+
.collect::<Vec<_>>(),
634+
)
635+
})
636+
.filter(|(_, values)| !values.is_empty())
637+
.collect::<Vec<_>>();
638+
643639
#[derive(Serialize)]
644640
struct CrateData<'a> {
645641
doc: String,
646642
#[serde(rename = "i")]
647643
items: Vec<&'a IndexItem>,
648644
#[serde(rename = "p")]
649645
paths: Vec<(ItemType, String)>,
646+
// The String is alias name and the vec is the list of the elements with this alias.
647+
//
648+
// To be noted: the `usize` elements are indexes to `items`.
649+
#[serde(rename = "a")]
650+
aliases: Option<Vec<(String, Vec<usize>)>>,
650651
}
651652

652653
// Collect the index into a string
@@ -657,6 +658,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
657658
doc: crate_doc,
658659
items: crate_items,
659660
paths: crate_paths,
661+
aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) },
660662
})
661663
.expect("failed serde conversion")
662664
// All these `replace` calls are because we have to go through JS string for JSON content.

src/librustdoc/html/static/main.js

+87-39
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ function getSearchElement() {
531531
var OUTPUT_DATA = 1;
532532
var NO_TYPE_FILTER = -1;
533533
var currentResults, index, searchIndex;
534+
var ALIASES = {};
534535
var params = getQueryStringParams();
535536

536537
// Populate search bar with query string search term when provided,
@@ -963,46 +964,60 @@ function getSearchElement() {
963964
return itemTypes[ty.ty] + ty.path + ty.name;
964965
}
965966

967+
function createAliasFromItem(item) {
968+
return {
969+
crate: item.crate,
970+
name: item.name,
971+
path: item.path,
972+
desc: item.desc,
973+
ty: item.ty,
974+
parent: item.parent,
975+
type: item.parent,
976+
is_alias: true,
977+
};
978+
}
979+
966980
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+
var aliases = [];
982+
var i;
983+
if (filterCrates !== undefined &&
984+
ALIASES[filterCrates] &&
985+
ALIASES[filterCrates][query.search]) {
986+
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
987+
aliases.push(
988+
createAliasFromItem(searchIndex[ALIASES[filterCrates][query.search]]));
981989
}
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;
990+
} else {
991+
Object.keys(ALIASES).forEach(function(crate) {
992+
if (ALIASES[crate][query.search]) {
993+
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
994+
aliases.push(
995+
createAliasFromItem(
996+
searchIndex[ALIASES[crate][query.search][i]]));
997+
}
987998
}
988-
return -1;
989999
});
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-
}
1000+
}
1001+
aliases.sort(function(aaa, bbb) {
1002+
if (aaa.path < bbb.path) {
1003+
return 1;
1004+
} else if (aaa.path === bbb.path) {
1005+
return 0;
1006+
}
1007+
return -1;
1008+
});
1009+
for (i = 0; i < aliases.length; ++i) {
1010+
var alias = aliases[i];
1011+
1012+
alias.alias = query.raw;
1013+
var res = buildHrefAndPath(alias);
1014+
alias.displayPath = pathSplitter(res[0]);
1015+
alias.fullPath = alias.displayPath + alias.name;
1016+
alias.href = res[1];
1017+
1018+
ret.others.unshift(alias);
1019+
if (ret.others.length > MAX_RESULTS) {
1020+
ret.others.pop();
10061021
}
10071022
}
10081023
}
@@ -1683,10 +1698,13 @@ function getSearchElement() {
16831698
searchIndex = [];
16841699
var searchWords = [];
16851700
var i;
1701+
var currentIndex = 0;
16861702

16871703
for (var crate in rawSearchIndex) {
16881704
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
16891705

1706+
var crateSize = 0;
1707+
16901708
searchWords.push(crate);
16911709
searchIndex.push({
16921710
crate: crate,
@@ -1696,6 +1714,7 @@ function getSearchElement() {
16961714
desc: rawSearchIndex[crate].doc,
16971715
type: null,
16981716
});
1717+
currentIndex += 1;
16991718

17001719
// an array of [(Number) item type,
17011720
// (String) name,
@@ -1707,6 +1726,9 @@ function getSearchElement() {
17071726
// an array of [(Number) item type,
17081727
// (String) name]
17091728
var paths = rawSearchIndex[crate].p;
1729+
// a array of [(String) alias name
1730+
// [Number] index to items]
1731+
var aliases = rawSearchIndex[crate].a;
17101732

17111733
// convert `rawPaths` entries into object form
17121734
var len = paths.length;
@@ -1725,9 +1747,18 @@ function getSearchElement() {
17251747
var lastPath = "";
17261748
for (i = 0; i < len; ++i) {
17271749
var rawRow = items[i];
1728-
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
1729-
path: rawRow[2] || lastPath, desc: rawRow[3],
1730-
parent: paths[rawRow[4]], type: rawRow[5]};
1750+
if (!rawRow[2]) {
1751+
rawRow[2] = lastPath;
1752+
}
1753+
var row = {
1754+
crate: crate,
1755+
ty: rawRow[0],
1756+
name: rawRow[1],
1757+
path: rawRow[2],
1758+
desc: rawRow[3],
1759+
parent: paths[rawRow[4]],
1760+
type: rawRow[5],
1761+
};
17311762
searchIndex.push(row);
17321763
if (typeof row.name === "string") {
17331764
var word = row.name.toLowerCase();
@@ -1736,7 +1767,24 @@ function getSearchElement() {
17361767
searchWords.push("");
17371768
}
17381769
lastPath = row.path;
1770+
crateSize += 1;
1771+
}
1772+
1773+
if (aliases) {
1774+
ALIASES[crate] = {};
1775+
var j, local_aliases;
1776+
for (i = 0; i < aliases.length; ++i) {
1777+
var alias_name = aliases[i][0];
1778+
if (!ALIASES[crate].hasOwnProperty(alias_name)) {
1779+
ALIASES[crate][alias_name] = [];
1780+
}
1781+
local_aliases = aliases[i][1];
1782+
for (j = 0; j < local_aliases.length; ++j) {
1783+
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
1784+
}
1785+
}
17391786
}
1787+
currentIndex += crateSize;
17401788
}
17411789
return searchWords;
17421790
}

src/test/rustdoc-js-std/alias-2.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const QUERY = '+';
44

55
const EXPECTED = {
66
'others': [
7-
{ 'path': 'core', 'name': 'AddAssign' },
8-
{ 'path': 'core', 'name': 'Add' },
9-
{ 'path': 'std', 'name': 'AddAssign' },
7+
{ 'path': 'core::ops', 'name': 'AddAssign' },
8+
{ 'path': 'core::ops', 'name': 'Add' },
9+
{ 'path': 'std::ops', 'name': 'AddAssign' },
1010
{ 'path': 'std::ops', 'name': 'Add' },
1111
],
1212
};

src/test/rustdoc-js-std/alias.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const QUERY = '[';
55
const EXPECTED = {
66
'others': [
77
{ 'path': 'std', 'name': 'slice' },
8-
{ 'path': 'std', 'name': 'IndexMut' },
9-
{ 'path': 'std', 'name': 'Index' },
8+
{ 'path': 'std::ops', 'name': 'IndexMut' },
9+
{ 'path': 'std::ops', 'name': 'Index' },
1010
],
1111
};

src/tools/rustdoc-js/tester.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function lookForEntry(entry, data) {
218218
return null;
219219
}
220220

221-
function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
221+
function loadMainJsAndIndex(mainJs, searchIndex, crate) {
222222
if (searchIndex[searchIndex.length - 1].length === 0) {
223223
searchIndex.pop();
224224
}
@@ -238,17 +238,15 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
238238
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
239239
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
240240

241+
ALIASES = {};
241242
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
242243
finalJS += 'var rootPath = "../";\n';
243-
finalJS += aliases;
244244
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
245245
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
246246
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
247247

248248
var loaded = loadContent(finalJS);
249249
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
250-
// We make it "global" so that the "loaded.execSearch" function will find it.
251-
rawSearchIndex = searchIndex.rawSearchIndex;
252250

253251
return [loaded, index];
254252
}
@@ -340,11 +338,10 @@ function runChecks(testFile, loaded, index) {
340338

341339
function load_files(doc_folder, resource_suffix, crate) {
342340
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
343-
var aliases = readFile(path.join(doc_folder, "aliases" + resource_suffix + ".js"));
344341
var searchIndex = readFile(
345342
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
346343

347-
return loadMainJsAndIndex(mainJs, aliases, searchIndex, crate);
344+
return loadMainJsAndIndex(mainJs, searchIndex, crate);
348345
}
349346

350347
function showHelp() {

0 commit comments

Comments
 (0)