Skip to content

Commit 9f86522

Browse files
committed
add extra indent spaces for rust-playground link
1 parent 340bb19 commit 9f86522

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

src/librustdoc/doctest.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ use crate::lint::init_lints;
4040
pub(crate) struct GlobalTestOptions {
4141
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
4242
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,
4346
/// Additional crate-level attributes to add to doctests.
4447
pub(crate) attrs: Vec<String>,
4548
}
@@ -221,7 +224,11 @@ pub(crate) fn run_tests(
221224
fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
222225
use rustc_ast_pretty::pprust;
223226

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+
};
225232

226233
let test_attrs: Vec<_> = attrs
227234
.iter()
@@ -725,7 +732,17 @@ pub(crate) fn make_test(
725732
// /// ``` <- end of the inner main
726733
line_offset += 1;
727734

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());
729746
}
730747

731748
debug!("final doctest:\n{prog}");

src/librustdoc/doctest/tests.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ assert_eq!(2+2, 4);
5353
fn make_test_no_crate_inject() {
5454
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
5555
// adding it anyway.
56-
let opts = GlobalTestOptions { no_crate_inject: true, attrs: vec![] };
56+
let opts =
57+
GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_extra_ident_space: false };
5758
let input = "use asdf::qwop;
5859
assert_eq!(2+2, 4);";
5960
let expected = "#![allow(unused)]
@@ -302,3 +303,22 @@ assert_eq!(2+2, 4);
302303
make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));
303304
assert_eq!((output, len), (expected, 2));
304305
}
306+
307+
#[test]
308+
fn make_test_insert_extra_space() {
309+
let opts =
310+
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_extra_ident_space: true };
311+
let input = "use std::*;
312+
assert_eq!(2+2, 4);
313+
eprintln!(\"hello anan\");
314+
";
315+
let expected = "#![allow(unused)]
316+
fn main() {
317+
use std::*;
318+
assert_eq!(2+2, 4);
319+
eprintln!(\"hello anan\");
320+
}"
321+
.to_string();
322+
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
323+
assert_eq!((output, len), (expected, 2));
324+
}

src/librustdoc/html/markdown.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use std::str::{self, CharIndices};
4545

4646
use crate::clean::RenderedLink;
4747
use crate::doctest;
48+
use crate::doctest::GlobalTestOptions;
4849
use crate::html::escape::Escape;
4950
use crate::html::format::Buffer;
5051
use crate::html::highlight;
@@ -302,8 +303,10 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
302303
.intersperse("\n".into())
303304
.collect::<String>();
304305
let krate = krate.as_ref().map(|s| s.as_str());
305-
let (test, _, _) =
306-
doctest::make_test(&test, krate, false, &Default::default(), edition, None);
306+
307+
let mut opts: GlobalTestOptions = Default::default();
308+
opts.insert_extra_ident_space = true;
309+
let (test, _, _) = doctest::make_test(&test, krate, false, &opts, edition, None);
307310
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };
308311

309312
let test_escaped = small_url_encode(test);

tests/rustdoc/playground-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
pub fn dummy() {}
1111

1212
// ensure that `extern crate foo;` was inserted into code snips automatically:
13-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0A%23%5Ballow(unused_extern_crates)%5D%0Aextern+crate+r%23foo;%0Afn+main()+%7B%0Ause+foo::dummy;%0Adummy();%0A%7D&edition=2015"]' "Run"
13+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0A%23%5Ballow(unused_extern_crates)%5D%0Aextern+crate+r%23foo;%0Afn+main()+%7B%0A++++use+foo::dummy;%0A++++dummy();%0A%7D&edition=2015"]' "Run"

tests/rustdoc/playground.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
//! }
2323
//! ```
2424
25-
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn+main()+%7B%0Aprintln!(%22Hello,+world!%22);%0A%7D&edition=2015"]' "Run"
25+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&edition=2015"]' "Run"
2626
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&edition=2015"]' "Run"
2727
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(something)%5D%0A%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&version=nightly&edition=2015"]' "Run"

0 commit comments

Comments
 (0)