Skip to content
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

rustdoc: simplify the codeblock tooltip #101600

Merged
merged 1 commit into from
Sep 9, 2022
Merged
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
55 changes: 28 additions & 27 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,14 @@ pub(crate) fn render_example_with_highlighting(
tooltip: Tooltip,
playground_button: Option<&str>,
) {
let class = match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
};

if tooltip != Tooltip::None {
write!(
out,
"<div class='information'><div class='tooltip{}'{}>ⓘ</div></div>",
class,
if let Tooltip::Edition(edition_info) = tooltip {
format!(" data-edition=\"{}\"", edition_info)
} else {
String::new()
},
);
}

write_header(out, &format!("rust-example-rendered{}", class), None);
write_header(out, "rust-example-rendered", None, tooltip);
write_code(out, src, None, None);
write_footer(out, playground_button);
}

/// Highlights `src` as a macro, returning the HTML output.
pub(crate) fn render_macro_with_highlighting(src: &str, out: &mut Buffer) {
write_header(out, "macro", None);
write_header(out, "macro", None, Tooltip::None);
write_code(out, src, None, None);
write_footer(out, None);
}
Expand All @@ -93,20 +72,42 @@ pub(crate) fn render_source_with_highlighting(
href_context: HrefContext<'_, '_, '_>,
decoration_info: DecorationInfo,
) {
write_header(out, "", Some(line_numbers));
write_header(out, "", Some(line_numbers), Tooltip::None);
write_code(out, src, Some(href_context), Some(decoration_info));
write_footer(out, None);
}

fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>) {
fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, tooltip: Tooltip) {
write!(out, "<div class=\"example-wrap\">");

let tooltip_class = match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
};

if tooltip != Tooltip::None {
write!(
out,
"<div class='information'><div class='tooltip{}'{}>ⓘ</div></div>",
tooltip_class,
if let Tooltip::Edition(edition_info) = tooltip {
format!(" data-edition=\"{}\"", edition_info)
} else {
String::new()
},
);
}

if let Some(extra) = extra_content {
out.push_buffer(extra);
}
if class.is_empty() {
if class.is_empty() && tooltip_class.is_empty() {
write!(out, "<pre class=\"rust\">");
} else {
write!(out, "<pre class=\"rust {}\">", class);
write!(out, "<pre class=\"rust {class}{tooltip_class}\">");
}
write!(out, "<code>");
}
Expand Down
20 changes: 8 additions & 12 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@ img {
max-width: 100%;
}

li {
position: relative;
}

