@@ -2461,6 +2461,7 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec<String> {
2461
2461
}
2462
2462
2463
2463
const MAX_FULL_EXAMPLES : usize = 5 ;
2464
+ const NUM_VISIBLE_LINES : usize = 10 ;
2464
2465
2465
2466
/// Generates the HTML for example call locations generated via the --scrape-examples flag.
2466
2467
fn render_call_locations ( w : & mut Buffer , cx : & Context < ' _ > , def_id : DefId , item : & clean:: Item ) {
@@ -2480,10 +2481,10 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
2480
2481
w,
2481
2482
"<div class=\" docblock scraped-example-list\" >\
2482
2483
<span></span>
2483
- <h5 id=\" scraped-examples \" class=\" section-header\" >\
2484
- <a href=\" #{}\" >Examples found in repository</a>\
2484
+ <h5 id=\" {id} \" class=\" section-header\" >\
2485
+ <a href=\" #{id }\" >Examples found in repository</a>\
2485
2486
</h5>",
2486
- id
2487
+ id = id
2487
2488
) ;
2488
2489
2489
2490
// Generate the HTML for a single example, being the title and code block
@@ -2503,54 +2504,58 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
2503
2504
assert ! ( !call_data. locations. is_empty( ) ) ;
2504
2505
let min_loc =
2505
2506
call_data. locations . iter ( ) . min_by_key ( |loc| loc. enclosing_item . byte_span . 0 ) . unwrap ( ) ;
2506
- let ( byte_offset, _) = min_loc. enclosing_item . byte_span ;
2507
- let ( line_offset, _) = min_loc. enclosing_item . line_span ;
2508
- let byte_ceiling =
2509
- call_data. locations . iter ( ) . map ( |loc| loc. enclosing_item . byte_span . 1 ) . max ( ) . unwrap ( ) ;
2507
+ let byte_min = min_loc. enclosing_item . byte_span . 0 ;
2508
+ let line_min = min_loc. enclosing_item . line_span . 0 ;
2509
+ let max_loc =
2510
+ call_data. locations . iter ( ) . max_by_key ( |loc| loc. enclosing_item . byte_span . 1 ) . unwrap ( ) ;
2511
+ let byte_max = max_loc. enclosing_item . byte_span . 1 ;
2512
+ let line_max = max_loc. enclosing_item . line_span . 1 ;
2510
2513
2511
2514
// The output code is limited to that byte range.
2512
- let contents_subset = & contents[ ( byte_offset as usize ) ..( byte_ceiling as usize ) ] ;
2515
+ let contents_subset = & contents[ ( byte_min as usize ) ..( byte_max as usize ) ] ;
2513
2516
2514
2517
// The call locations need to be updated to reflect that the size of the program has changed.
2515
- // Specifically, the ranges are all subtracted by `byte_offset ` since that's the new zero point.
2518
+ // Specifically, the ranges are all subtracted by `byte_min ` since that's the new zero point.
2516
2519
let ( byte_ranges, line_ranges) : ( Vec < _ > , Vec < _ > ) = call_data
2517
2520
. locations
2518
2521
. iter ( )
2519
2522
. map ( |loc| {
2520
2523
let ( byte_lo, byte_hi) = loc. call_expr . byte_span ;
2521
2524
let ( line_lo, line_hi) = loc. call_expr . line_span ;
2522
- (
2523
- ( byte_lo - byte_offset, byte_hi - byte_offset) ,
2524
- ( line_lo - line_offset, line_hi - line_offset) ,
2525
- )
2525
+ ( ( byte_lo - byte_min, byte_hi - byte_min) , ( line_lo - line_min, line_hi - line_min) )
2526
2526
} )
2527
2527
. unzip ( ) ;
2528
2528
2529
2529
let ( init_min, init_max) = line_ranges[ 0 ] ;
2530
2530
let line_range = if init_min == init_max {
2531
- format ! ( "line {}" , init_min + line_offset + 1 )
2531
+ format ! ( "line {}" , init_min + line_min + 1 )
2532
2532
} else {
2533
- format ! ( "lines {}-{}" , init_min + line_offset + 1 , init_max + line_offset + 1 )
2533
+ format ! ( "lines {}-{}" , init_min + line_min + 1 , init_max + line_min + 1 )
2534
2534
} ;
2535
2535
2536
+ let needs_expansion = line_max - line_min > NUM_VISIBLE_LINES ;
2537
+
2536
2538
write ! (
2537
2539
w,
2538
- "<div class=\" scraped-example\" data-locs= \" {locations }\" data-offset =\" {offset }\" >\
2540
+ "<div class=\" scraped-example {expanded_cls }\" data-locs =\" {locations }\" >\
2539
2541
<div class=\" scraped-example-title\" >\
2540
- {name} (<a href=\" {root}{url}\" target= \" _blank \" >{line_range}</a>)\
2542
+ {name} (<a href=\" {root}{url}\" >{line_range}</a>)\
2541
2543
</div>\
2542
2544
<div class=\" code-wrapper\" >",
2543
2545
root = cx. root_path( ) ,
2544
2546
url = call_data. url,
2545
2547
name = call_data. display_name,
2546
2548
line_range = line_range,
2547
- offset = line_offset ,
2549
+ expanded_cls = if needs_expansion { "" } else { "expanded" } ,
2548
2550
// The locations are encoded as a data attribute, so they can be read
2549
2551
// later by the JS for interactions.
2550
2552
locations = serde_json:: to_string( & line_ranges) . unwrap( ) ,
2551
2553
) ;
2552
2554
write ! ( w, r#"<span class="prev">≺</span> <span class="next">≻</span>"# ) ;
2553
- write ! ( w, r#"<span class="expand">↕</span>"# ) ;
2555
+
2556
+ if needs_expansion {
2557
+ write ! ( w, r#"<span class="expand">↕</span>"# ) ;
2558
+ }
2554
2559
2555
2560
// Look for the example file in the source map if it exists, otherwise return a dummy span
2556
2561
let file_span = ( || {
@@ -2565,8 +2570,8 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
2565
2570
_ => false ,
2566
2571
} ) ?;
2567
2572
Some ( rustc_span:: Span :: with_root_ctxt (
2568
- file. start_pos + BytePos ( byte_offset ) ,
2569
- file. start_pos + BytePos ( byte_ceiling ) ,
2573
+ file. start_pos + BytePos ( byte_min ) ,
2574
+ file. start_pos + BytePos ( byte_max ) ,
2570
2575
) )
2571
2576
} ) ( )
2572
2577
. unwrap_or ( rustc_span:: DUMMY_SP ) ;
@@ -2584,8 +2589,8 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
2584
2589
file_span,
2585
2590
cx,
2586
2591
& root_path,
2587
- Some ( line_offset) ,
2588
2592
Some ( highlight:: DecorationInfo ( decoration_info) ) ,
2593
+ sources:: SourceContext :: Embedded { offset : line_min } ,
2589
2594
) ;
2590
2595
write ! ( w, "</div></div>" ) ;
2591
2596
@@ -2648,7 +2653,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
2648
2653
it. for_each ( |( _, call_data) | {
2649
2654
write ! (
2650
2655
w,
2651
- r#"<li><a href="{root}{url}" target="_blank" >{name}</a></li>"# ,
2656
+ r#"<li><a href="{root}{url}">{name}</a></li>"# ,
2652
2657
root = cx. root_path( ) ,
2653
2658
url = call_data. url,
2654
2659
name = call_data. display_name
0 commit comments