Skip to content

Commit

Permalink
Rollup merge of #87035 - GuillaumeGomez:fix-implementors-display, r=n…
Browse files Browse the repository at this point in the history
…otriddle

Fix implementors display

Part of #86632.

This PR does a few things:
 * It fixes of the JS rendered implementors.
 * It generates anchors for JS rendered implementors to make it coherent with the others.
 * It adds a test to ensure that we won't have the same issue again.
 * It changes the way we render the rustdoc-gui crates to simplify it a bit and also to allow to have dependencies without going through compiletest.

Before:

![Screenshot from 2021-07-10 13-30-13](https://user-images.githubusercontent.com/3050060/125174172-b4048700-e1c3-11eb-8f0e-c46081371d4f.png)

After:

![Screenshot from 2021-07-10 21-11-15](https://user-images.githubusercontent.com/3050060/125174173-b49d1d80-e1c3-11eb-8740-1dbbff70c2eb.png)

I plan to add the `[src]` links in another PR because this one is already big enough.

cc `@Mark-Simulacrum` (for the bootstrap changes)

r? `@Nemo157`
  • Loading branch information
JohnTitor committed Jul 12, 2021
2 parents 2d9a038 + bd81949 commit fab45bf
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ __pycache__/
**node_modules
**package-lock.json

## Rustdoc GUI tests
src/test/rustdoc-gui/src/**.lock

# Before adding new lines, see the comment at the top.
26 changes: 12 additions & 14 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,27 +907,25 @@ impl Step for RustdocGUI {
// We remove existing folder to be sure there won't be artifacts remaining.
let _ = fs::remove_dir_all(&out_dir);

let mut nb_generated = 0;
let src_path = "src/test/rustdoc-gui/src";
// We generate docs for the libraries present in the rustdoc-gui's src folder.
let libs_dir = builder.build.src.join("src/test/rustdoc-gui/src");
for entry in libs_dir.read_dir().expect("read_dir call failed") {
let entry = entry.expect("invalid entry");
let path = entry.path();
if path.extension().map(|e| e == "rs").unwrap_or(false) {
let mut command = builder.rustdoc_cmd(self.compiler);
command.arg(path).arg("-o").arg(&out_dir);
builder.run(&mut command);
nb_generated += 1;
}
}
assert!(nb_generated > 0, "no documentation was generated...");
let mut cargo = Command::new(&builder.initial_cargo);
cargo
.arg("doc")
.arg("--workspace")
.arg("--target-dir")
.arg(&out_dir)
.env("RUSTDOC", builder.rustdoc(self.compiler))
.env("RUSTC", builder.rustc(self.compiler))
.current_dir(&builder.build.src.join(src_path));
builder.run(&mut cargo);

// We now run GUI tests.
let mut command = Command::new(&nodejs);
command
.arg(builder.build.src.join("src/tools/rustdoc-gui/tester.js"))
.arg("--doc-folder")
.arg(out_dir)
.arg(out_dir.join("doc"))
.arg("--tests-folder")
.arg(builder.build.src.join("src/test/rustdoc-gui"));
for path in &builder.paths {
Expand Down
18 changes: 14 additions & 4 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ function hideThemeButtonState() {
});
}

var currentNbImpls = implementors.getElementsByClassName("impl").length;
var traitName = document.querySelector("h1.fqn > .in-band > .trait").textContent;
var baseIdName = "impl-" + traitName + "-";
var libs = Object.getOwnPropertyNames(imp);
for (var i = 0, llength = libs.length; i < llength; ++i) {
if (libs[i] === window.currentCrate) { continue; }
Expand All @@ -705,6 +708,7 @@ function hideThemeButtonState() {

var code = document.createElement("code");
code.innerHTML = struct.text;
addClass(code, "in-band");

onEachLazy(code.getElementsByTagName("a"), function(elem) {
var href = elem.getAttribute("href");
Expand All @@ -714,12 +718,18 @@ function hideThemeButtonState() {
}
});

var display = document.createElement("h3");
var currentId = baseIdName + currentNbImpls;
var anchor = document.createElement("a");
anchor.href = "#" + currentId;
addClass(anchor, "anchor");

var display = document.createElement("div");
display.id = currentId;
addClass(display, "impl");
display.innerHTML = "<span class=\"in-band\"><table class=\"table-display\">" +
"<tbody><tr><td><code>" + code.outerHTML + "</code></td><td></td></tr>" +
"</tbody></table></span>";
display.appendChild(anchor);
display.appendChild(code);
list.appendChild(display);
currentNbImpls += 1;
}
}
};
Expand Down
16 changes: 16 additions & 0 deletions src/test/rustdoc-gui/implementors.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The goal of this test is to check that the external trait implementors, generated with JS,
// have the same display than the "local" ones.
goto: file://|DOC_PATH|/implementors/trait.Whatever.html
assert: "#implementors-list"
// There are supposed to be two implementors listed.
assert-count: ("#implementors-list > .impl", 2)
// Now we check that both implementors have an anchor, an ID and a similar DOM.
assert: ("#implementors-list > .impl:nth-child(1) > a.anchor")
assert-attribute: ("#implementors-list > .impl:nth-child(1)", {"id": "impl-Whatever"})
assert-attribute: ("#implementors-list > .impl:nth-child(1) > a.anchor", {"href": "#impl-Whatever"})
assert: "#implementors-list > .impl:nth-child(1) > code.in-band"

assert: ("#implementors-list > .impl:nth-child(2) > a.anchor")
assert-attribute: ("#implementors-list > .impl:nth-child(2)", {"id": "impl-Whatever-1"})
assert-attribute: ("#implementors-list > .impl:nth-child(2) > a.anchor", {"href": "#impl-Whatever-1"})
assert: "#implementors-list > .impl:nth-child(2) > code.in-band"
18 changes: 18 additions & 0 deletions src/test/rustdoc-gui/src/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "implementors"
version = "0.1.0"

[[package]]
name = "lib2"
version = "0.1.0"
dependencies = [
"implementors",
]

[[package]]
name = "test_docs"
version = "0.1.0"
6 changes: 6 additions & 0 deletions src/test/rustdoc-gui/src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"test_docs",
"lib2",
"implementors",
]
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/implementors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "implementors"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/implementors/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub trait Whatever {
fn method() {}
}

pub struct Struct;

impl Whatever for Struct {}
10 changes: 10 additions & 0 deletions src/test/rustdoc-gui/src/lib2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "lib2"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"

[dependencies]
implementors = { path = "../implementors" }
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ impl Trait for Foo {
type X = u32;
const Y: u32 = 0;
}

impl implementors::Whatever for Foo {}
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/lib2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/src/test_docs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_docs"
version = "0.1.0"
edition = "2018"

[lib]
path = "lib.rs"
File renamed without changes.

0 comments on commit fab45bf

Please sign in to comment.