Skip to content

Commit 8f4c69a

Browse files
authored
Unrolled build for rust-lang#129796
Rollup merge of rust-lang#129796 - GuillaumeGomez:unify-code-examples, r=notriddle Unify scraped examples with other code examples Fixes rust-lang#129763. This first PR both fixes rust-lang#129763 but also unifies buttons display for code examples: ![image](https://github.com/user-attachments/assets/c8475945-dcc3-4c25-8d7d-1659f85301c8) You can test it [here](https://rustdoc.crud.net/imperio/unify-code-examples/doc/scrape_examples/fn.test.html) and [here](https://rustdoc.crud.net/imperio/unify-code-examples/doc/scrape_examples/fn.test_many.html). I'm planning to send a follow-up to make the buttons generated in JS directly (or I can do it in this PR directly if you prefer). cc ```@willcrichton``` r? ```@notriddle```
2 parents eb33b43 + 7157f98 commit 8f4c69a

14 files changed

+380
-156
lines changed

src/librustdoc/html/render/mod.rs

+9-24
Original file line numberDiff line numberDiff line change
@@ -2455,28 +2455,6 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
24552455
let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES;
24562456
let locations_encoded = serde_json::to_string(&line_ranges).unwrap();
24572457

2458-
write!(
2459-
&mut w,
2460-
"<div class=\"scraped-example {expanded_cls}\" data-locs=\"{locations}\">\
2461-
<div class=\"scraped-example-title\">\
2462-
{name} (<a href=\"{url}\">{title}</a>)\
2463-
</div>\
2464-
<div class=\"code-wrapper\">",
2465-
expanded_cls = if needs_expansion { "" } else { "expanded" },
2466-
name = call_data.display_name,
2467-
url = init_url,
2468-
title = init_title,
2469-
// The locations are encoded as a data attribute, so they can be read
2470-
// later by the JS for interactions.
2471-
locations = Escape(&locations_encoded)
2472-
)
2473-
.unwrap();
2474-
2475-
if line_ranges.len() > 1 {
2476-
w.write_str(r#"<button class="prev">&pr;</button> <button class="next">&sc;</button>"#)
2477-
.unwrap();
2478-
}
2479-
24802458
// Look for the example file in the source map if it exists, otherwise return a dummy span
24812459
let file_span = (|| {
24822460
let source_map = tcx.sess.source_map();
@@ -2507,9 +2485,16 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
25072485
cx,
25082486
&cx.root_path(),
25092487
highlight::DecorationInfo(decoration_info),
2510-
sources::SourceContext::Embedded { offset: line_min, needs_expansion },
2488+
sources::SourceContext::Embedded(sources::ScrapedInfo {
2489+
needs_prev_next_buttons: line_ranges.len() > 1,
2490+
needs_expansion,
2491+
offset: line_min,
2492+
name: &call_data.display_name,
2493+
url: init_url,
2494+
title: init_title,
2495+
locations: locations_encoded,
2496+
}),
25112497
);
2512-
w.write_str("</div></div>").unwrap();
25132498

25142499
true
25152500
};

src/librustdoc/html/sources.rs

+38-19
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,34 @@ where
290290
}
291291
}
292292

293-
pub(crate) enum SourceContext {
293+
pub(crate) struct ScrapedInfo<'a> {
294+
pub(crate) offset: usize,
295+
pub(crate) needs_prev_next_buttons: bool,
296+
pub(crate) name: &'a str,
297+
pub(crate) url: &'a str,
298+
pub(crate) title: &'a str,
299+
pub(crate) locations: String,
300+
pub(crate) needs_expansion: bool,
301+
}
302+
303+
#[derive(Template)]
304+
#[template(path = "scraped_source.html")]
305+
struct ScrapedSource<'a, Code: std::fmt::Display> {
306+
info: ScrapedInfo<'a>,
307+
lines: RangeInclusive<usize>,
308+
code_html: Code,
309+
}
310+
311+
#[derive(Template)]
312+
#[template(path = "source.html")]
313+
struct Source<Code: std::fmt::Display> {
314+
lines: RangeInclusive<usize>,
315+
code_html: Code,
316+
}
317+
318+
pub(crate) enum SourceContext<'a> {
294319
Standalone,
295-
Embedded { offset: usize, needs_expansion: bool },
320+
Embedded(ScrapedInfo<'a>),
296321
}
297322

298323
/// Wrapper struct to render the source code of a file. This will do things like
@@ -304,23 +329,8 @@ pub(crate) fn print_src(
304329
context: &Context<'_>,
305330
root_path: &str,
306331
decoration_info: highlight::DecorationInfo,
307-
source_context: SourceContext,
332+
source_context: SourceContext<'_>,
308333
) {
309-
#[derive(Template)]
310-
#[template(path = "source.html")]
311-
struct Source<Code: std::fmt::Display> {
312-
embedded: bool,
313-
needs_expansion: bool,
314-
lines: RangeInclusive<usize>,
315-
code_html: Code,
316-
}
317-
let lines = s.lines().count();
318-
let (embedded, needs_expansion, lines) = match source_context {
319-
SourceContext::Standalone => (false, false, 1..=lines),
320-
SourceContext::Embedded { offset, needs_expansion } => {
321-
(true, needs_expansion, (1 + offset)..=(lines + offset))
322-
}
323-
};
324334
let current_href = context
325335
.href_from_span(clean::Span::new(file_span), false)
326336
.expect("only local crates should have sources emitted");
@@ -333,5 +343,14 @@ pub(crate) fn print_src(
333343
);
334344
Ok(())
335345
});
336-
Source { embedded, needs_expansion, lines, code_html: code }.render_into(&mut writer).unwrap();
346+
let lines = s.lines().count();
347+
match source_context {
348+
SourceContext::Standalone => {
349+
Source { lines: (1..=lines), code_html: code }.render_into(&mut writer).unwrap()
350+
}
351+
SourceContext::Embedded(info) => {
352+
let lines = (1 + info.offset)..=(lines + info.offset);
353+
ScrapedSource { info, lines, code_html: code }.render_into(&mut writer).unwrap();
354+
}
355+
};
337356
}

src/librustdoc/html/static/css/noscript.css

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ nav.sub {
5959
--copy-path-button-color: #999;
6060
--copy-path-img-filter: invert(50%);
6161
--copy-path-img-hover-filter: invert(35%);
62+
--code-example-button-color: #7f7f7f;
63+
--code-example-button-hover-color: #595959;
6264
--codeblock-error-hover-color: rgb(255, 0, 0);
6365
--codeblock-error-color: rgba(255, 0, 0, .5);
6466
--codeblock-ignore-hover-color: rgb(255, 142, 0);
@@ -162,6 +164,8 @@ nav.sub {
162164
--copy-path-button-color: #999;
163165
--copy-path-img-filter: invert(50%);
164166
--copy-path-img-hover-filter: invert(65%);
167+
--code-example-button-color: #7f7f7f;
168+
--code-example-button-hover-color: #a5a5a5;
165169
--codeblock-error-hover-color: rgb(255, 0, 0);
166170
--codeblock-error-color: rgba(255, 0, 0, .5);
167171
--codeblock-ignore-hover-color: rgb(255, 142, 0);

0 commit comments

Comments
 (0)