1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Formatting and tools for comments.
13 use std::{self, iter, borrow::Cow};
15 use syntax::codemap::Span;
18 use rewrite::RewriteContext;
19 use shape::{Indent, Shape};
20 use string::{rewrite_string, StringFormat};
21 use utils::{count_newlines, first_line_width, last_line_width};
23 fn is_custom_comment(comment: &str) -> bool {
24 if !comment.starts_with("//") {
26 } else if let Some(c) = comment.chars().nth(2) {
27 !c.is_alphanumeric() && !c.is_whitespace()
33 #[derive(Copy, Clone, PartialEq, Eq)]
34 pub enum CommentStyle<'a> {
44 fn custom_opener(s: &str) -> &str {
45 s.lines().next().map_or("", |first_line| {
48 .map_or(first_line, |space_index| &first_line[0..space_index + 1])
52 impl<'a> CommentStyle<'a> {
53 pub fn is_doc_comment(&self) -> bool {
55 CommentStyle::TripleSlash | CommentStyle::Doc => true,
60 pub fn opener(&self) -> &'a str {
62 CommentStyle::DoubleSlash => "// ",
63 CommentStyle::TripleSlash => "/// ",
64 CommentStyle::Doc => "//! ",
65 CommentStyle::SingleBullet => "/* ",
66 CommentStyle::DoubleBullet => "/** ",
67 CommentStyle::Exclamation => "/*! ",
68 CommentStyle::Custom(opener) => opener,
72 pub fn closer(&self) -> &'a str {
74 CommentStyle::DoubleSlash
75 | CommentStyle::TripleSlash
76 | CommentStyle::Custom(..)
77 | CommentStyle::Doc => "",
78 CommentStyle::DoubleBullet => " **/",
79 CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
83 pub fn line_start(&self) -> &'a str {
85 CommentStyle::DoubleSlash => "// ",
86 CommentStyle::TripleSlash => "/// ",
87 CommentStyle::Doc => "//! ",
88 CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
89 CommentStyle::DoubleBullet => " ** ",
90 CommentStyle::Custom(opener) => opener,
94 pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
95 (self.opener(), self.closer(), self.line_start())
98 pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
100 CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
101 line.trim_left().starts_with(self.line_start().trim_left())
102 || comment_style(line, normalize_comments) == *self
104 CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
105 line.trim_left().starts_with(self.closer().trim_left())
106 || line.trim_left().starts_with(self.line_start().trim_left())
107 || comment_style(line, normalize_comments) == *self
109 CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
114 fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
115 if !normalize_comments {
116 if orig.starts_with("/**") && !orig.starts_with("/**/") {
117 CommentStyle::DoubleBullet
118 } else if orig.starts_with("/*!") {
119 CommentStyle::Exclamation
120 } else if orig.starts_with("/*") {
121 CommentStyle::SingleBullet
122 } else if orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/') {
123 CommentStyle::TripleSlash
124 } else if orig.starts_with("//!") {
126 } else if is_custom_comment(orig) {
127 CommentStyle::Custom(custom_opener(orig))
129 CommentStyle::DoubleSlash
131 } else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/'))
132 || (orig.starts_with("/**") && !orig.starts_with("/**/"))
134 CommentStyle::TripleSlash
135 } else if orig.starts_with("//!") || orig.starts_with("/*!") {
137 } else if is_custom_comment(orig) {
138 CommentStyle::Custom(custom_opener(orig))
140 CommentStyle::DoubleSlash
144 /// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
145 /// comments between two strings. If there are such comments, then that will be
146 /// recovered. If `allow_extend` is true and there is no comment between the two
147 /// strings, then they will be put on a single line as long as doing so does not
148 /// exceed max width.
149 pub fn combine_strs_with_missing_comments(
150 context: &RewriteContext,
156 ) -> Option<String> {
158 String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
159 result.push_str(prev_str);
160 let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
161 let first_sep = if prev_str.is_empty() || next_str.is_empty() {
166 let mut one_line_width =
167 last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
169 let config = context.config;
170 let indent = shape.indent;
171 let missing_comment = rewrite_missing_comment(span, shape, context)?;
173 if missing_comment.is_empty() {
174 if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
175 result.push_str(first_sep);
176 } else if !prev_str.is_empty() {
177 result.push_str(&indent.to_string_with_newline(config))
179 result.push_str(next_str);
183 // We have a missing comment between the first expression and the second expression.
185 // Peek the the original source code and find out whether there is a newline between the first
186 // expression and the second expression or the missing comment. We will preserve the original
187 // layout whenever possible.
188 let original_snippet = context.snippet(span);
189 let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') {
190 !original_snippet[..pos].contains('\n')
192 !original_snippet.contains('\n')
195 one_line_width -= first_sep.len();
196 let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
199 let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
200 if prefer_same_line && one_line_width <= shape.width {
203 indent.to_string_with_newline(config)
206 result.push_str(&first_sep);
207 result.push_str(&missing_comment);
209 let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
211 } else if missing_comment.starts_with("//") {
212 indent.to_string_with_newline(config)
214 one_line_width += missing_comment.len() + first_sep.len() + 1;
215 allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
216 if prefer_same_line && allow_one_line && one_line_width <= shape.width {
219 indent.to_string_with_newline(config)
222 result.push_str(&second_sep);
223 result.push_str(next_str);
228 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
229 _rewrite_comment(orig, false, shape, config, true)
232 pub fn rewrite_comment(
237 ) -> Option<String> {
238 _rewrite_comment(orig, block_style, shape, config, false)
246 is_doc_comment: bool,
247 ) -> Option<String> {
248 // If there are lines without a starting sigil, we won't format them correctly
249 // so in that case we won't even re-align (if !config.normalize_comments()) and
250 // we should stop now.
251 let num_bare_lines = orig.lines()
252 .map(|line| line.trim())
253 .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
255 if num_bare_lines > 0 && !config.normalize_comments() {
256 return Some(orig.to_owned());
258 if !config.normalize_comments() && !config.wrap_comments() {
259 return light_rewrite_comment(orig, shape.indent, config, is_doc_comment);
262 identify_comment(orig, block_style, shape, config, is_doc_comment)
270 is_doc_comment: bool,
271 ) -> Option<String> {
272 let style = comment_style(orig, false);
273 let first_group = orig.lines()
274 .take_while(|l| style.line_with_same_comment_style(l, false))
277 let rest = orig.lines()
278 .skip(first_group.lines().count())
282 let first_group_str = rewrite_comment_inner(
288 is_doc_comment || style.is_doc_comment(),
291 Some(first_group_str)
293 identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
297 shape.indent.to_string(config),
304 fn rewrite_comment_inner(
310 is_doc_comment: bool,
311 ) -> Option<String> {
312 let (opener, closer, line_start) = if block_style {
313 CommentStyle::SingleBullet.to_str_tuplet()
315 comment_style(orig, config.normalize_comments()).to_str_tuplet()
318 let max_chars = shape
320 .checked_sub(closer.len() + opener.len())
322 let indent_str = shape.indent.to_string_with_newline(config);
323 let fmt_indent = shape.indent + (opener.len() - line_start.len());
324 let mut fmt = StringFormat {
329 shape: Shape::legacy(max_chars, fmt_indent),
334 let line_breaks = count_newlines(orig.trim_right());
335 let lines = orig.lines()
337 .map(|(i, mut line)| {
338 line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
340 if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
341 line = line[..(line.len() - 2)].trim_right();
346 .map(|s| left_trim_comment_line(s, &style))
347 .map(|(line, has_leading_whitespace)| {
348 if orig.starts_with("/*") && line_breaks == 0 {
351 has_leading_whitespace || config.normalize_comments(),
354 (line, has_leading_whitespace || config.normalize_comments())
358 let mut result = String::with_capacity(orig.len() * 2);
359 result.push_str(opener);
360 let mut code_block_buffer = String::with_capacity(128);
361 let mut is_prev_line_multi_line = false;
362 let mut inside_code_block = false;
363 let comment_line_separator = format!("{}{}", indent_str, line_start);
364 let join_code_block_with_comment_line_separator = |s: &str| {
365 let mut result = String::with_capacity(s.len() + 128);
366 let mut iter = s.lines().peekable();
367 while let Some(line) = iter.next() {
368 result.push_str(line);
369 result.push_str(match iter.peek() {
370 Some(next_line) if next_line.is_empty() => comment_line_separator.trim_right(),
371 Some(..) => &comment_line_separator,
378 for (i, (line, has_leading_whitespace)) in lines.enumerate() {
379 let is_last = i == count_newlines(orig);
381 if inside_code_block {
382 if line.starts_with("```") {
383 inside_code_block = false;
384 result.push_str(&comment_line_separator);
385 let code_block = ::format_code_block(&code_block_buffer, config)
386 .unwrap_or_else(|| code_block_buffer.to_owned());
387 result.push_str(&join_code_block_with_comment_line_separator(&code_block));
388 code_block_buffer.clear();
389 result.push_str(&comment_line_separator);
390 result.push_str(line);
392 code_block_buffer.push_str(line);
393 code_block_buffer.push('\n');
396 // There is an code block that is not properly enclosed by backticks.
397 // We will leave them untouched.
398 result.push_str(&comment_line_separator);
399 result.push_str(&join_code_block_with_comment_line_separator(
407 inside_code_block = line.starts_with("```rust");
409 if result == opener {
410 let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
411 if !has_leading_whitespace && !force_leading_whitespace && result.ends_with(' ') {
417 } else if is_prev_line_multi_line && !line.is_empty() {
419 } else if is_last && !closer.is_empty() && line.is_empty() {
420 result.push_str(&indent_str);
422 result.push_str(&comment_line_separator);
423 if !has_leading_whitespace && result.ends_with(' ') {
429 if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
430 match rewrite_string(line, &fmt, Some(max_chars)) {
432 is_prev_line_multi_line = s.contains('\n');
435 None if is_prev_line_multi_line => {
436 // We failed to put the current `line` next to the previous `line`.
437 // Remove the trailing space, then start rewrite on the next line.
439 result.push_str(&comment_line_separator);
440 fmt.shape = Shape::legacy(max_chars, fmt_indent);
441 match rewrite_string(line, &fmt, Some(max_chars)) {
443 is_prev_line_multi_line = s.contains('\n');
447 is_prev_line_multi_line = false;
448 result.push_str(line);
453 is_prev_line_multi_line = false;
454 result.push_str(line);
458 fmt.shape = if is_prev_line_multi_line {
460 let offset = 1 + last_line_width(&result) - line_start.len();
462 width: max_chars.checked_sub(offset).unwrap_or(0),
464 offset: fmt.shape.offset + offset,
467 Shape::legacy(max_chars, fmt_indent)
470 if line.is_empty() && result.ends_with(' ') && !is_last {
471 // Remove space if this is an empty comment or a doc comment.
474 result.push_str(line);
475 fmt.shape = Shape::legacy(max_chars, fmt_indent);
476 is_prev_line_multi_line = false;
480 result.push_str(closer);
481 if result.ends_with(opener) && opener.ends_with(' ') {
489 /// Returns true if the given string MAY include URLs or alike.
490 fn has_url(s: &str) -> bool {
491 // This function may return false positive, but should get its job done in most cases.
492 s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
495 /// Given the span, rewrite the missing comment inside it if available.
496 /// Note that the given span must only include comments (or leading/trailing whitespaces).
497 pub fn rewrite_missing_comment(
500 context: &RewriteContext,
501 ) -> Option<String> {
502 let missing_snippet = context.snippet(span);
503 let trimmed_snippet = missing_snippet.trim();
504 if !trimmed_snippet.is_empty() {
505 rewrite_comment(trimmed_snippet, false, shape, context.config)
511 /// Recover the missing comments in the specified span, if available.
512 /// The layout of the comments will be preserved as long as it does not break the code
513 /// and its total width does not exceed the max width.
514 pub fn recover_missing_comment_in_span(
517 context: &RewriteContext,
519 ) -> Option<String> {
520 let missing_comment = rewrite_missing_comment(span, shape, context)?;
521 if missing_comment.is_empty() {
524 let missing_snippet = context.snippet(span);
525 let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
527 let total_width = missing_comment.len() + used_width + 1;
528 let force_new_line_before_comment =
529 missing_snippet[..pos].contains('\n') || total_width > context.config.max_width();
530 let sep = if force_new_line_before_comment {
531 shape.indent.to_string_with_newline(context.config)
535 Some(format!("{}{}", sep, missing_comment))
539 /// Trim trailing whitespaces unless they consist of two or more whitespaces.
540 fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
541 if is_doc_comment && s.ends_with(" ") {
548 /// Trims whitespace and aligns to indent, but otherwise does not change comments.
549 fn light_rewrite_comment(
553 is_doc_comment: bool,
554 ) -> Option<String> {
555 let lines: Vec<&str> = orig.lines()
557 // This is basically just l.trim(), but in the case that a line starts
558 // with `*` we want to leave one space before it, so it aligns with the
560 let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
561 let left_trimmed = if let Some(fnw) = first_non_whitespace {
562 if l.as_bytes()[fnw] == b'*' && fnw > 0 {
570 // Preserve markdown's double-space line break syntax in doc comment.
571 trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
574 Some(lines.join(&format!("\n{}", offset.to_string(config))))
577 /// Trims comment characters and possibly a single space from the left of a string.
578 /// Does not trim all whitespace. If a single space is trimmed from the left of the string,
579 /// this function returns true.
580 fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str, bool) {
581 if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ")
582 || line.starts_with("/** ")
585 } else if let CommentStyle::Custom(opener) = *style {
586 if line.starts_with(opener) {
587 (&line[opener.len()..], true)
589 (&line[opener.trim_right().len()..], false)
591 } else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!")
592 || line.starts_with("///") || line.starts_with("** ")
593 || line.starts_with("/*!")
594 || (line.starts_with("/**") && !line.starts_with("/**/"))
596 (&line[3..], line.chars().nth(2).unwrap() == ' ')
597 } else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//")
598 || line.starts_with("**")
600 (&line[2..], line.chars().nth(1).unwrap() == ' ')
601 } else if line.starts_with('*') {
604 (line, line.starts_with(' '))
608 pub trait FindUncommented {
609 fn find_uncommented(&self, pat: &str) -> Option<usize>;
612 impl FindUncommented for str {
613 fn find_uncommented(&self, pat: &str) -> Option<usize> {
614 let mut needle_iter = pat.chars();
615 for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
616 match needle_iter.next() {
618 return Some(i - pat.len());
620 Some(c) => match kind {
621 FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
623 needle_iter = pat.chars();
629 // Handle case where the pattern is a suffix of the search string
630 match needle_iter.next() {
632 None => Some(self.len() - pat.len()),
637 // Returns the first byte position after the first comment. The given string
638 // is expected to be prefixed by a comment, including delimiters.
639 // Good: "/* /* inner */ outer */ code();"
640 // Bad: "code(); // hello\n world!"
641 pub fn find_comment_end(s: &str) -> Option<usize> {
642 let mut iter = CharClasses::new(s.char_indices());
643 for (kind, (i, _c)) in &mut iter {
644 if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
649 // Handle case where the comment ends at the end of s.
650 if iter.status == CharClassesStatus::Normal {
657 /// Returns true if text contains any comment.
658 pub fn contains_comment(text: &str) -> bool {
659 CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
662 /// Remove trailing spaces from the specified snippet. We do not remove spaces
663 /// inside strings or comments.
664 pub fn remove_trailing_white_spaces(text: &str) -> String {
665 let mut buffer = String::with_capacity(text.len());
666 let mut space_buffer = String::with_capacity(128);
667 for (char_kind, c) in CharClasses::new(text.chars()) {
670 if char_kind == FullCodeCharKind::InString {
671 buffer.push_str(&space_buffer);
673 space_buffer.clear();
676 _ if c.is_whitespace() => {
677 space_buffer.push(c);
680 if !space_buffer.is_empty() {
681 buffer.push_str(&space_buffer);
682 space_buffer.clear();
691 pub struct CharClasses<T>
696 base: iter::Peekable<T>,
697 status: CharClassesStatus,
701 fn get_char(&self) -> char;
704 impl RichChar for char {
705 fn get_char(&self) -> char {
710 impl RichChar for (usize, char) {
711 fn get_char(&self) -> char {
716 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
717 enum CharClassesStatus {
723 // The u32 is the nesting deepness of the comment
725 // Status when the '/' has been consumed, but not yet the '*', deepness is
726 // the new deepness (after the comment opening).
727 BlockCommentOpening(u32),
728 // Status when the '*' has been consumed, but not yet the '/', deepness is
729 // the new deepness (after the comment closing).
730 BlockCommentClosing(u32),
734 /// Distinguish between functional part of code and comments
735 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
736 pub enum CodeCharKind {
741 /// Distinguish between functional part of code and comments,
742 /// describing opening and closing of comments for ease when chunking
743 /// code from tagged characters
744 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
745 pub enum FullCodeCharKind {
747 /// The first character of a comment, there is only one for a comment (always '/')
749 /// Any character inside a comment including the second character of comment
750 /// marks ("//", "/*")
752 /// Last character of a comment, '\n' for a line comment, '/' for a block comment.
758 impl FullCodeCharKind {
759 pub fn is_comment(&self) -> bool {
761 FullCodeCharKind::StartComment
762 | FullCodeCharKind::InComment
763 | FullCodeCharKind::EndComment => true,
768 pub fn is_string(&self) -> bool {
769 *self == FullCodeCharKind::InString
772 fn to_codecharkind(&self) -> CodeCharKind {
773 if self.is_comment() {
774 CodeCharKind::Comment
781 impl<T> CharClasses<T>
786 pub fn new(base: T) -> CharClasses<T> {
788 base: base.peekable(),
789 status: CharClassesStatus::Normal,
794 impl<T> Iterator for CharClasses<T>
799 type Item = (FullCodeCharKind, T::Item);
801 fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
802 let item = self.base.next()?;
803 let chr = item.get_char();
804 let mut char_kind = FullCodeCharKind::Normal;
805 self.status = match self.status {
806 CharClassesStatus::LitString => match chr {
807 '"' => CharClassesStatus::Normal,
809 char_kind = FullCodeCharKind::InString;
810 CharClassesStatus::LitStringEscape
813 char_kind = FullCodeCharKind::InString;
814 CharClassesStatus::LitString
817 CharClassesStatus::LitStringEscape => {
818 char_kind = FullCodeCharKind::InString;
819 CharClassesStatus::LitString
821 CharClassesStatus::LitChar => match chr {
822 '\\' => CharClassesStatus::LitCharEscape,
823 '\'' => CharClassesStatus::Normal,
824 _ => CharClassesStatus::LitChar,
826 CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
827 CharClassesStatus::Normal => match chr {
829 char_kind = FullCodeCharKind::InString;
830 CharClassesStatus::LitString
832 '\'' => CharClassesStatus::LitChar,
833 '/' => match self.base.peek() {
834 Some(next) if next.get_char() == '*' => {
835 self.status = CharClassesStatus::BlockCommentOpening(1);
836 return Some((FullCodeCharKind::StartComment, item));
838 Some(next) if next.get_char() == '/' => {
839 self.status = CharClassesStatus::LineComment;
840 return Some((FullCodeCharKind::StartComment, item));
842 _ => CharClassesStatus::Normal,
844 _ => CharClassesStatus::Normal,
846 CharClassesStatus::BlockComment(deepness) => {
847 assert_ne!(deepness, 0);
848 self.status = match self.base.peek() {
849 Some(next) if next.get_char() == '/' && chr == '*' => {
850 CharClassesStatus::BlockCommentClosing(deepness - 1)
852 Some(next) if next.get_char() == '*' && chr == '/' => {
853 CharClassesStatus::BlockCommentOpening(deepness + 1)
855 _ => CharClassesStatus::BlockComment(deepness),
857 return Some((FullCodeCharKind::InComment, item));
859 CharClassesStatus::BlockCommentOpening(deepness) => {
860 assert_eq!(chr, '*');
861 self.status = CharClassesStatus::BlockComment(deepness);
862 return Some((FullCodeCharKind::InComment, item));
864 CharClassesStatus::BlockCommentClosing(deepness) => {
865 assert_eq!(chr, '/');
867 self.status = CharClassesStatus::Normal;
868 return Some((FullCodeCharKind::EndComment, item));
870 self.status = CharClassesStatus::BlockComment(deepness);
871 return Some((FullCodeCharKind::InComment, item));
874 CharClassesStatus::LineComment => match chr {
876 self.status = CharClassesStatus::Normal;
877 return Some((FullCodeCharKind::EndComment, item));
880 self.status = CharClassesStatus::LineComment;
881 return Some((FullCodeCharKind::InComment, item));
885 Some((char_kind, item))
889 /// Iterator over functional and commented parts of a string. Any part of a string is either
890 /// functional code, either *one* block comment, either *one* line comment. Whitespace between
891 /// comments is functional code. Line comments contain their ending newlines.
892 struct UngroupedCommentCodeSlices<'a> {
894 iter: iter::Peekable<CharClasses<std::str::CharIndices<'a>>>,
897 impl<'a> UngroupedCommentCodeSlices<'a> {
898 fn new(code: &'a str) -> UngroupedCommentCodeSlices<'a> {
899 UngroupedCommentCodeSlices {
901 iter: CharClasses::new(code.char_indices()).peekable(),
906 impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
907 type Item = (CodeCharKind, usize, &'a str);
909 fn next(&mut self) -> Option<Self::Item> {
910 let (kind, (start_idx, _)) = self.iter.next()?;
912 FullCodeCharKind::Normal | FullCodeCharKind::InString => {
913 // Consume all the Normal code
914 while let Some(&(char_kind, _)) = self.iter.peek() {
915 if char_kind.is_comment() {
918 let _ = self.iter.next();
921 FullCodeCharKind::StartComment => {
922 // Consume the whole comment
923 while let Some((FullCodeCharKind::InComment, (_, _))) = self.iter.next() {}
927 let slice = match self.iter.peek() {
928 Some(&(_, (end_idx, _))) => &self.slice[start_idx..end_idx],
929 None => &self.slice[start_idx..],
932 if kind.is_comment() {
933 CodeCharKind::Comment
943 /// Iterator over an alternating sequence of functional and commented parts of
944 /// a string. The first item is always a, possibly zero length, subslice of
945 /// functional text. Line style comments contain their ending newlines.
946 pub struct CommentCodeSlices<'a> {
948 last_slice_kind: CodeCharKind,
949 last_slice_end: usize,
952 impl<'a> CommentCodeSlices<'a> {
953 pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
956 last_slice_kind: CodeCharKind::Comment,
962 impl<'a> Iterator for CommentCodeSlices<'a> {
963 type Item = (CodeCharKind, usize, &'a str);
965 fn next(&mut self) -> Option<Self::Item> {
966 if self.last_slice_end == self.slice.len() {
970 let mut sub_slice_end = self.last_slice_end;
971 let mut first_whitespace = None;
972 let subslice = &self.slice[self.last_slice_end..];
973 let mut iter = CharClasses::new(subslice.char_indices());
975 for (kind, (i, c)) in &mut iter {
976 let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal
977 && &subslice[..2] == "//"
978 && [' ', '\t'].contains(&c);
980 if is_comment_connector && first_whitespace.is_none() {
981 first_whitespace = Some(i);
984 if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
985 let last_index = match first_whitespace {
989 sub_slice_end = self.last_slice_end + last_index;
993 if !is_comment_connector {
994 first_whitespace = None;
998 if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
999 // This was the last subslice.
1000 sub_slice_end = match first_whitespace {
1001 Some(i) => self.last_slice_end + i,
1002 None => self.slice.len(),
1006 let kind = match self.last_slice_kind {
1007 CodeCharKind::Comment => CodeCharKind::Normal,
1008 CodeCharKind::Normal => CodeCharKind::Comment,
1012 self.last_slice_end,
1013 &self.slice[self.last_slice_end..sub_slice_end],
1015 self.last_slice_end = sub_slice_end;
1016 self.last_slice_kind = kind;
1022 /// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1023 /// (if it fits in the width/offset, else return None), else return `new`
1024 pub fn recover_comment_removed(
1027 context: &RewriteContext,
1028 ) -> Option<String> {
1029 let snippet = context.snippet(span);
1030 if snippet != new && changed_comment_content(snippet, &new) {
1031 // We missed some comments. Keep the original text.
1032 Some(snippet.to_owned())
1038 /// Return true if the two strings of code have the same payload of comments.
1039 /// The payload of comments is everything in the string except:
1040 /// - actual code (not comments)
1041 /// - comment start/end marks
1043 /// - '*' at the beginning of lines in block comments
1044 fn changed_comment_content(orig: &str, new: &str) -> bool {
1045 // Cannot write this as a fn since we cannot return types containing closures
1046 let code_comment_content = |code| {
1047 let slices = UngroupedCommentCodeSlices::new(code);
1049 .filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
1050 .flat_map(|(_, _, s)| CommentReducer::new(s))
1052 let res = code_comment_content(orig).ne(code_comment_content(new));
1054 "comment::changed_comment_content: {}\norig: '{}'\nnew: '{}'\nraw_old: {}\nraw_new: {}",
1058 code_comment_content(orig).collect::<String>(),
1059 code_comment_content(new).collect::<String>()
1064 /// Iterator over the 'payload' characters of a comment.
1065 /// It skips whitespace, comment start/end marks, and '*' at the beginning of lines.
1066 /// The comment must be one comment, ie not more than one start mark (no multiple line comments,
1068 struct CommentReducer<'a> {
1070 at_start_line: bool,
1071 iter: std::str::Chars<'a>,
1074 impl<'a> CommentReducer<'a> {
1075 fn new(comment: &'a str) -> CommentReducer<'a> {
1076 let is_block = comment.starts_with("/*");
1077 let comment = remove_comment_header(comment);
1080 at_start_line: false, // There are no supplementary '*' on the first line
1081 iter: comment.chars(),
1086 impl<'a> Iterator for CommentReducer<'a> {
1088 fn next(&mut self) -> Option<Self::Item> {
1090 let mut c = self.iter.next()?;
1091 if self.is_block && self.at_start_line {
1092 while c.is_whitespace() {
1093 c = self.iter.next()?;
1095 // Ignore leading '*'
1097 c = self.iter.next()?;
1099 } else if c == '\n' {
1100 self.at_start_line = true;
1102 if !c.is_whitespace() {
1109 fn remove_comment_header(comment: &str) -> &str {
1110 if comment.starts_with("///") || comment.starts_with("//!") {
1112 } else if comment.starts_with("//") {
1114 } else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
1115 || comment.starts_with("/*!")
1117 &comment[3..comment.len() - 2]
1120 comment.starts_with("/*"),
1121 format!("string '{}' is not a comment", comment)
1123 &comment[2..comment.len() - 2]
1129 use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
1130 FindUncommented, FullCodeCharKind};
1131 use shape::{Indent, Shape};
1135 let mut iter = CharClasses::new("//\n\n".chars());
1137 assert_eq!((FullCodeCharKind::StartComment, '/'), iter.next().unwrap());
1138 assert_eq!((FullCodeCharKind::InComment, '/'), iter.next().unwrap());
1139 assert_eq!((FullCodeCharKind::EndComment, '\n'), iter.next().unwrap());
1140 assert_eq!((FullCodeCharKind::Normal, '\n'), iter.next().unwrap());
1141 assert_eq!(None, iter.next());
1145 fn comment_code_slices() {
1146 let input = "code(); /* test */ 1 + 1";
1147 let mut iter = CommentCodeSlices::new(input);
1149 assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
1151 (CodeCharKind::Comment, 8, "/* test */"),
1152 iter.next().unwrap()
1154 assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
1155 assert_eq!(None, iter.next());
1159 fn comment_code_slices_two() {
1160 let input = "// comment\n test();";
1161 let mut iter = CommentCodeSlices::new(input);
1163 assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
1165 (CodeCharKind::Comment, 0, "// comment\n"),
1166 iter.next().unwrap()
1169 (CodeCharKind::Normal, 11, " test();"),
1170 iter.next().unwrap()
1172 assert_eq!(None, iter.next());
1176 fn comment_code_slices_three() {
1177 let input = "1 // comment\n // comment2\n\n";
1178 let mut iter = CommentCodeSlices::new(input);
1180 assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
1182 (CodeCharKind::Comment, 2, "// comment\n // comment2\n"),
1183 iter.next().unwrap()
1185 assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
1186 assert_eq!(None, iter.next());
1190 #[cfg_attr(rustfmt, rustfmt_skip)]
1191 fn format_comments() {
1192 let mut config: ::config::Config = Default::default();
1193 config.set().wrap_comments(true);
1194 config.set().normalize_comments(true);
1196 let comment = rewrite_comment(" //test",
1198 Shape::legacy(100, Indent::new(0, 100)),
1200 assert_eq!("/* test */", comment);
1202 let comment = rewrite_comment("// comment on a",
1204 Shape::legacy(10, Indent::empty()),
1206 assert_eq!("// comment\n// on a", comment);
1208 let comment = rewrite_comment("// A multi line comment\n // between args.",
1210 Shape::legacy(60, Indent::new(0, 12)),
1212 assert_eq!("// A multi line comment\n // between args.", comment);
1214 let input = "// comment";
1217 let comment = rewrite_comment(input,
1219 Shape::legacy(9, Indent::new(0, 69)),
1221 assert_eq!(expected, comment);
1223 let comment = rewrite_comment("/* trimmed */",
1225 Shape::legacy(100, Indent::new(0, 100)),
1227 assert_eq!("/* trimmed */", comment);
1230 // This is probably intended to be a non-test fn, but it is not used. I'm
1231 // keeping it around unless it helps us test stuff.
1232 fn uncommented(text: &str) -> String {
1233 CharClasses::new(text.chars())
1234 .filter_map(|(s, c)| match s {
1235 FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
1242 fn test_uncommented() {
1243 assert_eq!(&uncommented("abc/*...*/"), "abc");
1245 &uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
1248 assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
1252 fn test_contains_comment() {
1253 assert_eq!(contains_comment("abc"), false);
1254 assert_eq!(contains_comment("abc // qsdf"), true);
1255 assert_eq!(contains_comment("abc /* kqsdf"), true);
1256 assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
1260 fn test_find_uncommented() {
1261 fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1262 assert_eq!(expected, haystack.find_uncommented(needle));
1265 check("/*/ */test", "test", Some(6));
1266 check("//test\ntest", "test", Some(7));
1267 check("/* comment only */", "whatever", None);
1269 "/* comment */ some text /* more commentary */ result",
1273 check("sup // sup", "p", Some(2));
1274 check("sup", "x", None);
1275 check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
1276 check("/*sup yo? \n sup*/ sup", "p", Some(20));
1277 check("hel/*lohello*/lo", "hello", None);
1278 check("acb", "ab", None);
1279 check(",/*A*/ ", ",", Some(0));
1280 check("abc", "abc", Some(0));
1281 check("/* abc */", "abc", None);
1282 check("/**/abc/* */", "abc", Some(4));
1283 check("\"/* abc */\"", "abc", Some(4));
1284 check("\"/* abc", "abc", Some(4));