Merge aliases and search-index

This commit is contained in:
Guillaume Gomez 2020-05-04 14:51:06 +02:00
parent 9697c467ac
commit f581cf7544
7 changed files with 118 additions and 108 deletions

View File

@ -114,7 +114,6 @@ pub fn render<T: Print, S: Print>(
window.rootPath = \"{root_path}\";\
window.currentCrate = \"{krate}\";\
</script>\
<script src=\"{root_path}aliases{suffix}.js\"></script>\
<script src=\"{static_root_path}main{suffix}.js\"></script>\
{static_extra_scripts}\
{extra_scripts}\

View File

@ -825,42 +825,6 @@ themePicker.onblur = handleThemeButtonsBlur;
Ok((ret, krates))
}
fn show_item(item: &IndexItem, krate: &str) -> String {
format!(
"{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}",
krate,
item.ty as usize,
item.name,
item.desc.replace("'", "\\'"),
item.path,
if let Some(p) = item.parent_idx { format!(",'parent':{}", p) } else { String::new() }
)
}
let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
{
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
let mut output = String::with_capacity(100);
for (alias, items) in cx.cache.get_aliases() {
if items.is_empty() {
continue;
}
output.push_str(&format!(
"\"{}\":[{}],",
alias,
items.iter().map(|v| show_item(v, &krate.name)).collect::<Vec<_>>().join(",")
));
}
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
all_aliases.sort();
let mut v = Buffer::html();
writeln!(&mut v, "var ALIASES = {{}};");
for aliases in &all_aliases {
writeln!(&mut v, "{}", aliases);
}
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
}
use std::ffi::OsString;
#[derive(Debug)]

View File

@ -503,27 +503,6 @@ impl DocFolder for Cache {
}
}
impl Cache {
pub fn get_aliases<'a>(&'a self) -> FxHashMap<String, Vec<&'a IndexItem>> {
self.aliases
.iter()
.map(|(k, values)| {
(
k.clone(),
values
.iter()
.filter(|v| {
let x = &self.search_index[**v];
x.parent_idx.is_some() == x.parent.is_some()
})
.map(|v| &self.search_index[*v])
.collect::<Vec<_>>(),
)
})
.collect()
}
}
/// Attempts to find where an external crate is located, given that we're
/// rendering in to the specified source destination.
fn extern_location(
@ -640,6 +619,23 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
.map(|module| shorten(plain_summary_line(module.doc_value())))
.unwrap_or(String::new());
let crate_aliases = aliases
.iter()
.map(|(k, values)| {
(
k.clone(),
values
.iter()
.filter_map(|v| {
let x = &crate_items[*v];
if x.parent_idx.is_some() == x.parent.is_some() { Some(*v) } else { None }
})
.collect::<Vec<_>>(),
)
})
.filter(|(_, values)| !values.is_empty())
.collect::<Vec<_>>();
#[derive(Serialize)]
struct CrateData<'a> {
doc: String,
@ -647,6 +643,11 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
items: Vec<&'a IndexItem>,
#[serde(rename = "p")]
paths: Vec<(ItemType, String)>,
// The String is alias name and the vec is the list of the elements with this alias.
//
// To be noted: the `usize` elements are indexes to `items`.
#[serde(rename = "a")]
aliases: Option<Vec<(String, Vec<usize>)>>,
}
// Collect the index into a string
@ -657,6 +658,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
doc: crate_doc,
items: crate_items,
paths: crate_paths,
aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) },
})
.expect("failed serde conversion")
// All these `replace` calls are because we have to go through JS string for JSON content.

View File

