]> git.lizzy.rs Git - rust.git/commitdiff
Merge aliases and search-index
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 4 May 2020 12:51:06 +0000 (14:51 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 7 May 2020 18:08:08 +0000 (20:08 +0200)
src/librustdoc/html/layout.rs
src/librustdoc/html/render.rs
src/librustdoc/html/render/cache.rs
src/librustdoc/html/static/main.js
src/test/rustdoc-js-std/alias-2.js
src/test/rustdoc-js-std/alias.js
src/tools/rustdoc-js/tester.js

index 0922c8cdd1200891c785bedbac0a4214242ae58c..ea65b3905272e9c180ae75d081c5cca588d9d713 100644 (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}\
index 4ed367a5c80d37d99f55aeedb3c2f98f30ed4d8a..9454baf7640979975b09e5a971c12ef4dd260c01 100644 (file)
@@ -825,42 +825,6 @@ fn collect_json(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String
         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)]
index 17003334bc8560ac88a626b75dd33afefab4764c..53cf1abb16d5639082188fba7e09fd52e31b086c 100644 (file)
@@ -503,27 +503,6 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
     }
 }
 
-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 @@ struct CrateData<'a> {
         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 @@ struct CrateData<'a> {
             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.
index 411d2d44059b202857d83ffdc2f4983bd62d7a02..94ae69fde57fd5ae9ca153b43c5621ea03432c0d 100644 (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;
         }
index 0ce1b87b7615329928e1f26dc1df52fa3d5e5d67..cb6ec4f8fed4e6f284be64ee00ff6c72bd44a10e 100644 (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' },
     ],
 };
index 0b1e983117f2fd5e8645ef2d0e88a41e842b8f4d..2b709c99119ae11e7a2b079d36c1d51f6f2955a6 100644 (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' },
     ],
 };
index 72bc496c5b5d34e5083e091181327bf5ef3330c5..90315d6f64431af49ba74250e060fda715d0e025 100644 (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() {