Skip to content

Format code block with sharp prefix #2732

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 3 commits into from
May 24, 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
29 changes: 25 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,18 @@ fn rewrite_comment_inner(
if line.starts_with("```") {
inside_code_block = false;
result.push_str(&comment_line_separator);
let code_block = ::format_code_block(&code_block_buffer, config)
.unwrap_or_else(|| code_block_buffer.to_owned());
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)
};
result.push_str(&join_code_block_with_comment_line_separator(&code_block));
code_block_buffer.clear();
result.push_str(&comment_line_separator);
result.push_str(line);
} else {
code_block_buffer.push_str(line);
code_block_buffer.push_str(&hide_sharp_behind_comment(line));
code_block_buffer.push('\n');

if is_last {
Expand All @@ -409,7 +413,7 @@ fn rewrite_comment_inner(

continue;
} else {
inside_code_block = line.starts_with("```rust");
inside_code_block = line.starts_with("```");

if result == opener {
let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
Expand Down Expand Up @@ -491,6 +495,23 @@ fn rewrite_comment_inner(
Some(result)
}

const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";

fn hide_sharp_behind_comment<'a>(s: &'a str) -> Cow<'a, str> {
if s.trim_left().starts_with('#') {
Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
} else {
Cow::from(s)
}
}

fn trim_custom_comment_prefix(s: String) -> String {
s.lines()
.map(|line| line.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX))
.collect::<Vec<_>>()
.join("\n")
}

/// Returns true if the given string MAY include URLs or alike.
fn has_url(s: &str) -> bool {
// This function may return false positive, but should get its job done in most cases.
Expand Down
12 changes: 12 additions & 0 deletions tests/source/doc-comment-with-example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-wrap_comments: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() { }
/// ```
///
fn foo() {}
12 changes: 12 additions & 0 deletions tests/target/doc-comment-with-example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-wrap_comments: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() {}
/// ```
///
fn foo() {}