Skip to content

Commit 7c1656a

Browse files
authored
Unrolled build for rust-lang#121095
Rollup merge of rust-lang#121095 - chenyukang:yukang-fix-120998-rust-playground-link, r=GuillaumeGomez Add extra indent spaces for rust-playground link Fixes rust-lang#120998 Seems add `rustfmt` for this is somehow too heavy, only adding indent spaces at the starting of each line of code seems good enough.
2 parents a447249 + bd546fb commit 7c1656a

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/librustdoc/doctest.rs

+16-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_indent_space: bool,
4346
/// Additional crate-level attributes to add to doctests.
4447
pub(crate) attrs: Vec<String>,
4548
}
@@ -221,7 +224,8 @@ 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 =
228+
GlobalTestOptions { no_crate_inject: false, attrs: Vec::new(), insert_indent_space: false };
225229

226230
let test_attrs: Vec<_> = attrs
227231
.iter()
@@ -725,7 +729,17 @@ pub(crate) fn make_test(
725729
// /// ``` <- end of the inner main
726730
line_offset += 1;
727731

728-
prog.extend([&main_pre, everything_else, &main_post].iter().cloned());
732+
// add extra 4 spaces for each line to offset the code block
733+
let content = if opts.insert_indent_space {
734+
everything_else
735+
.lines()
736+
.map(|line| format!(" {}", line))
737+
.collect::<Vec<String>>()
738+
.join("\n")
739+
} else {
740+
everything_else.to_string()
741+
};
742+
prog.extend([&main_pre, content.as_str(), &main_post].iter().cloned());
729743
}
730744

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

src/librustdoc/doctest/tests.rs

+43-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_indent_space: false };
5758
let input = "use asdf::qwop;
5859
assert_eq!(2+2, 4);";
5960
let expected = "#![allow(unused)]
@@ -302,3 +303,44 @@ 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+
// will insert indent spaces in the code block if `insert_indent_space` is true
310+
let opts =
311+
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
312+
let input = "use std::*;
313+
assert_eq!(2+2, 4);
314+
eprintln!(\"hello anan\");
315+
";
316+
let expected = "#![allow(unused)]
317+
fn main() {
318+
use std::*;
319+
assert_eq!(2+2, 4);
320+
eprintln!(\"hello anan\");
321+
}"
322+
.to_string();
323+
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
324+
assert_eq!((output, len), (expected, 2));
325+
}
326+
327+
#[test]
328+
fn make_test_insert_extra_space_fn_main() {
329+
// if input already has a fn main, it should insert a space before it
330+
let opts =
331+
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
332+
let input = "use std::*;
333+
fn main() {
334+
assert_eq!(2+2, 4);
335+
eprintln!(\"hello anan\");
336+
}";
337+
let expected = "#![allow(unused)]
338+
use std::*;
339+
fn main() {
340+
assert_eq!(2+2, 4);
341+
eprintln!(\"hello anan\");
342+
}"
343+
.to_string();
344+
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
345+
assert_eq!((output, len), (expected, 1));
346+
}

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_indent_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)