diff options
| author | Florian Gilcher <florian.gilcher@asquera.de> | 2016-09-29 21:34:46 +0200 |
|---|---|---|
| committer | Nick Cameron <nrc@ncameron.org> | 2016-09-30 08:34:46 +1300 |
| commit | b8f7ec3dcc8a08c835eba577b64efa92913ad429 (patch) | |
| tree | e682a0c12d2a46bb146322d6953ad43089aa60c6 | |
| parent | 3e14af0eeb9cd90b299aacc87bdd52c8fd797203 (diff) | |
Add custom comments (#1179)
* Add custom comments This allows users to use custom comments such as ``` //@ this is a custom comment //@ with multiple lines ``` without having them destroyed by rustfmt. * Fix issues with empty lines * Check non-whitespace right after custom comments
| -rw-r--r-- | src/comment.rs | 24 | ||||
| -rw-r--r-- | tests/source/comment5.rs | 7 | ||||
| -rw-r--r-- | tests/target/comment5.rs | 8 |
3 files changed, 39 insertions, 0 deletions
diff --git a/src/comment.rs b/src/comment.rs index 4ced667ef78..23d7dab3de9 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -20,6 +20,18 @@ use rewrite::RewriteContext; use string::{StringFormat, rewrite_string}; use utils::wrap_str; +fn is_custom_comment(comment: &str) -> bool { + if !comment.starts_with("//") { + false + } else { + if let Some(c) = comment.chars().nth(2) { + !c.is_alphanumeric() && !c.is_whitespace() + } else { + false + } + } +} + pub fn rewrite_comment(orig: &str, block_style: bool, width: usize, @@ -51,6 +63,12 @@ pub fn rewrite_comment(orig: &str, ("/// ", "", "/// ") } else if orig.starts_with("//!") || orig.starts_with("/*!") { ("//! ", "", "//! ") + } else if is_custom_comment(orig) { + if orig.chars().nth(3) == Some(' ') { + (&orig[0..4], "", &orig[0..4]) + } else { + (&orig[0..3], "", &orig[0..3]) + } } else { ("// ", "", "// ") }; @@ -138,6 +156,12 @@ fn left_trim_comment_line(line: &str) -> &str { if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") || line.starts_with("/** ") { &line[4..] + } else if is_custom_comment(line) { + if line.len() > 3 && line.chars().nth(3) == Some(' ') { + &line[4..] + } else { + &line[3..] + } } else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") || line.starts_with("///") || line.starts_with("** ") || line.starts_with("/*!") || diff --git a/tests/source/comment5.rs b/tests/source/comment5.rs new file mode 100644 index 00000000000..87a26f353a7 --- /dev/null +++ b/tests/source/comment5.rs @@ -0,0 +1,7 @@ +// rustfmt-wrap_comments: true + +//@ special comment +//@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adiam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam +//@ +//@foo +fn test() {} \ No newline at end of file diff --git a/tests/target/comment5.rs b/tests/target/comment5.rs new file mode 100644 index 00000000000..7006b35efed --- /dev/null +++ b/tests/target/comment5.rs @@ -0,0 +1,8 @@ +// rustfmt-wrap_comments: true + +//@ special comment +//@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adiam +//@ lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam +//@ +//@ foo +fn test() {} |
