Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra indent spaces for rust-playground link #121095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ use crate::lint::init_lints;
pub(crate) struct GlobalTestOptions {
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
pub(crate) no_crate_inject: bool,
/// Whether inserting extra indent spaces in code block,
/// default is `false`, only `true` for generating code link of Rust playground
pub(crate) insert_indent_space: bool,
/// Additional crate-level attributes to add to doctests.
pub(crate) attrs: Vec<String>,
}
Expand Down Expand Up @@ -221,7 +224,8 @@ pub(crate) fn run_tests(
fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
use rustc_ast_pretty::pprust;

let mut opts = GlobalTestOptions { no_crate_inject: false, attrs: Vec::new() };
let mut opts =
GlobalTestOptions { no_crate_inject: false, attrs: Vec::new(), insert_indent_space: false };

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

prog.extend([&main_pre, everything_else, &main_post].iter().cloned());
// add extra 4 spaces for each line to offset the code block
let content = if opts.insert_indent_space {
everything_else
.lines()
.map(|line| format!(" {}", line))
.collect::<Vec<String>>()
.join("\n")
} else {
everything_else.to_string()
};
prog.extend([&main_pre, content.as_str(), &main_post].iter().cloned());
}

debug!("final doctest:\n{prog}");
Expand Down
44 changes: 43 additions & 1 deletion src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ assert_eq!(2+2, 4);
fn make_test_no_crate_inject() {
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
// adding it anyway.
let opts = GlobalTestOptions { no_crate_inject: true, attrs: vec![] };
let opts =
GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_indent_space: false };
let input = "use asdf::qwop;
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand Down Expand Up @@ -302,3 +303,44 @@ assert_eq!(2+2, 4);
make_test(input, None, false, &opts, DEFAULT_EDITION, Some("_some_unique_name"));
assert_eq!((output, len), (expected, 2));
}

#[test]
fn make_test_insert_extra_space() {
// will insert indent spaces in the code block if `insert_indent_space` is true
let opts =
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
let input = "use std::*;
assert_eq!(2+2, 4);
eprintln!(\"hello anan\");
";
let expected = "#![allow(unused)]
fn main() {
use std::*;
assert_eq!(2+2, 4);
eprintln!(\"hello anan\");
}"
.to_string();
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
assert_eq!((output, len), (expected, 2));
}

#[test]
fn make_test_insert_extra_space_fn_main() {
// if input already has a fn main, it should insert a space before it
let opts =
GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
let input = "use std::*;
fn main() {
assert_eq!(2+2, 4);
eprintln!(\"hello anan\");
}";
let expected = "#![allow(unused)]
use std::*;
fn main() {
assert_eq!(2+2, 4);
eprintln!(\"hello anan\");
}"
.to_string();
let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
assert_eq!((output, len), (expected, 1));
}
7 changes: 5 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use std::str::{self, CharIndices};

use crate::clean::RenderedLink;
use crate::doctest;
use crate::doctest::GlobalTestOptions;
use crate::html::escape::Escape;
use crate::html::format::Buffer;
use crate::html::highlight;
Expand Down Expand Up @@ -302,8 +303,10 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
.intersperse("\n".into())
.collect::<String>();
let krate = krate.as_ref().map(|s| s.as_str());
let (test, _, _) =
doctest::make_test(&test, krate, false, &Default::default(), edition, None);

let mut opts: GlobalTestOptions = Default::default();
opts.insert_indent_space = true;
let (test, _, _) = doctest::make_test(&test, krate, false, &opts, edition, None);
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };

let test_escaped = small_url_encode(test);
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/playground-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
pub fn dummy() {}

// ensure that `extern crate foo;` was inserted into code snips automatically:
// @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"
// @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"
2 changes: 1 addition & 1 deletion tests/rustdoc/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
//! }
//! ```

// @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"
// @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"
// @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"
// @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"