use shape::{Indent, Shape};
use string::{rewrite_string, StringFormat};
use utils::{count_newlines, first_line_width, last_line_width};
+use {ErrorKind, FormattingError};
fn is_custom_comment(comment: &str) -> bool {
if !comment.starts_with("//") {
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
(self.opener(), self.closer(), self.line_start())
}
-
- pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
- match *self {
- CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
- line.trim_left().starts_with(self.line_start().trim_left())
- || comment_style(line, normalize_comments) == *self
- }
- CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
- line.trim_left().starts_with(self.closer().trim_left())
- || line.trim_left().starts_with(self.line_start().trim_left())
- || comment_style(line, normalize_comments) == *self
- }
- CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
- }
- }
}
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
is_doc_comment: bool,
) -> Option<String> {
let style = comment_style(orig, false);
- let first_group = orig
- .lines()
- .take_while(|l| style.line_with_same_comment_style(l, false))
- .collect::<Vec<_>>()
- .join("\n");
- let rest = orig
- .lines()
- .skip(first_group.lines().count())
- .collect::<Vec<_>>()
- .join("\n");
+ let mut first_group_ending = 0;
+
+ fn compute_len(orig: &str, line: &str) -> usize {
+ if orig.len() > line.len() {
+ if orig.as_bytes()[line.len()] == b'\r' {
+ line.len() + 2
+ } else {
+ line.len() + 1
+ }
+ } else {
+ line.len()
+ }
+ }
+ match style {
+ CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
+ let line_start = style.line_start().trim_left();
+ for line in orig.lines() {
+ if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
+ first_group_ending += compute_len(&orig[first_group_ending..], line);
+ } else {
+ break;
+ }
+ }
+ }
+ CommentStyle::Custom(opener) => {
+ let trimmed_opener = opener.trim_right();
+ for line in orig.lines() {
+ if line.trim_left().starts_with(trimmed_opener) {
+ first_group_ending += compute_len(&orig[first_group_ending..], line);
+ } else {
+ break;
+ }
+ }
+ }
+ // for a block comment, search for the closing symbol
+ CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
+ let closer = style.closer().trim_left();
+ for line in orig.lines() {
+ first_group_ending += compute_len(&orig[first_group_ending..], line);
+ if line.trim_left().ends_with(closer) {
+ break;
+ }
+ }
+ }
+ }
+
+ let (first_group, rest) = orig.split_at(first_group_ending);
let first_group_str = rewrite_comment_inner(
- &first_group,
+ first_group,
block_style,
style,
shape,
if rest.is_empty() {
Some(first_group_str)
} else {
- identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
+ identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
format!(
"{}\n{}{}",
first_group_str,
}
line
- })
- .map(|s| left_trim_comment_line(s, &style))
+ }).map(|s| left_trim_comment_line(s, &style))
.map(|(line, has_leading_whitespace)| {
if orig.starts_with("/*") && line_breaks == 0 {
(
}
} else if is_prev_line_multi_line && !line.is_empty() {
result.push(' ')
- } else if is_last && !closer.is_empty() && line.is_empty() {
- result.push_str(&indent_str);
+ } else if is_last && line.is_empty() {
+ // trailing blank lines are unwanted
+ if !closer.is_empty() {
+ result.push_str(&indent_str);
+ }
+ break;
} else {
result.push_str(&comment_line_separator);
if !has_leading_whitespace && result.ends_with(' ') {
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
fn hide_sharp_behind_comment<'a>(s: &'a str) -> Cow<'a, str> {
- if s.trim_left().starts_with('#') {
+ if s.trim_left().starts_with("# ") {
Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
} else {
Cow::from(s)
} else {
line
}
- })
- .collect::<Vec<_>>()
+ }).collect::<Vec<_>>()
.join("\n")
}
};
// Preserve markdown's double-space line break syntax in doc comment.
trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
- })
- .collect();
+ }).collect();
Some(lines.join(&format!("\n{}", offset.to_string(config))))
}
) -> Option<String> {
let snippet = context.snippet(span);
if snippet != new && changed_comment_content(snippet, &new) {
- // We missed some comments. Keep the original text.
+ // We missed some comments. Warn and keep the original text.
+ if context.config.error_on_unformatted() {
+ context.report.append(
+ context.codemap.span_to_filename(span).into(),
+ vec![FormattingError::from_span(
+ &span,
+ &context.codemap,
+ ErrorKind::LostComment,
+ )],
+ );
+ }
Some(snippet.to_owned())
} else {
Some(new)
}
}
+pub fn filter_normal_code(code: &str) -> String {
+ let mut buffer = String::with_capacity(code.len());
+ LineClasses::new(code).for_each(|(kind, line)| match kind {
+ FullCodeCharKind::Normal | FullCodeCharKind::InString => {
+ buffer.push_str(&line);
+ buffer.push('\n');
+ }
+ _ => (),
+ });
+ if !code.ends_with("\n") && buffer.ends_with("\n") {
+ buffer.pop();
+ }
+ buffer
+}
+
/// Return true if the two strings of code have the same payload of comments.
/// The payload of comments is everything in the string except:
/// - actual code (not comments)
.filter_map(|(s, c)| match s {
FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
_ => None,
- })
- .collect()
+ }).collect()
}
#[test]
let s = format!(" r#\"\n test\n \"#");
assert_eq!(remove_trailing_white_spaces(&s), s);
}
+
+ #[test]
+ fn test_filter_normal_code() {
+ let s = r#"
+fn main() {
+ println!("hello, world");
+}
+"#;
+ assert_eq!(s, filter_normal_code(s));
+ let s_with_comment = r#"
+fn main() {
+ // hello, world
+ println!("hello, world");
+}
+"#;
+ assert_eq!(s, filter_normal_code(s_with_comment));
+ }
}