.source .content {
max-width: none;
overflow: visible;
Expand Down Expand Up @@ -657,7 +653,7 @@ h2.location a {
position: relative;
}

.docblock > :not(.information):not(.more-examples-toggle) {
.docblock > :not(.more-examples-toggle):not(.example-wrap) {
max-width: 100%;
overflow-x: auto;
}
Expand Down Expand Up @@ -1174,12 +1170,12 @@ pre.ignore {
border-left: 2px solid var(--codeblock-ignore-color);
}

pre.compile_fail:hover, .information:hover + .example-wrap pre.compile_fail,
pre.should_panic:hover, .information:hover + .example-wrap pre.should_panic {
.example-wrap:hover pre.compile_fail,
.example-wrap:hover pre.should_panic {
border-left: 2px solid var(--codeblock-error-hover-color);
}

pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
.example-wrap:hover pre.ignore {
border-left: 2px solid var(--codeblock-ignore-hover-color);
}

Expand All @@ -1192,12 +1188,12 @@ pre.ignore:hover, .information:hover + .example-wrap pre.ignore {
color: var(--codeblock-ignore-color);
}

.information > .compile_fail:hover,
.information > .should_panic:hover {
.example-wrap:hover .tooltip.compile_fail,
.example-wrap:hover .tooltip.should_panic {
color: var(--codeblock-error-hover-color);
}

.information > .ignore:hover {
.example-wrap:hover .tooltip.ignore {
color: var(--codeblock-ignore-hover-color);
}

Expand Down Expand Up @@ -1738,7 +1734,7 @@ in storage.js plus the media query with (max-width: 700px)
to prevent an overlay between the "collapse toggle" and the information tooltip.
However, it's not needed with smaller screen width because the doc/code block is always put
"one line" below. */
.docblock > .information:first-child > .tooltip {
.docblock > .example-wrap:first-child > .information > .tooltip {
margin-top: 16px;
}

Expand Down
23 changes: 2 additions & 21 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,8 @@ function loadCss(cssFileName) {

(function() {
// To avoid checking on "rustdoc-line-numbers" value on every loop...
let lineNumbersFunc = () => {};
if (getSettingValue("line-numbers") === "true") {
lineNumbersFunc = x => {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const count = x.textContent.split("\n").length;
const elems = [];
for (let i = 0; i < count; ++i) {
Expand All @@ -711,26 +710,8 @@ function loadCss(cssFileName) {
addClass(node, "line-number");
node.innerHTML = elems.join("\n");
x.parentNode.insertBefore(node, x);
};
});
}
onEachLazy(document.getElementsByClassName("rust-example-rendered"), e => {
if (hasClass(e, "compile_fail")) {
e.addEventListener("mouseover", function() {
this.parentElement.previousElementSibling.childNodes[0].style.color = "#f00";
});
e.addEventListener("mouseout", function() {
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
});
} else if (hasClass(e, "ignore")) {
e.addEventListener("mouseover", function() {
this.parentElement.previousElementSibling.childNodes[0].style.color = "#ff9200";
});
e.addEventListener("mouseout", function() {
this.parentElement.previousElementSibling.childNodes[0].style.color = "";
});
}
lineNumbersFunc(e);
});
}());

let oldSidebarScrollPosition = null;
Expand Down
8 changes: 4 additions & 4 deletions src/test/rustdoc-gui/check_info_sign_position.goml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ goto: file://|DOC_PATH|/test_docs/index.html
goto: ./fn.check_list_code_block.html
// If the codeblock is the first element of the docblock, the information tooltip must have
// have some top margin to avoid going over the toggle (the "[+]").
assert-css: (".docblock > .information > .compile_fail", { "margin-top": "16px" })
assert-css: (".docblock > .example-wrap > .information > .compile_fail", { "margin-top": "16px" })
// Checks that the other codeblocks don't have this top margin.
assert-css: ("ol > li > .information > .compile_fail", { "margin-top": "0px" })
assert-css: ("ol > li > .information > .ignore", { "margin-top": "0px" })
assert-css: (".docblock > .information > .ignore", { "margin-top": "0px" })
assert-css: ("ol > li > .example-wrap > .information > .compile_fail", { "margin-top": "0px" })
assert-css: ("ol > li > .example-wrap > .information > .ignore", { "margin-top": "0px" })
assert-css: (".docblock > .example-wrap > .information > .ignore", { "margin-top": "0px" })
36 changes: 18 additions & 18 deletions src/test/rustdoc-gui/codeblock-tooltip.goml
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,89 @@ reload:

// compile_fail block
assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .compile_fail"

assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})

// should_panic block
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .should_panic"

assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})

// ignore block
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})

move-cursor-to: ".docblock .information .ignore"

assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})


// Light theme.
local-storage: {"rustdoc-theme": "light"}
reload:

assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .compile_fail"

assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})

// should_panic block
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .should_panic"

assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})

// ignore block
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})

move-cursor-to: ".docblock .information .ignore"

assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})


// Ayu theme.
local-storage: {"rustdoc-theme": "ayu"}
reload:

assert-css: (".docblock .information .compile_fail", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .compile_fail"

assert-css: (".docblock .information .compile_fail", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.compile_fail", {"border-left": "2px solid rgb(255, 0, 0)"})

// should_panic block
assert-css: (".docblock .information .should_panic", {"color": "rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgba(255, 0, 0, 0.5)"})

move-cursor-to: ".docblock .information .should_panic"

assert-css: (".docblock .information .should_panic", {"color": "rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap .should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})
assert-css: (".docblock .example-wrap pre.should_panic", {"border-left": "2px solid rgb(255, 0, 0)"})

// ignore block
assert-css: (".docblock .information .ignore", {"color": "rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgba(255, 142, 0, 0.6)"})

move-cursor-to: ".docblock .information .ignore"

assert-css: (".docblock .information .ignore", {"color": "rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap .ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
assert-css: (".docblock .example-wrap pre.ignore", {"border-left": "2px solid rgb(255, 142, 0)"})
2 changes: 1 addition & 1 deletion src/test/rustdoc-gui/overflow-tooltip-information.goml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// have overflow and max-width CSS rules set because they create a bug in firefox on
// mac. For more information: https://github.com/rust-lang/rust/issues/89185
goto: file://|DOC_PATH|/test_docs/fn.foo.html
assert-css: (".docblock > .information", {
assert-css: (".docblock > .example-wrap > .information", {
"overflow-x": "visible",
"max-width": "none"
}, ALL)