@@ -40,6 +40,9 @@ use crate::lint::init_lints;
40
40
pub ( crate ) struct GlobalTestOptions {
41
41
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
42
42
pub ( crate ) no_crate_inject : bool ,
43
+ /// Whether inserting extra indent spaces in code block,
44
+ /// default is `false`, only `true` for generating code link of Rust playground
45
+ pub ( crate ) insert_extra_ident_space : bool ,
43
46
/// Additional crate-level attributes to add to doctests.
44
47
pub ( crate ) attrs : Vec < String > ,
45
48
}
@@ -221,7 +224,11 @@ pub(crate) fn run_tests(
221
224
fn scrape_test_config ( attrs : & [ ast:: Attribute ] ) -> GlobalTestOptions {
222
225
use rustc_ast_pretty:: pprust;
223
226
224
- let mut opts = GlobalTestOptions { no_crate_inject : false , attrs : Vec :: new ( ) } ;
227
+ let mut opts = GlobalTestOptions {
228
+ no_crate_inject : false ,
229
+ attrs : Vec :: new ( ) ,
230
+ insert_extra_ident_space : false ,
231
+ } ;
225
232
226
233
let test_attrs: Vec < _ > = attrs
227
234
. iter ( )
@@ -725,7 +732,17 @@ pub(crate) fn make_test(
725
732
// /// ``` <- end of the inner main
726
733
line_offset += 1 ;
727
734
728
- prog. extend ( [ & main_pre, everything_else, & main_post] . iter ( ) . cloned ( ) ) ;
735
+ // add extra 4 spaces for each line to offset the code block
736
+ let content = if opts. insert_extra_ident_space {
737
+ everything_else
738
+ . lines ( )
739
+ . map ( |line| format ! ( " {}" , line) )
740
+ . collect :: < Vec < String > > ( )
741
+ . join ( "\n " )
742
+ } else {
743
+ everything_else. to_string ( )
744
+ } ;
745
+ prog. extend ( [ & main_pre, content. as_str ( ) , & main_post] . iter ( ) . cloned ( ) ) ;
729
746
}
730
747
731
748
debug ! ( "final doctest:\n {prog}" ) ;
0 commit comments