Skip to content

Commit

Permalink
Add custom comments (#1179)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
skade authored and nrc committed Sep 29, 2016
1 parent 3e14af0 commit b8f7ec3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/comment.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
("// ", "", "// ")
};
Expand Down Expand Up @@ -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("/*!") ||
Expand Down
7 changes: 7 additions & 0 deletions 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() {}
8 changes: 8 additions & 0 deletions 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() {}

0 comments on commit b8f7ec3

Please sign in to comment.