-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #78400 - GuillaumeGomez:fix-unindent, r=jyn514
Fix unindent in doc comments Fixes #70732 r? ``@jyn514``
- Loading branch information
Showing
4 changed files
with
163 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,63 @@ | ||
use super::*; | ||
use rustc_span::source_map::DUMMY_SP; | ||
|
||
fn create_doc_fragment(s: &str) -> Vec<DocFragment> { | ||
vec![DocFragment { | ||
line: 0, | ||
span: DUMMY_SP, | ||
parent_module: None, | ||
doc: s.to_string(), | ||
kind: DocFragmentKind::SugaredDoc, | ||
}] | ||
} | ||
|
||
#[track_caller] | ||
fn run_test(input: &str, expected: &str) { | ||
let mut s = create_doc_fragment(input); | ||
unindent_fragments(&mut s); | ||
assert_eq!(s[0].doc, expected); | ||
} | ||
|
||
#[test] | ||
fn should_unindent() { | ||
let s = " line1\n line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\nline2"); | ||
run_test(" line1\n line2", "line1\nline2"); | ||
} | ||
|
||
#[test] | ||
fn should_unindent_multiple_paragraphs() { | ||
let s = " line1\n\n line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\n\nline2"); | ||
run_test(" line1\n\n line2", "line1\n\nline2"); | ||
} | ||
|
||
#[test] | ||
fn should_leave_multiple_indent_levels() { | ||
// Line 2 is indented another level beyond the | ||
// base indentation and should be preserved | ||
let s = " line1\n\n line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\n\n line2"); | ||
run_test(" line1\n\n line2", "line1\n\n line2"); | ||
} | ||
|
||
#[test] | ||
fn should_ignore_first_line_indent() { | ||
// The first line of the first paragraph may not be indented as | ||
// far due to the way the doc string was written: | ||
// | ||
// #[doc = "Start way over here | ||
// and continue here"] | ||
let s = "line1\n line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\nline2"); | ||
run_test("line1\n line2", "line1\n line2"); | ||
} | ||
|
||
#[test] | ||
fn should_not_ignore_first_line_indent_in_a_single_line_para() { | ||
let s = "line1\n\n line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\n\n line2"); | ||
run_test("line1\n\n line2", "line1\n\n line2"); | ||
} | ||
|
||
#[test] | ||
fn should_unindent_tabs() { | ||
let s = "\tline1\n\tline2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\nline2"); | ||
run_test("\tline1\n\tline2", "line1\nline2"); | ||
} | ||
|
||
#[test] | ||
fn should_trim_mixed_indentation() { | ||
let s = "\t line1\n\t line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\nline2"); | ||
|
||
let s = " \tline1\n \tline2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1\nline2"); | ||
run_test("\t line1\n\t line2", "line1\nline2"); | ||
run_test(" \tline1\n \tline2", "line1\nline2"); | ||
} | ||
|
||
#[test] | ||
fn should_not_trim() { | ||
let s = "\t line1 \n\t line2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1 \nline2"); | ||
|
||
let s = " \tline1 \n \tline2".to_string(); | ||
let r = unindent(&s); | ||
assert_eq!(r, "line1 \nline2"); | ||
run_test("\t line1 \n\t line2", "line1 \nline2"); | ||
run_test(" \tline1 \n \tline2", "line1 \nline2"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Just some text. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![feature(external_doc)] | ||
|
||
#![crate_name = "foo"] | ||
|
||
// @has foo/struct.Example.html | ||
// @matches - '//pre[@class="rust rust-example-rendered"]' \ | ||
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.second\(\)\n \.build\(\);\Z' | ||
/// ```rust | ||
/// let example = Example::new() | ||
/// .first() | ||
#[cfg_attr(not(feature = "one"), doc = " .second()")] | ||
/// .build(); | ||
/// ``` | ||
pub struct Example; | ||
|
||
// @has foo/struct.F.html | ||
// @matches - '//pre[@class="rust rust-example-rendered"]' \ | ||
// '(?m)let example = Example::new\(\)\n \.first\(\)\n \.another\(\)\n \.build\(\);\Z' | ||
///```rust | ||
///let example = Example::new() | ||
/// .first() | ||
#[cfg_attr(not(feature = "one"), doc = " .another()")] | ||
/// .build(); | ||
/// ``` | ||
pub struct F; | ||
|
||
// @has foo/struct.G.html | ||
// @matches - '//pre[@class="rust rust-example-rendered"]' \ | ||
// '(?m)let example = Example::new\(\)\n\.first\(\)\n \.another\(\)\n\.build\(\);\Z' | ||
///```rust | ||
///let example = Example::new() | ||
///.first() | ||
#[cfg_attr(not(feature = "one"), doc = " .another()")] | ||
///.build(); | ||
///``` | ||
pub struct G; | ||
|
||
// @has foo/struct.H.html | ||
// @has - '//div[@class="docblock"]/p' 'no whitespace lol' | ||
///no whitespace | ||
#[doc = " lol"] | ||
pub struct H; | ||
|
||
// @has foo/struct.I.html | ||
// @matches - '//pre[@class="rust rust-example-rendered"]' '(?m)4 whitespaces!\Z' | ||
/// 4 whitespaces! | ||
#[doc = "something"] | ||
pub struct I; | ||
|
||
// @has foo/struct.J.html | ||
// @matches - '//div[@class="docblock"]/p' '(?m)a\nno whitespace\nJust some text.\Z' | ||
///a | ||
///no whitespace | ||
#[doc(include = "unindent.md")] | ||
pub struct J; | ||
|
||
// @has foo/struct.K.html | ||
// @matches - '//pre[@class="rust rust-example-rendered"]' '(?m)4 whitespaces!\Z' | ||
///a | ||
/// | ||
/// 4 whitespaces! | ||
/// | ||
#[doc(include = "unindent.md")] | ||
pub struct K; |