Skip to content

rustdoc: make srcIndex no longer a global variable #142100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,7 @@ impl SourcesPart {
// This needs to be `var`, not `const`.
// This variable needs declared in the current global scope so that if
// src-script.js loads first, it can pick it up.
SortedTemplate::from_before_after(
r"var srcIndex = new Map(JSON.parse('[",
r"]'));
createSrcSidebar();",
)
SortedTemplate::from_before_after(r"createSrcSidebar('[", r"]');")
}

fn get(cx: &Context<'_>, crate_name: &OrderedJson) -> Result<PartsAndLocations<Self>, Error> {
Expand Down
18 changes: 3 additions & 15 deletions src/librustdoc/html/render/write_shared/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,11 @@ fn but_last_line(s: &str) -> &str {
#[test]
fn sources_template() {
let mut template = SourcesPart::blank();
assert_eq!(
but_last_line(&template.to_string()),
r"var srcIndex = new Map(JSON.parse('[]'));
createSrcSidebar();"
);
assert_eq!(but_last_line(&template.to_string()), r"createSrcSidebar('[]');");
template.append(EscapedJson::from(OrderedJson::serialize("u").unwrap()).to_string());
assert_eq!(
but_last_line(&template.to_string()),
r#"var srcIndex = new Map(JSON.parse('["u"]'));
createSrcSidebar();"#
);
assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u"]');"#);
template.append(EscapedJson::from(OrderedJson::serialize("v").unwrap()).to_string());
assert_eq!(
but_last_line(&template.to_string()),
r#"var srcIndex = new Map(JSON.parse('["u","v"]'));
createSrcSidebar();"#
);
assert_eq!(but_last_line(&template.to_string()), r#"createSrcSidebar('["u","v"]');"#);
}

#[test]
Expand Down
6 changes: 2 additions & 4 deletions src/librustdoc/html/static/js/rustdoc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

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

/**
* A single parsed "atom" in a search query. For example,
*
*
* std::fmt::Formatter, Write -> Result<()>
* ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
* ┃ │ ┗ QueryElement { ┊
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/html/static/js/src-script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// From rust:
/* global srcIndex */

// Local js definitions:
/* global addClass, onEachLazy, removeClass, browserSupportsHistoryApi */
/* global updateLocalStorage, getVar, nonnull */
Expand Down Expand Up @@ -100,11 +97,15 @@ window.rustdocToggleSrcSidebar = () => {

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

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

let hasFoundFile = false;

Expand Down
3 changes: 0 additions & 3 deletions tests/rustdoc-gui/globals.goml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=sa'%3Bda'%3Bds"
wait-for: "#search-tabs"
assert-window-property-false: {"searchIndex": null}
assert-window-property: {"srcIndex": null}

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

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