@@ -34,9 +34,18 @@ impl clean::Attributes {
34
34
}
35
35
36
36
fn unindent_fragments ( docs : & mut Vec < DocFragment > ) {
37
- let mut saw_first_line = false ;
38
- let mut saw_second_line = false ;
39
-
37
+ // `add` is used in case the most common sugared doc syntax is used ("/// "). The other
38
+ // fragments kind's lines are never starting with a whitespace unless they are using some
39
+ // markdown formatting requiring it. Therefore, if the doc block have a mix between the two,
40
+ // we need to take into account the fact that the minimum indent minus one (to take this
41
+ // whitespace into account).
42
+ //
43
+ // For example:
44
+ //
45
+ // /// hello!
46
+ // #[doc = "another"]
47
+ //
48
+ // In this case, you want "hello! another" and not "hello! another".
40
49
let add = if !docs. windows ( 2 ) . all ( |arr| arr[ 0 ] . kind == arr[ 1 ] . kind )
41
50
&& docs. iter ( ) . any ( |d| d. kind == DocFragmentKind :: SugaredDoc )
42
51
{
@@ -47,27 +56,22 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
47
56
0
48
57
} ;
49
58
59
+ // `min_indent` is used to know how much whitespaces from the start of each lines must be
60
+ // removed. Example:
61
+ //
62
+ // /// hello!
63
+ // #[doc = "another"]
64
+ //
65
+ // In here, the `min_indent` is 1 (because non-sugared fragment are always counted with minimum
66
+ // 1 whitespace), meaning that "hello!" will be considered a codeblock because it starts with 4
67
+ // (5 - 1) whitespaces.
50
68
let min_indent = match docs
51
69
. iter ( )
52
70
. map ( |fragment| {
53
71
fragment. doc . lines ( ) . fold ( usize:: MAX , |min_indent, line| {
54
- // After we see the first non-whitespace line, look at
55
- // the line we have. If it is not whitespace, and therefore
56
- // part of the first paragraph, then ignore the indentation
57
- // level of the first line
58
- let ignore_previous_indents =
59
- saw_first_line && !saw_second_line && !line. chars ( ) . all ( |c| c. is_whitespace ( ) ) ;
60
-
61
- let min_indent = if ignore_previous_indents { usize:: MAX } else { min_indent } ;
62
-
63
- if saw_first_line {
64
- saw_second_line = true ;
65
- }
66
-
67
72
if line. chars ( ) . all ( |c| c. is_whitespace ( ) ) {
68
73
min_indent
69
74
} else {
70
- saw_first_line = true ;
71
75
// Compare against either space or tab, ignoring whether they are
72
76
// mixed or not.
73
77
let whitespace = line. chars ( ) . take_while ( |c| * c == ' ' || * c == '\t' ) . count ( ) ;
@@ -82,7 +86,6 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
82
86
None => return ,
83
87
} ;
84
88
85
- let mut first_ignored = false ;
86
89
for fragment in docs {
87
90
let lines: Vec < _ > = fragment. doc . lines ( ) . collect ( ) ;
88
91
@@ -93,26 +96,18 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
93
96
min_indent
94
97
} ;
95
98
96
- let mut iter = lines. iter ( ) ;
97
- let mut result = if !first_ignored {
98
- first_ignored = true ;
99
- vec ! [ iter. next( ) . unwrap( ) . trim_start( ) . to_string( ) ]
100
- } else {
101
- Vec :: new ( )
102
- } ;
103
- result. extend_from_slice (
104
- & iter
105
- . map ( |& line| {
106
- if line. chars ( ) . all ( |c| c. is_whitespace ( ) ) {
107
- line. to_string ( )
108
- } else {
109
- assert ! ( line. len( ) >= min_indent) ;
110
- line[ min_indent..] . to_string ( )
111
- }
112
- } )
113
- . collect :: < Vec < _ > > ( ) ,
114
- ) ;
115
- fragment. doc = result. join ( "\n " ) ;
99
+ fragment. doc = lines
100
+ . iter ( )
101
+ . map ( |& line| {
102
+ if line. chars ( ) . all ( |c| c. is_whitespace ( ) ) {
103
+ line. to_string ( )
104
+ } else {
105
+ assert ! ( line. len( ) >= min_indent) ;
106
+ line[ min_indent..] . to_string ( )
107
+ }
108
+ } )
109
+ . collect :: < Vec < _ > > ( )
110
+ . join ( "\n " ) ;
116
111
}
117
112
}
118
113
}
0 commit comments