Skip to content

Fix breaking code block with # in doc comments #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ fn rewrite_comment_inner(
let code_block = {
let mut config = config.clone();
config.set().wrap_comments(false);
::format_code_block(&code_block_buffer, &config)
.map_or_else(|| code_block_buffer.to_owned(), trim_custom_comment_prefix)
match ::format_code_block(&code_block_buffer, &config) {
Some(ref s) => trim_custom_comment_prefix(s),
None => trim_custom_comment_prefix(&code_block_buffer),
}
};
result.push_str(&join_code_block_with_comment_line_separator(&code_block));
code_block_buffer.clear();
Expand All @@ -406,7 +408,7 @@ fn rewrite_comment_inner(
// We will leave them untouched.
result.push_str(&comment_line_separator);
result.push_str(&join_code_block_with_comment_line_separator(
&code_block_buffer,
&trim_custom_comment_prefix(&code_block_buffer),
));
}
}
Expand Down Expand Up @@ -505,9 +507,16 @@ fn hide_sharp_behind_comment<'a>(s: &'a str) -> Cow<'a, str> {
}
}

fn trim_custom_comment_prefix(s: String) -> String {
fn trim_custom_comment_prefix(s: &str) -> String {
s.lines()
.map(|line| line.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX))
.map(|line| {
let left_trimmed = line.trim_left();
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
} else {
line
}
})
.collect::<Vec<_>>()
.join("\n")
}
Expand Down
64 changes: 64 additions & 0 deletions tests/target/issue-2759.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// rustfmt-wrap_comments: true
// rustfmt-max_width: 89

// Code block in doc comments that will exceed max width.
/// ```rust
/// extern crate actix_web;
/// use actix_web::{actix, server, App, HttpResponse};
///
/// fn main() {
/// // Run actix system, this method actually starts all async processes
/// actix::System::run(|| {
/// server::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok())))
/// .bind("127.0.0.1:0")
/// .expect("Can not bind to 127.0.0.1:0")
/// .start();
/// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0));
/// });
/// }
/// ```
fn foo() {}

// Code block in doc comments without the closing '```'.
/// ```rust
/// # extern crate actix_web;
/// use actix_web::{App, HttpResponse, http};
///
/// fn main() {
/// let app = App::new()
/// .resource(
/// "/", |r| r.method(http::Method::GET).f(|r| HttpResponse::Ok()))
/// .finish();
/// }
fn bar() {}

// `#` with indent.
/// ```rust
/// # use std::thread;
/// # extern crate actix_web;
/// use actix_web::{server, App, HttpResponse};
///
/// struct State1;
///
/// struct State2;
///
/// fn main() {
/// # thread::spawn(|| {
/// server::new(|| {
/// vec![
/// App::with_state(State1)
/// .prefix("/app1")
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
/// .boxed(),
/// App::with_state(State2)
/// .prefix("/app2")
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
/// .boxed(),
/// ]
/// }).bind("127.0.0.1:8080")
/// .unwrap()
/// .run()
/// # });
/// }
/// ```
fn foobar() {}