Skip to content

Commit 1f4435a

Browse files
committed
rustdoc: make srcIndex no longer a global variable
this is one-time initialization data, it can just be a function parameter. we also move the json parsing into createSrcSidebar to save a few bytes.
1 parent 642e49b commit 1f4435a

File tree

5 files changed

+12
-31
lines changed

5 files changed

+12
-31
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,7 @@ impl SourcesPart {
477477
// This needs to be `var`, not `const`.
478478
// This variable needs declared in the current global scope so that if
479479
// src-script.js loads first, it can pick it up.
480-
SortedTemplate::from_before_after(
481-
r"var srcIndex = new Map(JSON.parse('[",
482-
r"]'));
483-
createSrcSidebar();",
484-
)
480+
SortedTemplate::from_before_after(r"createSrcSidebar('[", r"]');")
485481
}
486482

487483
fn get(cx: &Context<'_>, crate_name: &OrderedJson) -> Result<PartsAndLocations<Self>, Error> {

src/librustdoc/html/render/write_shared/tests.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,11 @@ fn but_last_line(s: &str) -> &str {
2222
#[test]
2323
fn sources_template() {
2424
let mut template = SourcesPart::blank();
25-
assert_eq!(
26-
but_last_line(&template.to_string()),
27-
r"var srcIndex = new Map(JSON.parse('[]'));
28-
createSrcSidebar();"
29-
);
25+
assert_eq!(but_last_line(&template.to_string()), r"createSrcSidebar('[]');");
3026
template.append(EscapedJson::from(OrderedJson::serialize("u").unwrap()).to_string());
31-
assert_eq!(
32-
but_last_line(&template.to_string()),
33-
r#"var srcIndex = new Map(JSON.parse('["u"]'));
34-
createSrcSidebar();"#
35-
);
27+
assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u"]');"#);
3628
template.append(EscapedJson::from(OrderedJson::serialize("v").unwrap()).to_string());
37-
assert_eq!(
38-
but_last_line(&template.to_string()),
39-
r#"var srcIndex = new Map(JSON.parse('["u","v"]'));
40-
createSrcSidebar();"#
41-
);
29+
assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u","v"]');"#);
4230
}
4331

4432
#[test]

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
/* eslint-disable */
66
declare global {
7-
/** Map from crate name to directory structure, for source view */
8-
declare var srcIndex: Map<string, rustdoc.Dir>;
97
/** Defined and documented in `storage.js` */
108
declare function nonnull(x: T|null, msg: string|undefined);
119
/** Defined and documented in `storage.js` */
@@ -64,7 +62,7 @@ declare global {
6462
* create's the sidebar in source code view.
6563
* called in generated `src-files.js`.
6664
*/
67-
createSrcSidebar?: function(),
65+
createSrcSidebar?: function(string),
6866
/**
6967
* Set up event listeners for a scraped source example.
7068
*/
@@ -129,7 +127,7 @@ declare namespace rustdoc {
129127

130128
/**
131129
* A single parsed "atom" in a search query. For example,
132-
*
130+
*
133131
* std::fmt::Formatter, Write -> Result<()>
134132
* ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
135133
* ┃ │ ┗ QueryElement { ┊

src/librustdoc/html/static/js/src-script.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// From rust:
2-
/* global srcIndex */
3-
41
// Local js definitions:
52
/* global addClass, onEachLazy, removeClass, browserSupportsHistoryApi */
63
/* global updateLocalStorage, getVar, nonnull */
@@ -100,11 +97,16 @@ window.rustdocToggleSrcSidebar = () => {
10097

10198
// This function is called from "src-files.js", generated in `html/render/write_shared.rs`.
10299
// eslint-disable-next-line no-unused-vars
103-
function createSrcSidebar() {
100+
/**
101+
* @param {string} srcIndexStr - strinified json map from crate name to dir structure
102+
*/
103+
function createSrcSidebar(srcIndexStr) {
104+
console.log(srcIndexStr);
104105
const container = nonnull(document.querySelector("nav.sidebar"));
105106

106107
const sidebar = document.createElement("div");
107108
sidebar.id = "src-sidebar";
109+
const srcIndex = new Map(JSON.parse(srcIndexStr));
108110

109111
let hasFoundFile = false;
110112

tests/rustdoc-gui/globals.goml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=sa'%3Bda'%3Bds"
77
wait-for: "#search-tabs"
88
assert-window-property-false: {"searchIndex": null}
9-
assert-window-property: {"srcIndex": null}
109

1110
// Form input
1211
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
1312
write-into: (".search-input", "Foo")
1413
press-key: 'Enter'
1514
wait-for: "#search-tabs"
1615
assert-window-property-false: {"searchIndex": null}
17-
assert-window-property: {"srcIndex": null}
1816

1917
// source sidebar
2018
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
2119
click: "#sidebar-button"
2220
wait-for: "#src-sidebar details"
23-
assert-window-property-false: {"srcIndex": null}
2421
assert-window-property: {"searchIndex": null}

0 commit comments

Comments
 (0)