mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
Merge pull request #2732 from topecongiro/issue-2691
Format code block with sharp prefix
This commit is contained in:
commit
8f743405b3
@ -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 {
|
||||
@ -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;
|
||||
@ -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.
|
||||
|
12
tests/source/doc-comment-with-example.rs
Normal file
12
tests/source/doc-comment-with-example.rs
Normal file
@ -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
tests/target/doc-comment-with-example.rs
Normal file
12
tests/target/doc-comment-with-example.rs
Normal file
@ -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() {}
|
Loading…
Reference in New Issue
Block a user