Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7e81b0a

Browse files
committed
Improve Rustdoc UI for scraped examples with multiline arguments, fix
overflow in line numbers
1 parent 6252304 commit 7e81b0a

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,30 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
27172717
// The output code is limited to that byte range.
27182718
let contents_subset = &contents[(byte_min as usize)..(byte_max as usize)];
27192719

2720+
// Given a call-site range, return the set of sub-ranges that exclude leading whitespace
2721+
// when the range spans multiple lines.
2722+
let strip_leading_whitespace = |(lo, hi): (u32, u32)| -> Vec<(u32, u32)> {
2723+
let contents_range = &contents_subset[(lo as usize)..(hi as usize)];
2724+
let mut ignoring_whitespace = false;
2725+
let mut ranges = Vec::new();
2726+
let mut cur_lo = 0;
2727+
for (idx, chr) in contents_range.char_indices() {
2728+
let idx = idx as u32;
2729+
if ignoring_whitespace {
2730+
if !chr.is_whitespace() {
2731+
ignoring_whitespace = false;
2732+
cur_lo = idx;
2733+
}
2734+
} else if chr == '\n' {
2735+
ranges.push((lo + cur_lo, lo + idx));
2736+
cur_lo = idx;
2737+
ignoring_whitespace = true;
2738+
}
2739+
}
2740+
ranges.push((lo + cur_lo, hi));
2741+
ranges
2742+
};
2743+
27202744
// The call locations need to be updated to reflect that the size of the program has changed.
27212745
// Specifically, the ranges are all subtracted by `byte_min` since that's the new zero point.
27222746
let (mut byte_ranges, line_ranges): (Vec<_>, Vec<_>) = call_data
@@ -2726,10 +2750,12 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
27262750
let (byte_lo, byte_hi) = loc.call_expr.byte_span;
27272751
let (line_lo, line_hi) = loc.call_expr.line_span;
27282752
let byte_range = (byte_lo - byte_min, byte_hi - byte_min);
2753+
let byte_ranges = strip_leading_whitespace(byte_range);
2754+
27292755
let line_range = (line_lo - line_min, line_hi - line_min);
27302756
let (line_url, line_title) = link_to_loc(call_data, loc);
27312757

2732-
(byte_range, (line_range, line_url, line_title))
2758+
(byte_ranges, (line_range, line_url, line_title))
27332759
})
27342760
.unzip();
27352761

@@ -2784,8 +2810,8 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
27842810
let root_path = vec!["../"; cx.current.len() - 1].join("");
27852811

27862812
let mut decoration_info = FxHashMap::default();
2787-
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
2788-
decoration_info.insert("highlight", byte_ranges);
2813+
decoration_info.insert("highlight focus", byte_ranges.remove(0));
2814+
decoration_info.insert("highlight", byte_ranges.into_iter().flatten().collect());
27892815

27902816
sources::print_src(
27912817
w,

src/librustdoc/html/static/css/rustdoc.css

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,17 +2038,16 @@ details.rustdoc-toggle[open] > summary.hideme::after {
20382038
font-family: 'Fira Sans';
20392039
}
20402040

2041-
.scraped-example:not(.expanded) .code-wrapper pre.line-numbers {
2042-
overflow: hidden;
2043-
max-height: 240px;
2044-
}
2045-
2046-
.scraped-example:not(.expanded) .code-wrapper .example-wrap pre.rust {
2041+
.scraped-example:not(.expanded) .code-wrapper pre {
20472042
overflow-y: hidden;
20482043
max-height: 240px;
20492044
padding-bottom: 0;
20502045
}
20512046

2047+
.scraped-example:not(.expanded) .code-wrapper pre.line-numbers {
2048+
overflow-x: hidden;
2049+
}
2050+
20522051
.scraped-example .code-wrapper .prev {
20532052
position: absolute;
20542053
top: 0.25em;

src/librustdoc/html/static/js/scrape-examples.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
/* global addClass, hasClass, removeClass, onEach */
22

33
(function () {
4-
// Scroll code block to put the given code location in the middle of the viewer
4+
// Number of lines shown when code viewer is not expanded
5+
const MAX_LINES = 10;
6+
7+
// Scroll code block to the given code location
58
function scrollToLoc(elt, loc) {
6-
var wrapper = elt.querySelector(".code-wrapper");
7-
var halfHeight = wrapper.offsetHeight / 2;
89
var lines = elt.querySelector('.line-numbers');
9-
var offsetMid = (lines.children[loc[0]].offsetTop
10-
+ lines.children[loc[1]].offsetTop) / 2;
11-
var scrollOffset = offsetMid - halfHeight;
10+
var scrollOffset;
11+
12+
// If the block is greater than the size of the viewer,
13+
// then scroll to the top of the block. Otherwise scroll
14+
// to the middle of the block.
15+
if (loc[1] - loc[0] > MAX_LINES) {
16+
var line = Math.max(0, loc[0] - 1);
17+
scrollOffset = lines.children[line].offsetTop;
18+
} else {
19+
var wrapper = elt.querySelector(".code-wrapper");
20+
var halfHeight = wrapper.offsetHeight / 2;
21+
var offsetMid = (lines.children[loc[0]].offsetTop
22+
+ lines.children[loc[1]].offsetTop) / 2;
23+
scrollOffset = offsetMid - halfHeight;
24+
}
25+
1226
lines.scrollTo(0, scrollOffset);
1327
elt.querySelector(".rust").scrollTo(0, scrollOffset);
1428
}

0 commit comments

Comments
 (0)