Skip to content

Commit 7293a8b

Browse files
authored
Merge pull request #2450 from topecongiro/issue-37
Preserve trailing spaces in doc comments even when options are set
2 parents f10c73e + b60d9b0 commit 7293a8b

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

rustfmt-core/src/comment.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ fn custom_opener(s: &str) -> &str {
5050
}
5151

5252
impl<'a> CommentStyle<'a> {
53+
pub fn is_doc_comment(&self) -> bool {
54+
match *self {
55+
CommentStyle::TripleSlash | CommentStyle::Doc => true,
56+
_ => false,
57+
}
58+
}
59+
5360
pub fn opener(&self) -> &'a str {
5461
match *self {
5562
CommentStyle::DoubleSlash => "// ",
@@ -248,14 +255,15 @@ fn _rewrite_comment(
248255
return light_rewrite_comment(orig, shape.indent, config, is_doc_comment);
249256
}
250257

251-
identify_comment(orig, block_style, shape, config)
258+
identify_comment(orig, block_style, shape, config, is_doc_comment)
252259
}
253260

254261
fn identify_comment(
255262
orig: &str,
256263
block_style: bool,
257264
shape: Shape,
258265
config: &Config,
266+
is_doc_comment: bool,
259267
) -> Option<String> {
260268
let style = comment_style(orig, false);
261269
let first_group = orig.lines()
@@ -267,11 +275,18 @@ fn identify_comment(
267275
.collect::<Vec<_>>()
268276
.join("\n");
269277

270-
let first_group_str = rewrite_comment_inner(&first_group, block_style, style, shape, config)?;
278+
let first_group_str = rewrite_comment_inner(
279+
&first_group,
280+
block_style,
281+
style,
282+
shape,
283+
config,
284+
is_doc_comment || style.is_doc_comment(),
285+
)?;
271286
if rest.is_empty() {
272287
Some(first_group_str)
273288
} else {
274-
identify_comment(&rest, block_style, shape, config).map(|rest_str| {
289+
identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
275290
format!(
276291
"{}\n{}{}",
277292
first_group_str,
@@ -288,6 +303,7 @@ fn rewrite_comment_inner(
288303
style: CommentStyle,
289304
shape: Shape,
290305
config: &Config,
306+
is_doc_comment: bool,
291307
) -> Option<String> {
292308
let (opener, closer, line_start) = if block_style {
293309
CommentStyle::SingleBullet.to_str_tuplet()
@@ -315,7 +331,7 @@ fn rewrite_comment_inner(
315331
let lines = orig.lines()
316332
.enumerate()
317333
.map(|(i, mut line)| {
318-
line = line.trim();
334+
line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
319335
// Drop old closer.
320336
if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
321337
line = line[..(line.len() - 2)].trim_right();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// Preserve two trailing whitespaces in doc comment,
4+
// but trim any whitespaces in normal comment.
5+
6+
//! hello world
7+
//! hello world
8+
9+
/// hello world
10+
/// hello world
11+
/// hello world
12+
fn foo() {
13+
// hello world
14+
// hello world
15+
let x = 3;
16+
println!("x = {}", x);
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// Preserve two trailing whitespaces in doc comment,
4+
// but trim any whitespaces in normal comment.
5+
6+
//! hello world
7+
//! hello world
8+
9+
/// hello world
10+
/// hello world
11+
/// hello world
12+
fn foo() {
13+
// hello world
14+
// hello world
15+
let x = 3;
16+
println!("x = {}", x);
17+
}

0 commit comments

Comments
 (0)