Skip to content

Commit 95b4978

Browse files
authored
Merge pull request #2767 from topecongiro/issue-2759
Fix breaking code block with `#` in doc comments
2 parents 9e208cb + 604764a commit 95b4978

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/comment.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ fn rewrite_comment_inner(
390390
let code_block = {
391391
let mut config = config.clone();
392392
config.set().wrap_comments(false);
393-
::format_code_block(&code_block_buffer, &config)
394-
.map_or_else(|| code_block_buffer.to_owned(), trim_custom_comment_prefix)
393+
match ::format_code_block(&code_block_buffer, &config) {
394+
Some(ref s) => trim_custom_comment_prefix(s),
395+
None => trim_custom_comment_prefix(&code_block_buffer),
396+
}
395397
};
396398
result.push_str(&join_code_block_with_comment_line_separator(&code_block));
397399
code_block_buffer.clear();
@@ -406,7 +408,7 @@ fn rewrite_comment_inner(
406408
// We will leave them untouched.
407409
result.push_str(&comment_line_separator);
408410
result.push_str(&join_code_block_with_comment_line_separator(
409-
&code_block_buffer,
411+
&trim_custom_comment_prefix(&code_block_buffer),
410412
));
411413
}
412414
}
@@ -505,9 +507,16 @@ fn hide_sharp_behind_comment<'a>(s: &'a str) -> Cow<'a, str> {
505507
}
506508
}
507509

508-
fn trim_custom_comment_prefix(s: String) -> String {
510+
fn trim_custom_comment_prefix(s: &str) -> String {
509511
s.lines()
510-
.map(|line| line.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX))
512+
.map(|line| {
513+
let left_trimmed = line.trim_left();
514+
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
515+
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
516+
} else {
517+
line
518+
}
519+
})
511520
.collect::<Vec<_>>()
512521
.join("\n")
513522
}

tests/target/issue-2759.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-max_width: 89
3+
4+
// Code block in doc comments that will exceed max width.
5+
/// ```rust
6+
/// extern crate actix_web;
7+
/// use actix_web::{actix, server, App, HttpResponse};
8+
///
9+
/// fn main() {
10+
/// // Run actix system, this method actually starts all async processes
11+
/// actix::System::run(|| {
12+
/// server::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok())))
13+
/// .bind("127.0.0.1:0")
14+
/// .expect("Can not bind to 127.0.0.1:0")
15+
/// .start();
16+
/// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0));
17+
/// });
18+
/// }
19+
/// ```
20+
fn foo() {}
21+
22+
// Code block in doc comments without the closing '```'.
23+
/// ```rust
24+
/// # extern crate actix_web;
25+
/// use actix_web::{App, HttpResponse, http};
26+
///
27+
/// fn main() {
28+
/// let app = App::new()
29+
/// .resource(
30+
/// "/", |r| r.method(http::Method::GET).f(|r| HttpResponse::Ok()))
31+
/// .finish();
32+
/// }
33+
fn bar() {}
34+
35+
// `#` with indent.
36+
/// ```rust
37+
/// # use std::thread;
38+
/// # extern crate actix_web;
39+
/// use actix_web::{server, App, HttpResponse};
40+
///
41+
/// struct State1;
42+
///
43+
/// struct State2;
44+
///
45+
/// fn main() {
46+
/// # thread::spawn(|| {
47+
/// server::new(|| {
48+
/// vec![
49+
/// App::with_state(State1)
50+
/// .prefix("/app1")
51+
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
52+
/// .boxed(),
53+
/// App::with_state(State2)
54+
/// .prefix("/app2")
55+
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
56+
/// .boxed(),
57+
/// ]
58+
/// }).bind("127.0.0.1:8080")
59+
/// .unwrap()
60+
/// .run()
61+
/// # });
62+
/// }
63+
/// ```
64+
fn foobar() {}

0 commit comments

Comments
 (0)