Skip to content

Commit 3d61c2c

Browse files
committed
Move block comment helps to its own module
1 parent 08deb86 commit 3d61c2c

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed

compiler/rustc_ast/src/util/comments.rs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_span::source_map::SourceMap;
22
use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol};
33

4+
mod block_comment;
45
#[cfg(test)]
56
mod tests;
67

@@ -26,66 +27,7 @@ pub struct Comment {
2627
/// Makes a doc string more presentable to users.
2728
/// Used by rustdoc and perhaps other tools, but not by rustc.
2829
pub fn beautify_doc_string(data: Symbol) -> String {
29-
/// remove whitespace-only lines from the start/end of lines
30-
fn vertical_trim(lines: Vec<String>) -> Vec<String> {
31-
let mut i = 0;
32-
let mut j = lines.len();
33-
// first line of all-stars should be omitted
34-
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
35-
i += 1;
36-
}
37-
38-
while i < j && lines[i].trim().is_empty() {
39-
i += 1;
40-
}
41-
// like the first, a last line of all stars should be omitted
42-
if j > i && lines[j - 1].chars().skip(1).all(|c| c == '*') {
43-
j -= 1;
44-
}
45-
46-
while j > i && lines[j - 1].trim().is_empty() {
47-
j -= 1;
48-
}
49-
50-
lines[i..j].to_vec()
51-
}
52-
53-
/// remove a "[ \t]*\*" block from each line, if possible
54-
fn horizontal_trim(lines: Vec<String>) -> Vec<String> {
55-
let mut i = usize::MAX;
56-
let mut can_trim = true;
57-
let mut first = true;
58-
59-
for line in &lines {
60-
for (j, c) in line.chars().enumerate() {
61-
if j > i || !"* \t".contains(c) {
62-
can_trim = false;
63-
break;
64-
}
65-
if c == '*' {
66-
if first {
67-
i = j;
68-
first = false;
69-
} else if i != j {
70-
can_trim = false;
71-
}
72-
break;
73-
}
74-
}
75-
if i >= line.len() {
76-
can_trim = false;
77-
}
78-
if !can_trim {
79-
break;
80-
}
81-
}
82-
83-
if can_trim {
84-
lines.iter().map(|line| (&line[i + 1..line.len()]).to_string()).collect()
85-
} else {
86-
lines
87-
}
88-
}
30+
use block_comment::{horizontal_trim, vertical_trim};
8931

9032
let data = data.as_str();
9133
if data.contains('\n') {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*!
2+
* Block comment helpers.
3+
*/
4+
5+
/// remove whitespace-only lines from the start/end of lines
6+
pub fn vertical_trim(lines: Vec<String>) -> Vec<String> {
7+
let mut i = 0;
8+
let mut j = lines.len();
9+
// first line of all-stars should be omitted
10+
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
11+
i += 1;
12+
}
13+
14+
while i < j && lines[i].trim().is_empty() {
15+
i += 1;
16+
}
17+
// like the first, a last line of all stars should be omitted
18+
if j > i && lines[j - 1].chars().skip(1).all(|c| c == '*') {
19+
j -= 1;
20+
}
21+
22+
while j > i && lines[j - 1].trim().is_empty() {
23+
j -= 1;
24+
}
25+
26+
lines[i..j].to_vec()
27+
}
28+
29+
/// remove a "[ \t]*\*" block from each line, if possible
30+
pub fn horizontal_trim(lines: Vec<String>) -> Vec<String> {
31+
let mut i = usize::MAX;
32+
let mut can_trim = true;
33+
let mut first = true;
34+
35+
for line in &lines {
36+
for (j, c) in line.chars().enumerate() {
37+
if j > i || !"* \t".contains(c) {
38+
can_trim = false;
39+
break;
40+
}
41+
if c == '*' {
42+
if first {
43+
i = j;
44+
first = false;
45+
} else if i != j {
46+
can_trim = false;
47+
}
48+
break;
49+
}
50+
}
51+
if i >= line.len() {
52+
can_trim = false;
53+
}
54+
if !can_trim {
55+
break;
56+
}
57+
}
58+
59+
if can_trim {
60+
lines.iter().map(|line| (&line[i + 1..line.len()]).to_string()).collect()
61+
} else {
62+
lines
63+
}
64+
}

0 commit comments

Comments
 (0)