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, borrow::Cow, iter};
15 use itertools::{multipeek, MultiPeek};
16 use syntax::source_map::Span;
19 use rewrite::RewriteContext;
20 use shape::{Indent, Shape};
21 use string::{rewrite_string, StringFormat};
22 use utils::{count_newlines, first_line_width, last_line_width};
23 use {ErrorKind, FormattingError};
25 fn is_custom_comment(comment: &str) -> bool {
26 if !comment.starts_with("//") {
28 } else if let Some(c) = comment.chars().nth(2) {
29 !c.is_alphanumeric() && !c.is_whitespace()
35 #[derive(Copy, Clone, PartialEq, Eq)]
36 pub enum CommentStyle<'a> {
46 fn custom_opener(s: &str) -> &str {
47 s.lines().next().map_or("", |first_line| {
50 .map_or(first_line, |space_index| &first_line[0..=space_index])
54 impl<'a> CommentStyle<'a> {
55 /// Returns true if the commenting style covers a line only.
56 pub fn is_line_comment(&self) -> bool {
58 CommentStyle::DoubleSlash
59 | CommentStyle::TripleSlash
61 | CommentStyle::Custom(_) => true,
66 /// Returns true if the commenting style can span over multiple lines.
67 pub fn is_block_comment(&self) -> bool {
69 CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
76 pub fn is_doc_comment(&self) -> bool {
78 CommentStyle::TripleSlash | CommentStyle::Doc => true,
83 pub fn opener(&self) -> &'a str {
85 CommentStyle::DoubleSlash => "// ",
86 CommentStyle::TripleSlash => "/// ",
87 CommentStyle::Doc => "//! ",
88 CommentStyle::SingleBullet => "/* ",
89 CommentStyle::DoubleBullet => "/** ",
90 CommentStyle::Exclamation => "/*! ",
91 CommentStyle::Custom(opener) => opener,
95 pub fn closer(&self) -> &'a str {
97 CommentStyle::DoubleSlash
98 | CommentStyle::TripleSlash
99 | CommentStyle::Custom(..)
100 | CommentStyle::Doc => "",
101 CommentStyle::DoubleBullet => " **/",
102 CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
106 pub fn line_start(&self) -> &'a str {
108 CommentStyle::DoubleSlash => "// ",
109 CommentStyle::TripleSlash => "/// ",
110 CommentStyle::Doc => "//! ",
111 CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
112 CommentStyle::DoubleBullet => " ** ",
113 CommentStyle::Custom(opener) => opener,
117 pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
118 (self.opener(), self.closer(), self.line_start())
122 fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
123 if !normalize_comments {
124 if orig.starts_with("/**") && !orig.starts_with("/**/") {
125 CommentStyle::DoubleBullet
126 } else if orig.starts_with("/*!") {
127 CommentStyle::Exclamation
128 } else if orig.starts_with("/*") {
129 CommentStyle::SingleBullet
130 } else if orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/') {
131 CommentStyle::TripleSlash
132 } else if orig.starts_with("//!") {
134 } else if is_custom_comment(orig) {
135 CommentStyle::Custom(custom_opener(orig))
137 CommentStyle::DoubleSlash
139 } else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/'))
140 || (orig.starts_with("/**") && !orig.starts_with("/**/"))
142 CommentStyle::TripleSlash
143 } else if orig.starts_with("//!") || orig.starts_with("/*!") {
145 } else if is_custom_comment(orig) {
146 CommentStyle::Custom(custom_opener(orig))
148 CommentStyle::DoubleSlash
152 /// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
153 /// comments between two strings. If there are such comments, then that will be
154 /// recovered. If `allow_extend` is true and there is no comment between the two
155 /// strings, then they will be put on a single line as long as doing so does not
156 /// exceed max width.
157 pub fn combine_strs_with_missing_comments(
158 context: &RewriteContext,
164 ) -> Option<String> {
166 String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
167 result.push_str(prev_str);
168 let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
169 let first_sep = if prev_str.is_empty() || next_str.is_empty() {
174 let mut one_line_width =
175 last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
177 let config = context.config;
178 let indent = shape.indent;
179 let missing_comment = rewrite_missing_comment(span, shape, context)?;
181 if missing_comment.is_empty() {
182 if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
183 result.push_str(first_sep);
184 } else if !prev_str.is_empty() {
185 result.push_str(&indent.to_string_with_newline(config))
187 result.push_str(next_str);
191 // We have a missing comment between the first expression and the second expression.
193 // Peek the the original source code and find out whether there is a newline between the first
194 // expression and the second expression or the missing comment. We will preserve the original
195 // layout whenever possible.
196 let original_snippet = context.snippet(span);
197 let prefer_same_line = if let Some(pos) = original_snippet.find('/') {
198 !original_snippet[..pos].contains('\n')
200 !original_snippet.contains('\n')
203 one_line_width -= first_sep.len();
204 let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
207 let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
208 if prefer_same_line && one_line_width <= shape.width {
211 indent.to_string_with_newline(config)
214 result.push_str(&first_sep);
215 result.push_str(&missing_comment);
217 let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
219 } else if missing_comment.starts_with("//") {
220 indent.to_string_with_newline(config)
222 one_line_width += missing_comment.len() + first_sep.len() + 1;
223 allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
224 if prefer_same_line && allow_one_line && one_line_width <= shape.width {
227 indent.to_string_with_newline(config)
230 result.push_str(&second_sep);
231 result.push_str(next_str);
236 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
237 identify_comment(orig, false, shape, config, true)
240 pub fn rewrite_comment(
245 ) -> Option<String> {
246 identify_comment(orig, block_style, shape, config, false)
254 is_doc_comment: bool,
255 ) -> Option<String> {
256 let style = comment_style(orig, false);
258 // Computes the len of line taking into account a newline if the line is part of a paragraph.
259 fn compute_len(orig: &str, line: &str) -> usize {
260 if orig.len() > line.len() {
261 if orig.as_bytes()[line.len()] == b'\r' {
271 // Get the first group of line comments having the same commenting style.
273 // Returns a tuple with:
274 // - a boolean indicating if there is a blank line
275 // - a number indicating the size of the first group of comments
276 fn consume_same_line_comments(
281 let mut first_group_ending = 0;
284 for line in orig.lines() {
285 let trimmed_line = line.trim_left();
286 if trimmed_line.is_empty() {
289 } else if trimmed_line.starts_with(line_start)
290 || comment_style(trimmed_line, false) == style
292 first_group_ending += compute_len(&orig[first_group_ending..], line);
297 (hbl, first_group_ending)
300 let (has_bare_lines, first_group_ending) = match style {
301 CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
302 let line_start = style.line_start().trim_left();
303 consume_same_line_comments(style, orig, line_start)
305 CommentStyle::Custom(opener) => {
306 let trimmed_opener = opener.trim_right();
307 consume_same_line_comments(style, orig, trimmed_opener)
309 // for a block comment, search for the closing symbol
310 CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
311 let closer = style.closer().trim_left();
312 let mut closing_symbol_offset = 0;
314 for line in orig.lines() {
315 closing_symbol_offset += compute_len(&orig[closing_symbol_offset..], line);
316 let trimmed_line = line.trim_left();
317 if !trimmed_line.starts_with('*')
318 && !trimmed_line.starts_with("//")
319 && !trimmed_line.starts_with("/*")
323 if trimmed_line.ends_with(closer) {
327 (hbl, closing_symbol_offset)
331 let (first_group, rest) = orig.split_at(first_group_ending);
332 let rewritten_first_group =
333 if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
334 light_rewrite_block_comment_with_bare_lines(first_group, shape, config)?
335 } else if !config.normalize_comments() && !config.wrap_comments() {
336 light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)?
338 rewrite_comment_inner(
344 is_doc_comment || style.is_doc_comment(),
348 Some(rewritten_first_group)
350 identify_comment(rest.trim_left(), block_style, shape, config, is_doc_comment).map(
354 rewritten_first_group,
355 // insert back the blank line
356 if has_bare_lines && style.is_line_comment() {
361 shape.indent.to_string(config),
369 /// Trims a minimum of leading whitespaces so that the content layout is kept and aligns to indent.
370 fn light_rewrite_block_comment_with_bare_lines(
374 ) -> Option<String> {
375 let prefix_whitespace_min = orig
377 // skip the line with the starting sigil since the leading whitespace is removed
378 // otherwise, the minimum would always be zero
380 .filter(|line| !line.is_empty())
383 for c in line.chars() {
386 '\t' => width += config.tab_spaces(),
394 let indent_str = shape.indent.to_string(config);
395 let mut lines = orig.lines();
396 let first_line = lines.next()?;
402 &line[prefix_whitespace_min..]
405 .collect::<Vec<&str>>()
406 .join(&format!("\n{}", indent_str));
407 Some(format!("{}\n{}{}", first_line, indent_str, rest))
410 fn rewrite_comment_inner(
416 is_doc_comment: bool,
417 ) -> Option<String> {
418 let (opener, closer, line_start) = if block_style {
419 CommentStyle::SingleBullet.to_str_tuplet()
421 comment_style(orig, config.normalize_comments()).to_str_tuplet()
424 let max_chars = shape
426 .checked_sub(closer.len() + opener.len())
428 let indent_str = shape.indent.to_string_with_newline(config);
429 let fmt_indent = shape.indent + (opener.len() - line_start.len());
430 let mut fmt = StringFormat {
435 shape: Shape::legacy(max_chars, fmt_indent),
440 let line_breaks = count_newlines(orig.trim_right());
444 .map(|(i, mut line)| {
445 line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
447 if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
448 line = line[..(line.len() - 2)].trim_right();
453 .map(|s| left_trim_comment_line(s, &style))
454 .map(|(line, has_leading_whitespace)| {
455 if orig.starts_with("/*") && line_breaks == 0 {
458 has_leading_whitespace || config.normalize_comments(),
461 (line, has_leading_whitespace || config.normalize_comments())
465 let mut result = String::with_capacity(orig.len() * 2);
466 result.push_str(opener);
467 let mut code_block_buffer = String::with_capacity(128);
468 let mut is_prev_line_multi_line = false;
469 let mut inside_code_block = false;
470 let comment_line_separator = format!("{}{}", indent_str, line_start);
471 let join_code_block_with_comment_line_separator = |s: &str| {
472 let mut result = String::with_capacity(s.len() + 128);
473 let mut iter = s.lines().peekable();
474 while let Some(line) = iter.next() {
475 result.push_str(line);
476 result.push_str(match iter.peek() {
477 Some(next_line) if next_line.is_empty() => comment_line_separator.trim_right(),
478 Some(..) => &comment_line_separator,
485 for (i, (line, has_leading_whitespace)) in lines.enumerate() {
486 let is_last = i == count_newlines(orig);
488 if inside_code_block {
489 if line.starts_with("```") {
490 inside_code_block = false;
491 result.push_str(&comment_line_separator);
493 let mut config = config.clone();
494 config.set().wrap_comments(false);
495 match ::format_code_block(&code_block_buffer, &config) {
496 Some(ref s) => trim_custom_comment_prefix(s),
497 None => trim_custom_comment_prefix(&code_block_buffer),
500 result.push_str(&join_code_block_with_comment_line_separator(&code_block));
501 code_block_buffer.clear();
502 result.push_str(&comment_line_separator);
503 result.push_str(line);
505 code_block_buffer.push_str(&hide_sharp_behind_comment(line));
506 code_block_buffer.push('\n');
509 // There is an code block that is not properly enclosed by backticks.
510 // We will leave them untouched.
511 result.push_str(&comment_line_separator);
512 result.push_str(&join_code_block_with_comment_line_separator(
513 &trim_custom_comment_prefix(&code_block_buffer),
520 inside_code_block = line.starts_with("```");
522 if result == opener {
523 let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
524 if !has_leading_whitespace && !force_leading_whitespace && result.ends_with(' ') {
530 } else if is_prev_line_multi_line && !line.is_empty() {
532 } else if is_last && line.is_empty() {
533 // trailing blank lines are unwanted
534 if !closer.is_empty() {
535 result.push_str(&indent_str);
539 result.push_str(&comment_line_separator);
540 if !has_leading_whitespace && result.ends_with(' ') {
546 if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
547 match rewrite_string(line, &fmt) {
549 is_prev_line_multi_line = s.contains('\n');
552 None if is_prev_line_multi_line => {
553 // We failed to put the current `line` next to the previous `line`.
554 // Remove the trailing space, then start rewrite on the next line.
556 result.push_str(&comment_line_separator);
557 fmt.shape = Shape::legacy(max_chars, fmt_indent);
558 match rewrite_string(line, &fmt) {
560 is_prev_line_multi_line = s.contains('\n');
564 is_prev_line_multi_line = false;
565 result.push_str(line);
570 is_prev_line_multi_line = false;
571 result.push_str(line);
575 fmt.shape = if is_prev_line_multi_line {
577 let offset = 1 + last_line_width(&result) - line_start.len();
579 width: max_chars.saturating_sub(offset),
581 offset: fmt.shape.offset + offset,
584 Shape::legacy(max_chars, fmt_indent)
587 if line.is_empty() && result.ends_with(' ') && !is_last {
588 // Remove space if this is an empty comment or a doc comment.
591 result.push_str(line);
592 fmt.shape = Shape::legacy(max_chars, fmt_indent);
593 is_prev_line_multi_line = false;
597 result.push_str(closer);
598 if result.ends_with(opener) && opener.ends_with(' ') {
606 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
608 fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
609 if s.trim_left().starts_with("# ") {
610 Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
616 fn trim_custom_comment_prefix(s: &str) -> String {
619 let left_trimmed = line.trim_left();
620 if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
621 left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
630 /// Returns true if the given string MAY include URLs or alike.
631 fn has_url(s: &str) -> bool {
632 // This function may return false positive, but should get its job done in most cases.
633 s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
636 /// Given the span, rewrite the missing comment inside it if available.
637 /// Note that the given span must only include comments (or leading/trailing whitespaces).
638 pub fn rewrite_missing_comment(
641 context: &RewriteContext,
642 ) -> Option<String> {
643 let missing_snippet = context.snippet(span);
644 let trimmed_snippet = missing_snippet.trim();
645 if !trimmed_snippet.is_empty() {
646 rewrite_comment(trimmed_snippet, false, shape, context.config)
652 /// Recover the missing comments in the specified span, if available.
653 /// The layout of the comments will be preserved as long as it does not break the code
654 /// and its total width does not exceed the max width.
655 pub fn recover_missing_comment_in_span(
658 context: &RewriteContext,
660 ) -> Option<String> {
661 let missing_comment = rewrite_missing_comment(span, shape, context)?;
662 if missing_comment.is_empty() {
665 let missing_snippet = context.snippet(span);
666 let pos = missing_snippet.find('/').unwrap_or(0);
668 let total_width = missing_comment.len() + used_width + 1;
669 let force_new_line_before_comment =
670 missing_snippet[..pos].contains('\n') || total_width > context.config.max_width();
671 let sep = if force_new_line_before_comment {
672 shape.indent.to_string_with_newline(context.config)
676 Some(format!("{}{}", sep, missing_comment))
680 /// Trim trailing whitespaces unless they consist of two or more whitespaces.
681 fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
682 if is_doc_comment && s.ends_with(" ") {
689 /// Trims whitespace and aligns to indent, but otherwise does not change comments.
690 fn light_rewrite_comment(
694 is_doc_comment: bool,
695 ) -> Option<String> {
696 let lines: Vec<&str> = orig
699 // This is basically just l.trim(), but in the case that a line starts
700 // with `*` we want to leave one space before it, so it aligns with the
702 let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
703 let left_trimmed = if let Some(fnw) = first_non_whitespace {
704 if l.as_bytes()[fnw] == b'*' && fnw > 0 {
712 // Preserve markdown's double-space line break syntax in doc comment.
713 trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
716 Some(lines.join(&format!("\n{}", offset.to_string(config))))
719 /// Trims comment characters and possibly a single space from the left of a string.
720 /// Does not trim all whitespace. If a single space is trimmed from the left of the string,
721 /// this function returns true.
722 fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str, bool) {
723 if line.starts_with("//! ")
724 || line.starts_with("/// ")
725 || line.starts_with("/*! ")
726 || line.starts_with("/** ")
729 } else if let CommentStyle::Custom(opener) = *style {
730 if line.starts_with(opener) {
731 (&line[opener.len()..], true)
733 (&line[opener.trim_right().len()..], false)
735 } else if line.starts_with("/* ")
736 || line.starts_with("// ")
737 || line.starts_with("//!")
738 || line.starts_with("///")
739 || line.starts_with("** ")
740 || line.starts_with("/*!")
741 || (line.starts_with("/**") && !line.starts_with("/**/"))
743 (&line[3..], line.chars().nth(2).unwrap() == ' ')
744 } else if line.starts_with("/*")
745 || line.starts_with("* ")
746 || line.starts_with("//")
747 || line.starts_with("**")
749 (&line[2..], line.chars().nth(1).unwrap() == ' ')
750 } else if line.starts_with('*') {
753 (line, line.starts_with(' '))
757 pub trait FindUncommented {
758 fn find_uncommented(&self, pat: &str) -> Option<usize>;
761 impl FindUncommented for str {
762 fn find_uncommented(&self, pat: &str) -> Option<usize> {
763 let mut needle_iter = pat.chars();
764 for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
765 match needle_iter.next() {
767 return Some(i - pat.len());
769 Some(c) => match kind {
770 FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
772 needle_iter = pat.chars();
778 // Handle case where the pattern is a suffix of the search string
779 match needle_iter.next() {
781 None => Some(self.len() - pat.len()),
786 // Returns the first byte position after the first comment. The given string
787 // is expected to be prefixed by a comment, including delimiters.
788 // Good: "/* /* inner */ outer */ code();"
789 // Bad: "code(); // hello\n world!"
790 pub fn find_comment_end(s: &str) -> Option<usize> {
791 let mut iter = CharClasses::new(s.char_indices());
792 for (kind, (i, _c)) in &mut iter {
793 if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
798 // Handle case where the comment ends at the end of s.
799 if iter.status == CharClassesStatus::Normal {
806 /// Returns true if text contains any comment.
807 pub fn contains_comment(text: &str) -> bool {
808 CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
811 /// Remove trailing spaces from the specified snippet. We do not remove spaces
812 /// inside strings or comments.
813 pub fn remove_trailing_white_spaces(text: &str) -> String {
814 let mut buffer = String::with_capacity(text.len());
815 let mut space_buffer = String::with_capacity(128);
816 for (char_kind, c) in CharClasses::new(text.chars()) {
819 if char_kind == FullCodeCharKind::InString {
820 buffer.push_str(&space_buffer);
822 space_buffer.clear();
825 _ if c.is_whitespace() => {
826 space_buffer.push(c);
829 if !space_buffer.is_empty() {
830 buffer.push_str(&space_buffer);
831 space_buffer.clear();
840 pub struct CharClasses<T>
846 status: CharClassesStatus,
850 fn get_char(&self) -> char;
853 impl RichChar for char {
854 fn get_char(&self) -> char {
859 impl RichChar for (usize, char) {
860 fn get_char(&self) -> char {
865 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
866 enum CharClassesStatus {
871 RawStringPrefix(u32),
872 RawStringSuffix(u32),
875 // The u32 is the nesting deepness of the comment
877 // Status when the '/' has been consumed, but not yet the '*', deepness is
878 // the new deepness (after the comment opening).
879 BlockCommentOpening(u32),
880 // Status when the '*' has been consumed, but not yet the '/', deepness is
881 // the new deepness (after the comment closing).
882 BlockCommentClosing(u32),
886 /// Distinguish between functional part of code and comments
887 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
888 pub enum CodeCharKind {
893 /// Distinguish between functional part of code and comments,
894 /// describing opening and closing of comments for ease when chunking
895 /// code from tagged characters
896 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
897 pub enum FullCodeCharKind {
899 /// The first character of a comment, there is only one for a comment (always '/')
901 /// Any character inside a comment including the second character of comment
902 /// marks ("//", "/*")
904 /// Last character of a comment, '\n' for a line comment, '/' for a block comment.
906 /// Start of a mutlitine string
908 /// End of a mutlitine string
914 impl FullCodeCharKind {
915 pub fn is_comment(self) -> bool {
917 FullCodeCharKind::StartComment
918 | FullCodeCharKind::InComment
919 | FullCodeCharKind::EndComment => true,
924 pub fn is_string(self) -> bool {
925 self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
928 fn to_codecharkind(self) -> CodeCharKind {
929 if self.is_comment() {
930 CodeCharKind::Comment
937 impl<T> CharClasses<T>
942 pub fn new(base: T) -> CharClasses<T> {
944 base: multipeek(base),
945 status: CharClassesStatus::Normal,
950 fn is_raw_string_suffix<T>(iter: &mut MultiPeek<T>, count: u32) -> bool
957 Some(c) if c.get_char() == '#' => continue,
964 impl<T> Iterator for CharClasses<T>
969 type Item = (FullCodeCharKind, T::Item);
971 fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
972 let item = self.base.next()?;
973 let chr = item.get_char();
974 let mut char_kind = FullCodeCharKind::Normal;
975 self.status = match self.status {
976 CharClassesStatus::LitRawString(sharps) => {
977 char_kind = FullCodeCharKind::InString;
981 char_kind = FullCodeCharKind::Normal;
982 CharClassesStatus::Normal
983 } else if is_raw_string_suffix(&mut self.base, sharps) {
984 CharClassesStatus::RawStringSuffix(sharps)
986 CharClassesStatus::LitRawString(sharps)
989 _ => CharClassesStatus::LitRawString(sharps),
992 CharClassesStatus::RawStringPrefix(sharps) => {
993 char_kind = FullCodeCharKind::InString;
995 '#' => CharClassesStatus::RawStringPrefix(sharps + 1),
996 '"' => CharClassesStatus::LitRawString(sharps),
997 _ => CharClassesStatus::Normal, // Unreachable.
1000 CharClassesStatus::RawStringSuffix(sharps) => {
1004 CharClassesStatus::Normal
1006 char_kind = FullCodeCharKind::InString;
1007 CharClassesStatus::RawStringSuffix(sharps - 1)
1010 _ => CharClassesStatus::Normal, // Unreachable
1013 CharClassesStatus::LitString => {
1014 char_kind = FullCodeCharKind::InString;
1016 '"' => CharClassesStatus::Normal,
1017 '\\' => CharClassesStatus::LitStringEscape,
1018 _ => CharClassesStatus::LitString,
1021 CharClassesStatus::LitStringEscape => {
1022 char_kind = FullCodeCharKind::InString;
1023 CharClassesStatus::LitString
1025 CharClassesStatus::LitChar => match chr {
1026 '\\' => CharClassesStatus::LitCharEscape,
1027 '\'' => CharClassesStatus::Normal,
1028 _ => CharClassesStatus::LitChar,
1030 CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
1031 CharClassesStatus::Normal => match chr {
1032 'r' => match self.base.peek().map(|c| c.get_char()) {
1033 Some('#') | Some('"') => {
1034 char_kind = FullCodeCharKind::InString;
1035 CharClassesStatus::RawStringPrefix(0)
1037 _ => CharClassesStatus::Normal,
1040 char_kind = FullCodeCharKind::InString;
1041 CharClassesStatus::LitString
1044 // HACK: Work around mut borrow.
1045 match self.base.peek() {
1046 Some(next) if next.get_char() == '\\' => {
1047 self.status = CharClassesStatus::LitChar;
1048 return Some((char_kind, item));
1053 match self.base.peek() {
1054 Some(next) if next.get_char() == '\'' => CharClassesStatus::LitChar,
1055 _ => CharClassesStatus::Normal,
1058 '/' => match self.base.peek() {
1059 Some(next) if next.get_char() == '*' => {
1060 self.status = CharClassesStatus::BlockCommentOpening(1);
1061 return Some((FullCodeCharKind::StartComment, item));
1063 Some(next) if next.get_char() == '/' => {
1064 self.status = CharClassesStatus::LineComment;
1065 return Some((FullCodeCharKind::StartComment, item));
1067 _ => CharClassesStatus::Normal,
1069 _ => CharClassesStatus::Normal,
1071 CharClassesStatus::BlockComment(deepness) => {
1072 assert_ne!(deepness, 0);
1073 self.status = match self.base.peek() {
1074 Some(next) if next.get_char() == '/' && chr == '*' => {
1075 CharClassesStatus::BlockCommentClosing(deepness - 1)
1077 Some(next) if next.get_char() == '*' && chr == '/' => {
1078 CharClassesStatus::BlockCommentOpening(deepness + 1)
1080 _ => CharClassesStatus::BlockComment(deepness),
1082 return Some((FullCodeCharKind::InComment, item));
1084 CharClassesStatus::BlockCommentOpening(deepness) => {
1085 assert_eq!(chr, '*');
1086 self.status = CharClassesStatus::BlockComment(deepness);
1087 return Some((FullCodeCharKind::InComment, item));
1089 CharClassesStatus::BlockCommentClosing(deepness) => {
1090 assert_eq!(chr, '/');
1092 self.status = CharClassesStatus::Normal;
1093 return Some((FullCodeCharKind::EndComment, item));
1095 self.status = CharClassesStatus::BlockComment(deepness);
1096 return Some((FullCodeCharKind::InComment, item));
1099 CharClassesStatus::LineComment => match chr {
1101 self.status = CharClassesStatus::Normal;
1102 return Some((FullCodeCharKind::EndComment, item));
1105 self.status = CharClassesStatus::LineComment;
1106 return Some((FullCodeCharKind::InComment, item));
1110 Some((char_kind, item))
1114 /// An iterator over the lines of a string, paired with the char kind at the
1115 /// end of the line.
1116 pub struct LineClasses<'a> {
1117 base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
1118 kind: FullCodeCharKind,
1121 impl<'a> LineClasses<'a> {
1122 pub fn new(s: &'a str) -> Self {
1124 base: CharClasses::new(s.chars()).peekable(),
1125 kind: FullCodeCharKind::Normal,
1130 impl<'a> Iterator for LineClasses<'a> {
1131 type Item = (FullCodeCharKind, String);
1133 fn next(&mut self) -> Option<Self::Item> {
1136 let mut line = String::new();
1138 let start_class = match self.base.peek() {
1139 Some((kind, _)) => *kind,
1140 None => FullCodeCharKind::Normal,
1143 while let Some((kind, c)) = self.base.next() {
1145 self.kind = match (start_class, kind) {
1146 (FullCodeCharKind::Normal, FullCodeCharKind::InString) => {
1147 FullCodeCharKind::StartString
1149 (FullCodeCharKind::InString, FullCodeCharKind::Normal) => {
1150 FullCodeCharKind::EndString
1160 Some((self.kind, line))
1164 /// Iterator over functional and commented parts of a string. Any part of a string is either
1165 /// functional code, either *one* block comment, either *one* line comment. Whitespace between
1166 /// comments is functional code. Line comments contain their ending newlines.
1167 struct UngroupedCommentCodeSlices<'a> {
1169 iter: iter::Peekable<CharClasses<std::str::CharIndices<'a>>>,
1172 impl<'a> UngroupedCommentCodeSlices<'a> {
1173 fn new(code: &'a str) -> UngroupedCommentCodeSlices<'a> {
1174 UngroupedCommentCodeSlices {
1176 iter: CharClasses::new(code.char_indices()).peekable(),
1181 impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1182 type Item = (CodeCharKind, usize, &'a str);
1184 fn next(&mut self) -> Option<Self::Item> {
1185 let (kind, (start_idx, _)) = self.iter.next()?;
1187 FullCodeCharKind::Normal | FullCodeCharKind::InString => {
1188 // Consume all the Normal code
1189 while let Some(&(char_kind, _)) = self.iter.peek() {
1190 if char_kind.is_comment() {
1193 let _ = self.iter.next();
1196 FullCodeCharKind::StartComment => {
1197 // Consume the whole comment
1198 while let Some((FullCodeCharKind::InComment, (_, _))) = self.iter.next() {}
1202 let slice = match self.iter.peek() {
1203 Some(&(_, (end_idx, _))) => &self.slice[start_idx..end_idx],
1204 None => &self.slice[start_idx..],
1207 if kind.is_comment() {
1208 CodeCharKind::Comment
1210 CodeCharKind::Normal
1218 /// Iterator over an alternating sequence of functional and commented parts of
1219 /// a string. The first item is always a, possibly zero length, subslice of
1220 /// functional text. Line style comments contain their ending newlines.
1221 pub struct CommentCodeSlices<'a> {
1223 last_slice_kind: CodeCharKind,
1224 last_slice_end: usize,
1227 impl<'a> CommentCodeSlices<'a> {
1228 pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
1231 last_slice_kind: CodeCharKind::Comment,
1237 impl<'a> Iterator for CommentCodeSlices<'a> {
1238 type Item = (CodeCharKind, usize, &'a str);
1240 fn next(&mut self) -> Option<Self::Item> {
1241 if self.last_slice_end == self.slice.len() {
1245 let mut sub_slice_end = self.last_slice_end;
1246 let mut first_whitespace = None;
1247 let subslice = &self.slice[self.last_slice_end..];
1248 let mut iter = CharClasses::new(subslice.char_indices());
1250 for (kind, (i, c)) in &mut iter {
1251 let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal
1252 && &subslice[..2] == "//"
1253 && [' ', '\t'].contains(&c);
1255 if is_comment_connector && first_whitespace.is_none() {
1256 first_whitespace = Some(i);
1259 if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
1260 let last_index = match first_whitespace {
1264 sub_slice_end = self.last_slice_end + last_index;
1268 if !is_comment_connector {
1269 first_whitespace = None;
1273 if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
1274 // This was the last subslice.
1275 sub_slice_end = match first_whitespace {
1276 Some(i) => self.last_slice_end + i,
1277 None => self.slice.len(),
1281 let kind = match self.last_slice_kind {
1282 CodeCharKind::Comment => CodeCharKind::Normal,
1283 CodeCharKind::Normal => CodeCharKind::Comment,
1287 self.last_slice_end,
1288 &self.slice[self.last_slice_end..sub_slice_end],
1290 self.last_slice_end = sub_slice_end;
1291 self.last_slice_kind = kind;
1297 /// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1298 /// (if it fits in the width/offset, else return None), else return `new`
1299 pub fn recover_comment_removed(
1302 context: &RewriteContext,
1303 ) -> Option<String> {
1304 let snippet = context.snippet(span);
1305 if snippet != new && changed_comment_content(snippet, &new) {
1306 // We missed some comments. Warn and keep the original text.
1307 if context.config.error_on_unformatted() {
1308 context.report.append(
1309 context.source_map.span_to_filename(span).into(),
1310 vec![FormattingError::from_span(
1312 &context.source_map,
1313 ErrorKind::LostComment,
1317 Some(snippet.to_owned())
1323 pub fn filter_normal_code(code: &str) -> String {
1324 let mut buffer = String::with_capacity(code.len());
1325 LineClasses::new(code).for_each(|(kind, line)| match kind {
1326 FullCodeCharKind::Normal
1327 | FullCodeCharKind::StartString
1328 | FullCodeCharKind::InString
1329 | FullCodeCharKind::EndString => {
1330 buffer.push_str(&line);
1335 if !code.ends_with('\n') && buffer.ends_with('\n') {
1341 /// Return true if the two strings of code have the same payload of comments.
1342 /// The payload of comments is everything in the string except:
1343 /// - actual code (not comments)
1344 /// - comment start/end marks
1346 /// - '*' at the beginning of lines in block comments
1347 fn changed_comment_content(orig: &str, new: &str) -> bool {
1348 // Cannot write this as a fn since we cannot return types containing closures
1349 let code_comment_content = |code| {
1350 let slices = UngroupedCommentCodeSlices::new(code);
1352 .filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
1353 .flat_map(|(_, _, s)| CommentReducer::new(s))
1355 let res = code_comment_content(orig).ne(code_comment_content(new));
1357 "comment::changed_comment_content: {}\norig: '{}'\nnew: '{}'\nraw_old: {}\nraw_new: {}",
1361 code_comment_content(orig).collect::<String>(),
1362 code_comment_content(new).collect::<String>()
1367 /// Iterator over the 'payload' characters of a comment.
1368 /// It skips whitespace, comment start/end marks, and '*' at the beginning of lines.
1369 /// The comment must be one comment, ie not more than one start mark (no multiple line comments,
1371 struct CommentReducer<'a> {
1373 at_start_line: bool,
1374 iter: std::str::Chars<'a>,
1377 impl<'a> CommentReducer<'a> {
1378 fn new(comment: &'a str) -> CommentReducer<'a> {
1379 let is_block = comment.starts_with("/*");
1380 let comment = remove_comment_header(comment);
1383 at_start_line: false, // There are no supplementary '*' on the first line
1384 iter: comment.chars(),
1389 impl<'a> Iterator for CommentReducer<'a> {
1392 fn next(&mut self) -> Option<Self::Item> {
1394 let mut c = self.iter.next()?;
1395 if self.is_block && self.at_start_line {
1396 while c.is_whitespace() {
1397 c = self.iter.next()?;
1399 // Ignore leading '*'
1401 c = self.iter.next()?;
1403 } else if c == '\n' {
1404 self.at_start_line = true;
1406 if !c.is_whitespace() {
1413 fn remove_comment_header(comment: &str) -> &str {
1414 if comment.starts_with("///") || comment.starts_with("//!") {
1416 } else if comment.starts_with("//") {
1418 } else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
1419 || comment.starts_with("/*!")
1421 &comment[3..comment.len() - 2]
1424 comment.starts_with("/*"),
1425 format!("string '{}' is not a comment", comment)
1427 &comment[2..comment.len() - 2]
1434 use shape::{Indent, Shape};
1438 let mut iter = CharClasses::new("//\n\n".chars());
1440 assert_eq!((FullCodeCharKind::StartComment, '/'), iter.next().unwrap());
1441 assert_eq!((FullCodeCharKind::InComment, '/'), iter.next().unwrap());
1442 assert_eq!((FullCodeCharKind::EndComment, '\n'), iter.next().unwrap());
1443 assert_eq!((FullCodeCharKind::Normal, '\n'), iter.next().unwrap());
1444 assert_eq!(None, iter.next());
1448 fn comment_code_slices() {
1449 let input = "code(); /* test */ 1 + 1";
1450 let mut iter = CommentCodeSlices::new(input);
1452 assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
1454 (CodeCharKind::Comment, 8, "/* test */"),
1455 iter.next().unwrap()
1457 assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
1458 assert_eq!(None, iter.next());
1462 fn comment_code_slices_two() {
1463 let input = "// comment\n test();";
1464 let mut iter = CommentCodeSlices::new(input);
1466 assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
1468 (CodeCharKind::Comment, 0, "// comment\n"),
1469 iter.next().unwrap()
1472 (CodeCharKind::Normal, 11, " test();"),
1473 iter.next().unwrap()
1475 assert_eq!(None, iter.next());
1479 fn comment_code_slices_three() {
1480 let input = "1 // comment\n // comment2\n\n";
1481 let mut iter = CommentCodeSlices::new(input);
1483 assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
1485 (CodeCharKind::Comment, 2, "// comment\n // comment2\n"),
1486 iter.next().unwrap()
1488 assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
1489 assert_eq!(None, iter.next());
1494 fn format_comments() {
1495 let mut wrap_normalize_config: ::config::Config = Default::default();
1496 wrap_normalize_config.set().wrap_comments(true);
1497 wrap_normalize_config.set().normalize_comments(true);
1499 let mut wrap_config: ::config::Config = Default::default();
1500 wrap_config.set().wrap_comments(true);
1502 let comment = rewrite_comment(" //test",
1504 Shape::legacy(100, Indent::new(0, 100)),
1505 &wrap_normalize_config).unwrap();
1506 assert_eq!("/* test */", comment);
1508 let comment = rewrite_comment("// comment on a",
1510 Shape::legacy(10, Indent::empty()),
1511 &wrap_normalize_config).unwrap();
1512 assert_eq!("// comment\n// on a", comment);
1514 let comment = rewrite_comment("// A multi line comment\n // between args.",
1516 Shape::legacy(60, Indent::new(0, 12)),
1517 &wrap_normalize_config).unwrap();
1518 assert_eq!("// A multi line comment\n // between args.", comment);
1520 let input = "// comment";
1523 let comment = rewrite_comment(input,
1525 Shape::legacy(9, Indent::new(0, 69)),
1526 &wrap_normalize_config).unwrap();
1527 assert_eq!(expected, comment);
1529 let comment = rewrite_comment("/* trimmed */",
1531 Shape::legacy(100, Indent::new(0, 100)),
1532 &wrap_normalize_config).unwrap();
1533 assert_eq!("/* trimmed */", comment);
1535 // check that different comment style are properly recognised
1536 let comment = rewrite_comment(r#"/// test1
1542 Shape::legacy(100, Indent::new(0, 0)),
1543 &wrap_normalize_config).unwrap();
1544 assert_eq!("/// test1\n/// test2\n// test3", comment);
1546 // check that the blank line marks the end of a commented paragraph
1547 let comment = rewrite_comment(r#"// test1
1551 Shape::legacy(100, Indent::new(0, 0)),
1552 &wrap_normalize_config).unwrap();
1553 assert_eq!("// test1\n\n// test2", comment);
1555 // check that the blank line marks the end of a custom-commented paragraph
1556 let comment = rewrite_comment(r#"//@ test1
1560 Shape::legacy(100, Indent::new(0, 0)),
1561 &wrap_normalize_config).unwrap();
1562 assert_eq!("//@ test1\n\n//@ test2", comment);
1564 // check that bare lines are just indented but left unchanged otherwise
1565 let comment = rewrite_comment(r#"// test1
1572 Shape::legacy(100, Indent::new(0, 0)),
1573 &wrap_config).unwrap();
1574 assert_eq!("// test1\n/*\n a bare line!\n\n another bare line!\n*/", comment);
1577 // This is probably intended to be a non-test fn, but it is not used. I'm
1578 // keeping it around unless it helps us test stuff.
1579 fn uncommented(text: &str) -> String {
1580 CharClasses::new(text.chars())
1581 .filter_map(|(s, c)| match s {
1582 FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
1589 fn test_uncommented() {
1590 assert_eq!(&uncommented("abc/*...*/"), "abc");
1592 &uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
1595 assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
1599 fn test_contains_comment() {
1600 assert_eq!(contains_comment("abc"), false);
1601 assert_eq!(contains_comment("abc // qsdf"), true);
1602 assert_eq!(contains_comment("abc /* kqsdf"), true);
1603 assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
1607 fn test_find_uncommented() {
1608 fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1609 assert_eq!(expected, haystack.find_uncommented(needle));
1612 check("/*/ */test", "test", Some(6));
1613 check("//test\ntest", "test", Some(7));
1614 check("/* comment only */", "whatever", None);
1616 "/* comment */ some text /* more commentary */ result",
1620 check("sup // sup", "p", Some(2));
1621 check("sup", "x", None);
1622 check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
1623 check("/*sup yo? \n sup*/ sup", "p", Some(20));
1624 check("hel/*lohello*/lo", "hello", None);
1625 check("acb", "ab", None);
1626 check(",/*A*/ ", ",", Some(0));
1627 check("abc", "abc", Some(0));
1628 check("/* abc */", "abc", None);
1629 check("/**/abc/* */", "abc", Some(4));
1630 check("\"/* abc */\"", "abc", Some(4));
1631 check("\"/* abc", "abc", Some(4));
1635 fn test_remove_trailing_white_spaces() {
1636 let s = " r#\"\n test\n \"#";
1637 assert_eq!(remove_trailing_white_spaces(&s), s);
1641 fn test_filter_normal_code() {
1644 println!("hello, world");
1647 assert_eq!(s, filter_normal_code(s));
1648 let s_with_comment = r#"
1651 println!("hello, world");
1654 assert_eq!(s, filter_normal_code(s_with_comment));