Rollup merge of #87035 - GuillaumeGomez:fix-implementors-display, r=notriddle

Fix implementors display

Part of https://github.com/rust-lang/rust/issues/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`
This commit is contained in:
Yuki Okushi 2021-07-13 08:54:33 +09:00 committed by GitHub
commit fab45bf485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 109 additions and 18 deletions

3
.gitignore vendored
View File

@ -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.

View File

@ -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 {

View File

@ -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; }
@ -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");
@ -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;
}
}
};

View File

@ -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"

View File

@ -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"

View File

@ -0,0 +1,6 @@
[workspace]
members = [
"test_docs",
"lib2",
"implementors",
]

View File

@ -0,0 +1,7 @@
[package]
name = "implementors"
version = "0.1.0"
edition = "2018"
[lib]
path = "lib.rs"

View File

@ -0,0 +1,7 @@
pub trait Whatever {
fn method() {}
}
pub struct Struct;
impl Whatever for Struct {}

View File

@ -0,0 +1,10 @@
[package]
name = "lib2"
version = "0.1.0"
edition = "2018"
[lib]
path = "lib.rs"
[dependencies]
implementors = { path = "../implementors" }

View File

@ -31,3 +31,5 @@ impl Trait for Foo {
type X = u32;
const Y: u32 = 0;
}
impl implementors::Whatever for Foo {}

View File

@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -0,0 +1,7 @@
[package]
name = "test_docs"
version = "0.1.0"
edition = "2018"
[lib]
path = "lib.rs"