@ -531,6 +531,7 @@ function getSearchElement() {
var OUTPUT_DATA = 1;
var NO_TYPE_FILTER = -1;
var currentResults, index, searchIndex;
var ALIASES = {};
var params = getQueryStringParams();
// Populate search bar with query string search term when provided,
@ -963,46 +964,60 @@ function getSearchElement() {
return itemTypes[ty.ty] + ty.path + ty.name;
}
function createAliasFromItem(item) {
return {
crate: item.crate,
name: item.name,
path: item.path,
desc: item.desc,
ty: item.ty,
parent: item.parent,
type: item.parent,
is_alias: true,
};
}
function handleAliases(ret, query, filterCrates) {
if (ALIASES) {
var aliases = [];
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
ALIASES[filterCrates][query.search]) {
aliases = ALIASES[filterCrates][query.search];
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
for (var i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(ALIASES[crate][query.search][i]);
}
}
});
var aliases = [];
var i;
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
createAliasFromItem(searchIndex[ALIASES[filterCrates][query.search]]));
}
aliases.sort(function(aaa, bbb) {
if (aaa.path < bbb.path) {
return 1;
} else if (aaa.path === bbb.path) {
return 0;
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
createAliasFromItem(
searchIndex[ALIASES[crate][query.search][i]]));
}
}
return -1;
});
for (var i = 0; i < aliases.length; ++i) {
var alias = aliases[i];
alias.is_alias = true;
if (typeof alias.parent === "number") {
alias.parent = rawSearchIndex[alias.crate].p[alias.parent];
}
alias.alias = query.raw;
alias.path = alias.p || alias.crate;
var res = buildHrefAndPath(aliases[i]);
alias.displayPath = pathSplitter(res[0]);
alias.fullPath = alias.displayPath + alias.name;
alias.href = res[1];
ret.others.unshift(alias);
if (ret.others.length > MAX_RESULTS) {
ret.others.pop();
}
}
aliases.sort(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];
alias.alias = query.raw;
var res = buildHrefAndPath(alias);
alias.displayPath = pathSplitter(res[0]);
alias.fullPath = alias.displayPath + alias.name;
alias.href = res[1];
ret.others.unshift(alias);
if (ret.others.length > MAX_RESULTS) {
ret.others.pop();
}
}
}
@ -1683,10 +1698,13 @@ function getSearchElement() {
searchIndex = [];
var searchWords = [];
var i;
var currentIndex = 0;
for (var crate in rawSearchIndex) {
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
var crateSize = 0;
searchWords.push(crate);
searchIndex.push({
crate: crate,
@ -1696,6 +1714,7 @@ function getSearchElement() {
desc: rawSearchIndex[crate].doc,
type: null,
});
currentIndex += 1;
// an array of [(Number) item type,
// (String) name,
@ -1707,6 +1726,9 @@ function getSearchElement() {
// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].p;
// a array of [(String) alias name
// [Number] index to items]
var aliases = rawSearchIndex[crate].a;
// convert `rawPaths` entries into object form
var len = paths.length;
@ -1725,9 +1747,18 @@ function getSearchElement() {
var lastPath = "";
for (i = 0; i < len; ++i) {
var rawRow = items[i];
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
path: rawRow[2] || lastPath, desc: rawRow[3],
parent: paths[rawRow[4]], type: rawRow[5]};
if (!rawRow[2]) {
rawRow[2] = lastPath;
}
var row = {
crate: crate,
ty: rawRow[0],
name: rawRow[1],
path: rawRow[2],
desc: rawRow[3],
parent: paths[rawRow[4]],
type: rawRow[5],
};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
@ -1736,7 +1767,24 @@ function getSearchElement() {
searchWords.push("");
}
lastPath = row.path;
crateSize += 1;
}
if (aliases) {
ALIASES[crate] = {};
var j, local_aliases;
for (i = 0; i < aliases.length; ++i) {
var alias_name = aliases[i][0];
if (!ALIASES[crate].hasOwnProperty(alias_name)) {
ALIASES[crate][alias_name] = [];
}
local_aliases = aliases[i][1];
for (j = 0; j < local_aliases.length; ++j) {
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
}
}
}
currentIndex += crateSize;
}
return searchWords;
}

View File

@ -4,9 +4,9 @@ const QUERY = '+';
const EXPECTED = {
'others': [
{ 'path': 'core', 'name': 'AddAssign' },
{ 'path': 'core', 'name': 'Add' },
{ 'path': 'std', 'name': 'AddAssign' },
{ 'path': 'core::ops', 'name': 'AddAssign' },
{ 'path': 'core::ops', 'name': 'Add' },
{ 'path': 'std::ops', 'name': 'AddAssign' },
{ 'path': 'std::ops', 'name': 'Add' },
],
};

View File

@ -5,7 +5,7 @@ const QUERY = '[';
const EXPECTED = {
'others': [
{ 'path': 'std', 'name': 'slice' },
{ 'path': 'std', 'name': 'IndexMut' },
{ 'path': 'std', 'name': 'Index' },
{ 'path': 'std::ops', 'name': 'IndexMut' },
{ 'path': 'std::ops', 'name': 'Index' },
],
};

View File

@ -218,7 +218,7 @@ function lookForEntry(entry, data) {
return null;
}
function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
function loadMainJsAndIndex(mainJs, searchIndex, crate) {
if (searchIndex[searchIndex.length - 1].length === 0) {
searchIndex.pop();
}
@ -238,17 +238,15 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
ALIASES = {};
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
finalJS += 'var rootPath = "../";\n';
finalJS += aliases;
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
var loaded = loadContent(finalJS);
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
// We make it "global" so that the "loaded.execSearch" function will find it.
rawSearchIndex = searchIndex.rawSearchIndex;
return [loaded, index];
}
@ -340,11 +338,10 @@ 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 aliases = readFile(path.join(doc_folder, "aliases" + resource_suffix + ".js"));
var searchIndex = readFile(
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
return loadMainJsAndIndex(mainJs, aliases, searchIndex, crate);
return loadMainJsAndIndex(mainJs, searchIndex, crate);
}
function showHelp() {