Merge pull request #1607 from topecongiro/many-slashes
Allow longer custom comments
This commit is contained in:
commit
f7de945f9e
@ -33,18 +33,26 @@ fn is_custom_comment(comment: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum CommentStyle {
|
pub enum CommentStyle<'a> {
|
||||||
DoubleSlash,
|
DoubleSlash,
|
||||||
TripleSlash,
|
TripleSlash,
|
||||||
Doc,
|
Doc,
|
||||||
SingleBullet,
|
SingleBullet,
|
||||||
DoubleBullet,
|
DoubleBullet,
|
||||||
Exclamation,
|
Exclamation,
|
||||||
Custom,
|
Custom(&'a str),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommentStyle {
|
fn custom_opener(s: &str) -> &str {
|
||||||
pub fn opener<'a>(&self, orig: &'a str) -> &'a str {
|
s.lines().next().map_or("", |first_line| {
|
||||||
|
first_line
|
||||||
|
.find(' ')
|
||||||
|
.map_or(first_line, |space_index| &first_line[0..space_index + 1])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> CommentStyle<'a> {
|
||||||
|
pub fn opener(&self) -> &'a str {
|
||||||
match *self {
|
match *self {
|
||||||
CommentStyle::DoubleSlash => "// ",
|
CommentStyle::DoubleSlash => "// ",
|
||||||
CommentStyle::TripleSlash => "/// ",
|
CommentStyle::TripleSlash => "/// ",
|
||||||
@ -52,21 +60,15 @@ pub fn opener<'a>(&self, orig: &'a str) -> &'a str {
|
|||||||
CommentStyle::SingleBullet => "/* ",
|
CommentStyle::SingleBullet => "/* ",
|
||||||
CommentStyle::DoubleBullet => "/** ",
|
CommentStyle::DoubleBullet => "/** ",
|
||||||
CommentStyle::Exclamation => "/*! ",
|
CommentStyle::Exclamation => "/*! ",
|
||||||
CommentStyle::Custom => {
|
CommentStyle::Custom(opener) => opener,
|
||||||
if orig.chars().nth(3) == Some(' ') {
|
|
||||||
&orig[0..4]
|
|
||||||
} else {
|
|
||||||
&orig[0..3]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn closer<'a>(&self) -> &'a str {
|
pub fn closer(&self) -> &'a str {
|
||||||
match *self {
|
match *self {
|
||||||
CommentStyle::DoubleSlash |
|
CommentStyle::DoubleSlash |
|
||||||
CommentStyle::TripleSlash |
|
CommentStyle::TripleSlash |
|
||||||
CommentStyle::Custom |
|
CommentStyle::Custom(..) |
|
||||||
CommentStyle::Doc => "",
|
CommentStyle::Doc => "",
|
||||||
CommentStyle::DoubleBullet => " **/",
|
CommentStyle::DoubleBullet => " **/",
|
||||||
CommentStyle::SingleBullet |
|
CommentStyle::SingleBullet |
|
||||||
@ -74,7 +76,7 @@ pub fn closer<'a>(&self) -> &'a str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_start<'a>(&self, orig: &'a str) -> &'a str {
|
pub fn line_start(&self) -> &'a str {
|
||||||
match *self {
|
match *self {
|
||||||
CommentStyle::DoubleSlash => "// ",
|
CommentStyle::DoubleSlash => "// ",
|
||||||
CommentStyle::TripleSlash => "/// ",
|
CommentStyle::TripleSlash => "/// ",
|
||||||
@ -82,42 +84,30 @@ pub fn line_start<'a>(&self, orig: &'a str) -> &'a str {
|
|||||||
CommentStyle::SingleBullet |
|
CommentStyle::SingleBullet |
|
||||||
CommentStyle::Exclamation => " * ",
|
CommentStyle::Exclamation => " * ",
|
||||||
CommentStyle::DoubleBullet => " ** ",
|
CommentStyle::DoubleBullet => " ** ",
|
||||||
CommentStyle::Custom => {
|
CommentStyle::Custom(opener) => opener,
|
||||||
if orig.chars().nth(3) == Some(' ') {
|
|
||||||
&orig[0..4]
|
|
||||||
} else {
|
|
||||||
&orig[0..3]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_str_tuplet<'a>(&self, orig: &'a str) -> (&'a str, &'a str, &'a str) {
|
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
|
||||||
(self.opener(orig), self.closer(), self.line_start(orig))
|
(self.opener(), self.closer(), self.line_start())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_with_same_comment_style<'a>(&self,
|
pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
|
||||||
line: &str,
|
|
||||||
orig: &'a str,
|
|
||||||
normalize_comments: bool)
|
|
||||||
-> bool {
|
|
||||||
match *self {
|
match *self {
|
||||||
CommentStyle::DoubleSlash |
|
CommentStyle::DoubleSlash |
|
||||||
CommentStyle::TripleSlash |
|
CommentStyle::TripleSlash |
|
||||||
CommentStyle::Custom |
|
|
||||||
CommentStyle::Doc => {
|
CommentStyle::Doc => {
|
||||||
line.trim_left()
|
line.trim_left().starts_with(self.line_start().trim_left()) ||
|
||||||
.starts_with(self.line_start(orig).trim_left()) ||
|
|
||||||
comment_style(line, normalize_comments) == *self
|
comment_style(line, normalize_comments) == *self
|
||||||
}
|
}
|
||||||
CommentStyle::DoubleBullet |
|
CommentStyle::DoubleBullet |
|
||||||
CommentStyle::SingleBullet |
|
CommentStyle::SingleBullet |
|
||||||
CommentStyle::Exclamation => {
|
CommentStyle::Exclamation => {
|
||||||
line.trim_left().starts_with(self.closer().trim_left()) ||
|
line.trim_left().starts_with(self.closer().trim_left()) ||
|
||||||
line.trim_left()
|
line.trim_left().starts_with(self.line_start().trim_left()) ||
|
||||||
.starts_with(self.line_start(orig).trim_left()) ||
|
|
||||||
comment_style(line, normalize_comments) == *self
|
comment_style(line, normalize_comments) == *self
|
||||||
}
|
}
|
||||||
|
CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,19 +120,22 @@ fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
|
|||||||
CommentStyle::Exclamation
|
CommentStyle::Exclamation
|
||||||
} else if orig.starts_with("/*") {
|
} else if orig.starts_with("/*") {
|
||||||
CommentStyle::SingleBullet
|
CommentStyle::SingleBullet
|
||||||
} else if orig.starts_with("///") {
|
} else if orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/') {
|
||||||
CommentStyle::TripleSlash
|
CommentStyle::TripleSlash
|
||||||
} else if orig.starts_with("//!") {
|
} else if orig.starts_with("//!") {
|
||||||
CommentStyle::Doc
|
CommentStyle::Doc
|
||||||
|
} else if is_custom_comment(orig) {
|
||||||
|
CommentStyle::Custom(custom_opener(orig))
|
||||||
} else {
|
} else {
|
||||||
CommentStyle::DoubleSlash
|
CommentStyle::DoubleSlash
|
||||||
}
|
}
|
||||||
} else if orig.starts_with("///") || (orig.starts_with("/**") && !orig.starts_with("/**/")) {
|
} else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/')) ||
|
||||||
|
(orig.starts_with("/**") && !orig.starts_with("/**/")) {
|
||||||
CommentStyle::TripleSlash
|
CommentStyle::TripleSlash
|
||||||
} else if orig.starts_with("//!") || orig.starts_with("/*!") {
|
} else if orig.starts_with("//!") || orig.starts_with("/*!") {
|
||||||
CommentStyle::Doc
|
CommentStyle::Doc
|
||||||
} else if is_custom_comment(orig) {
|
} else if is_custom_comment(orig) {
|
||||||
CommentStyle::Custom
|
CommentStyle::Custom(custom_opener(orig))
|
||||||
} else {
|
} else {
|
||||||
CommentStyle::DoubleSlash
|
CommentStyle::DoubleSlash
|
||||||
}
|
}
|
||||||
@ -177,7 +170,7 @@ fn identify_comment(orig: &str,
|
|||||||
-> Option<String> {
|
-> Option<String> {
|
||||||
let style = comment_style(orig, false);
|
let style = comment_style(orig, false);
|
||||||
let first_group = orig.lines()
|
let first_group = orig.lines()
|
||||||
.take_while(|l| style.line_with_same_comment_style(l, orig, false))
|
.take_while(|l| style.line_with_same_comment_style(l, false))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
let rest = orig.lines()
|
let rest = orig.lines()
|
||||||
@ -185,7 +178,8 @@ fn identify_comment(orig: &str,
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
|
||||||
let first_group_str = try_opt!(rewrite_comment_inner(&first_group, block_style, shape, config));
|
let first_group_str =
|
||||||
|
try_opt!(rewrite_comment_inner(&first_group, block_style, style, shape, config));
|
||||||
if rest.is_empty() {
|
if rest.is_empty() {
|
||||||
Some(first_group_str)
|
Some(first_group_str)
|
||||||
} else {
|
} else {
|
||||||
@ -202,13 +196,14 @@ fn identify_comment(orig: &str,
|
|||||||
|
|
||||||
fn rewrite_comment_inner(orig: &str,
|
fn rewrite_comment_inner(orig: &str,
|
||||||
block_style: bool,
|
block_style: bool,
|
||||||
|
style: CommentStyle,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
config: &Config)
|
config: &Config)
|
||||||
-> Option<String> {
|
-> Option<String> {
|
||||||
let (opener, closer, line_start) = if block_style {
|
let (opener, closer, line_start) = if block_style {
|
||||||
CommentStyle::SingleBullet.to_str_tuplet("")
|
CommentStyle::SingleBullet.to_str_tuplet()
|
||||||
} else {
|
} else {
|
||||||
comment_style(orig, config.normalize_comments()).to_str_tuplet(orig)
|
comment_style(orig, config.normalize_comments()).to_str_tuplet()
|
||||||
};
|
};
|
||||||
|
|
||||||
let max_chars = shape
|
let max_chars = shape
|
||||||
@ -238,7 +233,7 @@ fn rewrite_comment_inner(orig: &str,
|
|||||||
|
|
||||||
line
|
line
|
||||||
})
|
})
|
||||||
.map(left_trim_comment_line)
|
.map(|s| left_trim_comment_line(s, &style))
|
||||||
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
|
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
|
||||||
line.trim_left()
|
line.trim_left()
|
||||||
} else {
|
} else {
|
||||||
@ -261,7 +256,7 @@ fn rewrite_comment_inner(orig: &str,
|
|||||||
let rewrite = rewrite_string(line, &fmt).unwrap_or(line.to_owned());
|
let rewrite = rewrite_string(line, &fmt).unwrap_or(line.to_owned());
|
||||||
result.push_str(&rewrite);
|
result.push_str(&rewrite);
|
||||||
} else {
|
} else {
|
||||||
if line.is_empty() {
|
if line.is_empty() && result.ends_with(' ') {
|
||||||
// Remove space if this is an empty comment or a doc comment.
|
// Remove space if this is an empty comment or a doc comment.
|
||||||
result.pop();
|
result.pop();
|
||||||
}
|
}
|
||||||
@ -270,7 +265,7 @@ fn rewrite_comment_inner(orig: &str,
|
|||||||
}
|
}
|
||||||
|
|
||||||
result.push_str(closer);
|
result.push_str(closer);
|
||||||
if result == opener {
|
if result == opener && result.ends_with(' ') {
|
||||||
// Trailing space.
|
// Trailing space.
|
||||||
result.pop();
|
result.pop();
|
||||||
}
|
}
|
||||||
@ -302,15 +297,15 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<
|
|||||||
|
|
||||||
/// Trims comment characters and possibly a single space from the left of a string.
|
/// Trims comment characters and possibly a single space from the left of a string.
|
||||||
/// Does not trim all whitespace.
|
/// Does not trim all whitespace.
|
||||||
fn left_trim_comment_line(line: &str) -> &str {
|
fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> &'a str {
|
||||||
if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") ||
|
if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") ||
|
||||||
line.starts_with("/** ") {
|
line.starts_with("/** ") {
|
||||||
&line[4..]
|
&line[4..]
|
||||||
} else if is_custom_comment(line) {
|
} else if let &CommentStyle::Custom(opener) = style {
|
||||||
if line.len() > 3 && line.chars().nth(3) == Some(' ') {
|
if line.starts_with(opener) {
|
||||||
&line[4..]
|
&line[opener.len()..]
|
||||||
} else {
|
} else {
|
||||||
&line[3..]
|
&line[opener.trim_right().len()..]
|
||||||
}
|
}
|
||||||
} else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") ||
|
} else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") ||
|
||||||
line.starts_with("///") ||
|
line.starts_with("///") ||
|
||||||
|
@ -6,3 +6,9 @@
|
|||||||
//@
|
//@
|
||||||
//@foo
|
//@foo
|
||||||
fn test() {}
|
fn test() {}
|
||||||
|
|
||||||
|
//@@@ another 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 bar() {}
|
||||||
|
@ -6,3 +6,8 @@ fn dolor() -> usize {}
|
|||||||
|
|
||||||
/* sit amet: */
|
/* sit amet: */
|
||||||
fn adipiscing() -> usize {}
|
fn adipiscing() -> usize {}
|
||||||
|
|
||||||
|
// #652
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Basic slice extension methods
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -6,3 +6,8 @@ fn dolor() -> usize {}
|
|||||||
|
|
||||||
/* sit amet: */
|
/* sit amet: */
|
||||||
fn adipiscing() -> usize {}
|
fn adipiscing() -> usize {}
|
||||||
|
|
||||||
|
// #652
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Basic slice extension methods
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -7,3 +7,10 @@
|
|||||||
//@
|
//@
|
||||||
//@ foo
|
//@ foo
|
||||||
fn test() {}
|
fn test() {}
|
||||||
|
|
||||||
|
//@@@ another 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 bar() {}
|
||||||
|
@ -6,3 +6,8 @@ fn dolor() -> usize {}
|
|||||||
|
|
||||||
/* sit amet: */
|
/* sit amet: */
|
||||||
fn adipiscing() -> usize {}
|
fn adipiscing() -> usize {}
|
||||||
|
|
||||||
|
// #652
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Basic slice extension methods
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -6,3 +6,8 @@ fn dolor() -> usize {}
|
|||||||
|
|
||||||
// sit amet:
|
// sit amet:
|
||||||
fn adipiscing() -> usize {}
|
fn adipiscing() -> usize {}
|
||||||
|
|
||||||
|
// #652
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Basic slice extension methods
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user