Skip to content

Commit 6bbb7fd

Browse files
Change a bit how the first doc comment lines are handled
1 parent b4c3536 commit 6bbb7fd

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

src/librustdoc/passes/unindent_comments.rs

+33-38
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,18 @@ impl clean::Attributes {
3434
}
3535

3636
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".
4049
let add = if !docs.windows(2).all(|arr| arr[0].kind == arr[1].kind)
4150
&& docs.iter().any(|d| d.kind == DocFragmentKind::SugaredDoc)
4251
{
@@ -47,27 +56,22 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
4756
0
4857
};
4958

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.
5068
let min_indent = match docs
5169
.iter()
5270
.map(|fragment| {
5371
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-
6772
if line.chars().all(|c| c.is_whitespace()) {
6873
min_indent
6974
} else {
70-
saw_first_line = true;
7175
// Compare against either space or tab, ignoring whether they are
7276
// mixed or not.
7377
let whitespace = line.chars().take_while(|c| *c == ' ' || *c == '\t').count();
@@ -82,7 +86,6 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
8286
None => return,
8387
};
8488

85-
let mut first_ignored = false;
8689
for fragment in docs {
8790
let lines: Vec<_> = fragment.doc.lines().collect();
8891

@@ -93,26 +96,18 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
9396
min_indent
9497
};
9598

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");
116111
}
117112
}
118113
}

0 commit comments

Comments
 (0)