]> git.lizzy.rs Git - rust.git/blob - src/comment.rs
Merge pull request #3035 from topecongiro/issue-3006
[rust.git] / src / comment.rs
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.
4 //
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.
10
11 // Formatting and tools for comments.
12
13 use std::{self, borrow::Cow, iter};
14
15 use itertools::{multipeek, MultiPeek};
16 use syntax::source_map::Span;
17
18 use config::Config;
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};
24
25 fn is_custom_comment(comment: &str) -> bool {
26     if !comment.starts_with("//") {
27         false
28     } else if let Some(c) = comment.chars().nth(2) {
29         !c.is_alphanumeric() && !c.is_whitespace()
30     } else {
31         false
32     }
33 }
34
35 #[derive(Copy, Clone, PartialEq, Eq)]
36 pub enum CommentStyle<'a> {
37     DoubleSlash,
38     TripleSlash,
39     Doc,
40     SingleBullet,
41     DoubleBullet,
42     Exclamation,
43     Custom(&'a str),
44 }
45
46 fn custom_opener(s: &str) -> &str {
47     s.lines().next().map_or("", |first_line| {
48         first_line
49             .find(' ')
50             .map_or(first_line, |space_index| &first_line[0..=space_index])
51     })
52 }
53
54 impl<'a> CommentStyle<'a> {
55     pub fn is_doc_comment(&self) -> bool {
56         match *self {
57             CommentStyle::TripleSlash | CommentStyle::Doc => true,
58             _ => false,
59         }
60     }
61
62     pub fn opener(&self) -> &'a str {
63         match *self {
64             CommentStyle::DoubleSlash => "// ",
65             CommentStyle::TripleSlash => "/// ",
66             CommentStyle::Doc => "//! ",
67             CommentStyle::SingleBullet => "/* ",
68             CommentStyle::DoubleBullet => "/** ",
69             CommentStyle::Exclamation => "/*! ",
70             CommentStyle::Custom(opener) => opener,
71         }
72     }
73
74     pub fn closer(&self) -> &'a str {
75         match *self {
76             CommentStyle::DoubleSlash
77             | CommentStyle::TripleSlash
78             | CommentStyle::Custom(..)
79             | CommentStyle::Doc => "",
80             CommentStyle::DoubleBullet => " **/",
81             CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
82         }
83     }
84
85     pub fn line_start(&self) -> &'a str {
86         match *self {
87             CommentStyle::DoubleSlash => "// ",
88             CommentStyle::TripleSlash => "/// ",
89             CommentStyle::Doc => "//! ",
90             CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
91             CommentStyle::DoubleBullet => " ** ",
92             CommentStyle::Custom(opener) => opener,
93         }
94     }
95
96     pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
97         (self.opener(), self.closer(), self.line_start())
98     }
99 }
100
101 fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
102     if !normalize_comments {
103         if orig.starts_with("/**") && !orig.starts_with("/**/") {
104             CommentStyle::DoubleBullet
105         } else if orig.starts_with("/*!") {
106             CommentStyle::Exclamation
107         } else if orig.starts_with("/*") {
108             CommentStyle::SingleBullet
109         } else if orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/') {
110             CommentStyle::TripleSlash
111         } else if orig.starts_with("//!") {
112             CommentStyle::Doc
113         } else if is_custom_comment(orig) {
114             CommentStyle::Custom(custom_opener(orig))
115         } else {
116             CommentStyle::DoubleSlash
117         }
118     } else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/'))
119         || (orig.starts_with("/**") && !orig.starts_with("/**/"))
120     {
121         CommentStyle::TripleSlash
122     } else if orig.starts_with("//!") || orig.starts_with("/*!") {
123         CommentStyle::Doc
124     } else if is_custom_comment(orig) {
125         CommentStyle::Custom(custom_opener(orig))
126     } else {
127         CommentStyle::DoubleSlash
128     }
129 }
130
131 /// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
132 /// comments between two strings. If there are such comments, then that will be
133 /// recovered. If `allow_extend` is true and there is no comment between the two
134 /// strings, then they will be put on a single line as long as doing so does not
135 /// exceed max width.
136 pub fn combine_strs_with_missing_comments(
137     context: &RewriteContext,
138     prev_str: &str,
139     next_str: &str,
140     span: Span,
141     shape: Shape,
142     allow_extend: bool,
143 ) -> Option<String> {
144     let mut result =
145         String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
146     result.push_str(prev_str);
147     let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
148     let first_sep = if prev_str.is_empty() || next_str.is_empty() {
149         ""
150     } else {
151         " "
152     };
153     let mut one_line_width =
154         last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
155
156     let config = context.config;
157     let indent = shape.indent;
158     let missing_comment = rewrite_missing_comment(span, shape, context)?;
159
160     if missing_comment.is_empty() {
161         if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
162             result.push_str(first_sep);
163         } else if !prev_str.is_empty() {
164             result.push_str(&indent.to_string_with_newline(config))
165         }
166         result.push_str(next_str);
167         return Some(result);
168     }
169
170     // We have a missing comment between the first expression and the second expression.
171
172     // Peek the the original source code and find out whether there is a newline between the first
173     // expression and the second expression or the missing comment. We will preserve the original
174     // layout whenever possible.
175     let original_snippet = context.snippet(span);
176     let prefer_same_line = if let Some(pos) = original_snippet.find('/') {
177         !original_snippet[..pos].contains('\n')
178     } else {
179         !original_snippet.contains('\n')
180     };
181
182     one_line_width -= first_sep.len();
183     let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
184         Cow::from("")
185     } else {
186         let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
187         if prefer_same_line && one_line_width <= shape.width {
188             Cow::from(" ")
189         } else {
190             indent.to_string_with_newline(config)
191         }
192     };
193     result.push_str(&first_sep);
194     result.push_str(&missing_comment);
195
196     let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
197         Cow::from("")
198     } else if missing_comment.starts_with("//") {
199         indent.to_string_with_newline(config)
200     } else {
201         one_line_width += missing_comment.len() + first_sep.len() + 1;
202         allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
203         if prefer_same_line && allow_one_line && one_line_width <= shape.width {
204             Cow::from(" ")
205         } else {
206             indent.to_string_with_newline(config)
207         }
208     };
209     result.push_str(&second_sep);
210     result.push_str(next_str);
211
212     Some(result)
213 }
214
215 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
216     _rewrite_comment(orig, false, shape, config, true)
217 }
218
219 pub fn rewrite_comment(
220     orig: &str,
221     block_style: bool,
222     shape: Shape,
223     config: &Config,
224 ) -> Option<String> {
225     _rewrite_comment(orig, block_style, shape, config, false)
226 }
227
228 fn _rewrite_comment(
229     orig: &str,
230     block_style: bool,
231     shape: Shape,
232     config: &Config,
233     is_doc_comment: bool,
234 ) -> Option<String> {
235     // If there are lines without a starting sigil, we won't format them correctly
236     // so in that case we won't even re-align (if !config.normalize_comments()) and
237     // we should stop now.
238     let num_bare_lines = orig
239         .lines()
240         .map(|line| line.trim())
241         .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
242         .count();
243     if num_bare_lines > 0 && !config.normalize_comments() {
244         return Some(orig.to_owned());
245     }
246     if !config.normalize_comments() && !config.wrap_comments() {
247         return light_rewrite_comment(orig, shape.indent, config, is_doc_comment);
248     }
249
250     identify_comment(orig, block_style, shape, config, is_doc_comment)
251 }
252
253 fn identify_comment(
254     orig: &str,
255     block_style: bool,
256     shape: Shape,
257     config: &Config,
258     is_doc_comment: bool,
259 ) -> Option<String> {
260     let style = comment_style(orig, false);
261     let mut first_group_ending = 0;
262
263     fn compute_len(orig: &str, line: &str) -> usize {
264         if orig.len() > line.len() {
265             if orig.as_bytes()[line.len()] == b'\r' {
266                 line.len() + 2
267             } else {
268                 line.len() + 1
269             }
270         } else {
271             line.len()
272         }
273     }
274
275     match style {
276         CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
277             let line_start = style.line_start().trim_left();
278             for line in orig.lines() {
279                 if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
280                     first_group_ending += compute_len(&orig[first_group_ending..], line);
281                 } else {
282                     break;
283                 }
284             }
285         }
286         CommentStyle::Custom(opener) => {
287             let trimmed_opener = opener.trim_right();
288             for line in orig.lines() {
289                 if line.trim_left().starts_with(trimmed_opener) {
290                     first_group_ending += compute_len(&orig[first_group_ending..], line);
291                 } else {
292                     break;
293                 }
294             }
295         }
296         // for a block comment, search for the closing symbol
297         CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
298             let closer = style.closer().trim_left();
299             for line in orig.lines() {
300                 first_group_ending += compute_len(&orig[first_group_ending..], line);
301                 if line.trim_left().ends_with(closer) {
302                     break;
303                 }
304             }
305         }
306     }
307
308     let (first_group, rest) = orig.split_at(first_group_ending);
309     let first_group_str = rewrite_comment_inner(
310         first_group,
311         block_style,
312         style,
313         shape,
314         config,
315         is_doc_comment || style.is_doc_comment(),
316     )?;
317     if rest.is_empty() {
318         Some(first_group_str)
319     } else {
320         identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
321             format!(
322                 "{}\n{}{}",
323                 first_group_str,
324                 shape.indent.to_string(config),
325                 rest_str
326             )
327         })
328     }
329 }
330
331 fn rewrite_comment_inner(
332     orig: &str,
333     block_style: bool,
334     style: CommentStyle,
335     shape: Shape,
336     config: &Config,
337     is_doc_comment: bool,
338 ) -> Option<String> {
339     let (opener, closer, line_start) = if block_style {
340         CommentStyle::SingleBullet.to_str_tuplet()
341     } else {
342         comment_style(orig, config.normalize_comments()).to_str_tuplet()
343     };
344
345     let max_chars = shape
346         .width
347         .checked_sub(closer.len() + opener.len())
348         .unwrap_or(1);
349     let indent_str = shape.indent.to_string_with_newline(config);
350     let fmt_indent = shape.indent + (opener.len() - line_start.len());
351     let mut fmt = StringFormat {
352         opener: "",
353         closer: "",
354         line_start,
355         line_end: "",
356         shape: Shape::legacy(max_chars, fmt_indent),
357         trim_end: true,
358         config,
359     };
360
361     let line_breaks = count_newlines(orig.trim_right());
362     let lines = orig
363         .lines()
364         .enumerate()
365         .map(|(i, mut line)| {
366             line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
367             // Drop old closer.
368             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
369                 line = line[..(line.len() - 2)].trim_right();
370             }
371
372             line
373         }).map(|s| left_trim_comment_line(s, &style))
374         .map(|(line, has_leading_whitespace)| {
375             if orig.starts_with("/*") && line_breaks == 0 {
376                 (
377                     line.trim_left(),
378                     has_leading_whitespace || config.normalize_comments(),
379                 )
380             } else {
381                 (line, has_leading_whitespace || config.normalize_comments())
382             }
383         });
384
385     let mut result = String::with_capacity(orig.len() * 2);
386     result.push_str(opener);
387     let mut code_block_buffer = String::with_capacity(128);
388     let mut is_prev_line_multi_line = false;
389     let mut inside_code_block = false;
390     let comment_line_separator = format!("{}{}", indent_str, line_start);
391     let join_code_block_with_comment_line_separator = |s: &str| {
392         let mut result = String::with_capacity(s.len() + 128);
393         let mut iter = s.lines().peekable();
394         while let Some(line) = iter.next() {
395             result.push_str(line);
396             result.push_str(match iter.peek() {
397                 Some(next_line) if next_line.is_empty() => comment_line_separator.trim_right(),
398                 Some(..) => &comment_line_separator,
399                 None => "",
400             });
401         }
402         result
403     };
404
405     for (i, (line, has_leading_whitespace)) in lines.enumerate() {
406         let is_last = i == count_newlines(orig);
407
408         if inside_code_block {
409             if line.starts_with("```") {
410                 inside_code_block = false;
411                 result.push_str(&comment_line_separator);
412                 let code_block = {
413                     let mut config = config.clone();
414                     config.set().wrap_comments(false);
415                     match ::format_code_block(&code_block_buffer, &config) {
416                         Some(ref s) => trim_custom_comment_prefix(s),
417                         None => trim_custom_comment_prefix(&code_block_buffer),
418                     }
419                 };
420                 result.push_str(&join_code_block_with_comment_line_separator(&code_block));
421                 code_block_buffer.clear();
422                 result.push_str(&comment_line_separator);
423                 result.push_str(line);
424             } else {
425                 code_block_buffer.push_str(&hide_sharp_behind_comment(line));
426                 code_block_buffer.push('\n');
427
428                 if is_last {
429                     // There is an code block that is not properly enclosed by backticks.
430                     // We will leave them untouched.
431                     result.push_str(&comment_line_separator);
432                     result.push_str(&join_code_block_with_comment_line_separator(
433                         &trim_custom_comment_prefix(&code_block_buffer),
434                     ));
435                 }
436             }
437
438             continue;
439         } else {
440             inside_code_block = line.starts_with("```");
441
442             if result == opener {
443                 let force_leading_whitespace = opener == "/* " && count_newlines(orig) == 0;
444                 if !has_leading_whitespace && !force_leading_whitespace && result.ends_with(' ') {
445                     result.pop();
446                 }
447                 if line.is_empty() {
448                     continue;
449                 }
450             } else if is_prev_line_multi_line && !line.is_empty() {
451                 result.push(' ')
452             } else if is_last && line.is_empty() {
453                 // trailing blank lines are unwanted
454                 if !closer.is_empty() {
455                     result.push_str(&indent_str);
456                 }
457                 break;
458             } else {
459                 result.push_str(&comment_line_separator);
460                 if !has_leading_whitespace && result.ends_with(' ') {
461                     result.pop();
462                 }
463             }
464         }
465
466         if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
467             match rewrite_string(line, &fmt) {
468                 Some(ref s) => {
469                     is_prev_line_multi_line = s.contains('\n');
470                     result.push_str(s);
471                 }
472                 None if is_prev_line_multi_line => {
473                     // We failed to put the current `line` next to the previous `line`.
474                     // Remove the trailing space, then start rewrite on the next line.
475                     result.pop();
476                     result.push_str(&comment_line_separator);
477                     fmt.shape = Shape::legacy(max_chars, fmt_indent);
478                     match rewrite_string(line, &fmt) {
479                         Some(ref s) => {
480                             is_prev_line_multi_line = s.contains('\n');
481                             result.push_str(s);
482                         }
483                         None => {
484                             is_prev_line_multi_line = false;
485                             result.push_str(line);
486                         }
487                     }
488                 }
489                 None => {
490                     is_prev_line_multi_line = false;
491                     result.push_str(line);
492                 }
493             }
494
495             fmt.shape = if is_prev_line_multi_line {
496                 // 1 = " "
497                 let offset = 1 + last_line_width(&result) - line_start.len();
498                 Shape {
499                     width: max_chars.saturating_sub(offset),
500                     indent: fmt_indent,
501                     offset: fmt.shape.offset + offset,
502                 }
503             } else {
504                 Shape::legacy(max_chars, fmt_indent)
505             };
506         } else {
507             if line.is_empty() && result.ends_with(' ') && !is_last {
508                 // Remove space if this is an empty comment or a doc comment.
509                 result.pop();
510             }
511             result.push_str(line);
512             fmt.shape = Shape::legacy(max_chars, fmt_indent);
513             is_prev_line_multi_line = false;
514         }
515     }
516
517     result.push_str(closer);
518     if result.ends_with(opener) && opener.ends_with(' ') {
519         // Trailing space.
520         result.pop();
521     }
522
523     Some(result)
524 }
525
526 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
527
528 fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
529     if s.trim_left().starts_with("# ") {
530         Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
531     } else {
532         Cow::from(s)
533     }
534 }
535
536 fn trim_custom_comment_prefix(s: &str) -> String {
537     s.lines()
538         .map(|line| {
539             let left_trimmed = line.trim_left();
540             if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
541                 left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
542             } else {
543                 line
544             }
545         }).collect::<Vec<_>>()
546         .join("\n")
547 }
548
549 /// Returns true if the given string MAY include URLs or alike.
550 fn has_url(s: &str) -> bool {
551     // This function may return false positive, but should get its job done in most cases.
552     s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
553 }
554
555 /// Given the span, rewrite the missing comment inside it if available.
556 /// Note that the given span must only include comments (or leading/trailing whitespaces).
557 pub fn rewrite_missing_comment(
558     span: Span,
559     shape: Shape,
560     context: &RewriteContext,
561 ) -> Option<String> {
562     let missing_snippet = context.snippet(span);
563     let trimmed_snippet = missing_snippet.trim();
564     if !trimmed_snippet.is_empty() {
565         rewrite_comment(trimmed_snippet, false, shape, context.config)
566     } else {
567         Some(String::new())
568     }
569 }
570
571 /// Recover the missing comments in the specified span, if available.
572 /// The layout of the comments will be preserved as long as it does not break the code
573 /// and its total width does not exceed the max width.
574 pub fn recover_missing_comment_in_span(
575     span: Span,
576     shape: Shape,
577     context: &RewriteContext,
578     used_width: usize,
579 ) -> Option<String> {
580     let missing_comment = rewrite_missing_comment(span, shape, context)?;
581     if missing_comment.is_empty() {
582         Some(String::new())
583     } else {
584         let missing_snippet = context.snippet(span);
585         let pos = missing_snippet.find('/').unwrap_or(0);
586         // 1 = ` `
587         let total_width = missing_comment.len() + used_width + 1;
588         let force_new_line_before_comment =
589             missing_snippet[..pos].contains('\n') || total_width > context.config.max_width();
590         let sep = if force_new_line_before_comment {
591             shape.indent.to_string_with_newline(context.config)
592         } else {
593             Cow::from(" ")
594         };
595         Some(format!("{}{}", sep, missing_comment))
596     }
597 }
598
599 /// Trim trailing whitespaces unless they consist of two or more whitespaces.
600 fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
601     if is_doc_comment && s.ends_with("  ") {
602         s
603     } else {
604         s.trim_right()
605     }
606 }
607
608 /// Trims whitespace and aligns to indent, but otherwise does not change comments.
609 fn light_rewrite_comment(
610     orig: &str,
611     offset: Indent,
612     config: &Config,
613     is_doc_comment: bool,
614 ) -> Option<String> {
615     let lines: Vec<&str> = orig
616         .lines()
617         .map(|l| {
618             // This is basically just l.trim(), but in the case that a line starts
619             // with `*` we want to leave one space before it, so it aligns with the
620             // `*` in `/*`.
621             let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
622             let left_trimmed = if let Some(fnw) = first_non_whitespace {
623                 if l.as_bytes()[fnw] == b'*' && fnw > 0 {
624                     &l[fnw - 1..]
625                 } else {
626                     &l[fnw..]
627                 }
628             } else {
629                 ""
630             };
631             // Preserve markdown's double-space line break syntax in doc comment.
632             trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
633         }).collect();
634     Some(lines.join(&format!("\n{}", offset.to_string(config))))
635 }
636
637 /// Trims comment characters and possibly a single space from the left of a string.
638 /// Does not trim all whitespace. If a single space is trimmed from the left of the string,
639 /// this function returns true.
640 fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str, bool) {
641     if line.starts_with("//! ")
642         || line.starts_with("/// ")
643         || line.starts_with("/*! ")
644         || line.starts_with("/** ")
645     {
646         (&line[4..], true)
647     } else if let CommentStyle::Custom(opener) = *style {
648         if line.starts_with(opener) {
649             (&line[opener.len()..], true)
650         } else {
651             (&line[opener.trim_right().len()..], false)
652         }
653     } else if line.starts_with("/* ")
654         || line.starts_with("// ")
655         || line.starts_with("//!")
656         || line.starts_with("///")
657         || line.starts_with("** ")
658         || line.starts_with("/*!")
659         || (line.starts_with("/**") && !line.starts_with("/**/"))
660     {
661         (&line[3..], line.chars().nth(2).unwrap() == ' ')
662     } else if line.starts_with("/*")
663         || line.starts_with("* ")
664         || line.starts_with("//")
665         || line.starts_with("**")
666     {
667         (&line[2..], line.chars().nth(1).unwrap() == ' ')
668     } else if line.starts_with('*') {
669         (&line[1..], false)
670     } else {
671         (line, line.starts_with(' '))
672     }
673 }
674
675 pub trait FindUncommented {
676     fn find_uncommented(&self, pat: &str) -> Option<usize>;
677 }
678
679 impl FindUncommented for str {
680     fn find_uncommented(&self, pat: &str) -> Option<usize> {
681         let mut needle_iter = pat.chars();
682         for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
683             match needle_iter.next() {
684                 None => {
685                     return Some(i - pat.len());
686                 }
687                 Some(c) => match kind {
688                     FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
689                     _ => {
690                         needle_iter = pat.chars();
691                     }
692                 },
693             }
694         }
695
696         // Handle case where the pattern is a suffix of the search string
697         match needle_iter.next() {
698             Some(_) => None,
699             None => Some(self.len() - pat.len()),
700         }
701     }
702 }
703
704 // Returns the first byte position after the first comment. The given string
705 // is expected to be prefixed by a comment, including delimiters.
706 // Good: "/* /* inner */ outer */ code();"
707 // Bad:  "code(); // hello\n world!"
708 pub fn find_comment_end(s: &str) -> Option<usize> {
709     let mut iter = CharClasses::new(s.char_indices());
710     for (kind, (i, _c)) in &mut iter {
711         if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
712             return Some(i);
713         }
714     }
715
716     // Handle case where the comment ends at the end of s.
717     if iter.status == CharClassesStatus::Normal {
718         Some(s.len())
719     } else {
720         None
721     }
722 }
723
724 /// Returns true if text contains any comment.
725 pub fn contains_comment(text: &str) -> bool {
726     CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
727 }
728
729 /// Remove trailing spaces from the specified snippet. We do not remove spaces
730 /// inside strings or comments.
731 pub fn remove_trailing_white_spaces(text: &str) -> String {
732     let mut buffer = String::with_capacity(text.len());
733     let mut space_buffer = String::with_capacity(128);
734     for (char_kind, c) in CharClasses::new(text.chars()) {
735         match c {
736             '\n' => {
737                 if char_kind == FullCodeCharKind::InString {
738                     buffer.push_str(&space_buffer);
739                 }
740                 space_buffer.clear();
741                 buffer.push('\n');
742             }
743             _ if c.is_whitespace() => {
744                 space_buffer.push(c);
745             }
746             _ => {
747                 if !space_buffer.is_empty() {
748                     buffer.push_str(&space_buffer);
749                     space_buffer.clear();
750                 }
751                 buffer.push(c);
752             }
753         }
754     }
755     buffer
756 }
757
758 pub struct CharClasses<T>
759 where
760     T: Iterator,
761     T::Item: RichChar,
762 {
763     base: MultiPeek<T>,
764     status: CharClassesStatus,
765 }
766
767 pub trait RichChar {
768     fn get_char(&self) -> char;
769 }
770
771 impl RichChar for char {
772     fn get_char(&self) -> char {
773         *self
774     }
775 }
776
777 impl RichChar for (usize, char) {
778     fn get_char(&self) -> char {
779         self.1
780     }
781 }
782
783 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
784 enum CharClassesStatus {
785     Normal,
786     LitString,
787     LitStringEscape,
788     LitRawString(u32),
789     RawStringPrefix(u32),
790     RawStringSuffix(u32),
791     LitChar,
792     LitCharEscape,
793     // The u32 is the nesting deepness of the comment
794     BlockComment(u32),
795     // Status when the '/' has been consumed, but not yet the '*', deepness is
796     // the new deepness (after the comment opening).
797     BlockCommentOpening(u32),
798     // Status when the '*' has been consumed, but not yet the '/', deepness is
799     // the new deepness (after the comment closing).
800     BlockCommentClosing(u32),
801     LineComment,
802 }
803
804 /// Distinguish between functional part of code and comments
805 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
806 pub enum CodeCharKind {
807     Normal,
808     Comment,
809 }
810
811 /// Distinguish between functional part of code and comments,
812 /// describing opening and closing of comments for ease when chunking
813 /// code from tagged characters
814 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
815 pub enum FullCodeCharKind {
816     Normal,
817     /// The first character of a comment, there is only one for a comment (always '/')
818     StartComment,
819     /// Any character inside a comment including the second character of comment
820     /// marks ("//", "/*")
821     InComment,
822     /// Last character of a comment, '\n' for a line comment, '/' for a block comment.
823     EndComment,
824     /// Start of a mutlitine string
825     StartString,
826     /// End of a mutlitine string
827     EndString,
828     /// Inside a string.
829     InString,
830 }
831
832 impl FullCodeCharKind {
833     pub fn is_comment(self) -> bool {
834         match self {
835             FullCodeCharKind::StartComment
836             | FullCodeCharKind::InComment
837             | FullCodeCharKind::EndComment => true,
838             _ => false,
839         }
840     }
841
842     pub fn is_string(self) -> bool {
843         self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
844     }
845
846     fn to_codecharkind(self) -> CodeCharKind {
847         if self.is_comment() {
848             CodeCharKind::Comment
849         } else {
850             CodeCharKind::Normal
851         }
852     }
853 }
854
855 impl<T> CharClasses<T>
856 where
857     T: Iterator,
858     T::Item: RichChar,
859 {
860     pub fn new(base: T) -> CharClasses<T> {
861         CharClasses {
862             base: multipeek(base),
863             status: CharClassesStatus::Normal,
864         }
865     }
866 }
867
868 fn is_raw_string_suffix<T>(iter: &mut MultiPeek<T>, count: u32) -> bool
869 where
870     T: Iterator,
871     T::Item: RichChar,
872 {
873     for _ in 0..count {
874         match iter.peek() {
875             Some(c) if c.get_char() == '#' => continue,
876             _ => return false,
877         }
878     }
879     true
880 }
881
882 impl<T> Iterator for CharClasses<T>
883 where
884     T: Iterator,
885     T::Item: RichChar,
886 {
887     type Item = (FullCodeCharKind, T::Item);
888
889     fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
890         let item = self.base.next()?;
891         let chr = item.get_char();
892         let mut char_kind = FullCodeCharKind::Normal;
893         self.status = match self.status {
894             CharClassesStatus::LitRawString(sharps) => {
895                 char_kind = FullCodeCharKind::InString;
896                 match chr {
897                     '"' => {
898                         if sharps == 0 {
899                             char_kind = FullCodeCharKind::Normal;
900                             CharClassesStatus::Normal
901                         } else if is_raw_string_suffix(&mut self.base, sharps) {
902                             CharClassesStatus::RawStringSuffix(sharps)
903                         } else {
904                             CharClassesStatus::LitRawString(sharps)
905                         }
906                     }
907                     _ => CharClassesStatus::LitRawString(sharps),
908                 }
909             }
910             CharClassesStatus::RawStringPrefix(sharps) => {
911                 char_kind = FullCodeCharKind::InString;
912                 match chr {
913                     '#' => CharClassesStatus::RawStringPrefix(sharps + 1),
914                     '"' => CharClassesStatus::LitRawString(sharps),
915                     _ => CharClassesStatus::Normal, // Unreachable.
916                 }
917             }
918             CharClassesStatus::RawStringSuffix(sharps) => {
919                 match chr {
920                     '#' => {
921                         if sharps == 1 {
922                             CharClassesStatus::Normal
923                         } else {
924                             char_kind = FullCodeCharKind::InString;
925                             CharClassesStatus::RawStringSuffix(sharps - 1)
926                         }
927                     }
928                     _ => CharClassesStatus::Normal, // Unreachable
929                 }
930             }
931             CharClassesStatus::LitString => {
932                 char_kind = FullCodeCharKind::InString;
933                 match chr {
934                     '"' => CharClassesStatus::Normal,
935                     '\\' => CharClassesStatus::LitStringEscape,
936                     _ => CharClassesStatus::LitString,
937                 }
938             }
939             CharClassesStatus::LitStringEscape => {
940                 char_kind = FullCodeCharKind::InString;
941                 CharClassesStatus::LitString
942             }
943             CharClassesStatus::LitChar => match chr {
944                 '\\' => CharClassesStatus::LitCharEscape,
945                 '\'' => CharClassesStatus::Normal,
946                 _ => CharClassesStatus::LitChar,
947             },
948             CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
949             CharClassesStatus::Normal => match chr {
950                 'r' => match self.base.peek().map(|c| c.get_char()) {
951                     Some('#') | Some('"') => {
952                         char_kind = FullCodeCharKind::InString;
953                         CharClassesStatus::RawStringPrefix(0)
954                     }
955                     _ => CharClassesStatus::Normal,
956                 },
957                 '"' => {
958                     char_kind = FullCodeCharKind::InString;
959                     CharClassesStatus::LitString
960                 }
961                 '\'' => {
962                     // HACK: Work around mut borrow.
963                     match self.base.peek() {
964                         Some(next) if next.get_char() == '\\' => {
965                             self.status = CharClassesStatus::LitChar;
966                             return Some((char_kind, item));
967                         }
968                         _ => (),
969                     }
970
971                     match self.base.peek() {
972                         Some(next) if next.get_char() == '\'' => CharClassesStatus::LitChar,
973                         _ => CharClassesStatus::Normal,
974                     }
975                 }
976                 '/' => match self.base.peek() {
977                     Some(next) if next.get_char() == '*' => {
978                         self.status = CharClassesStatus::BlockCommentOpening(1);
979                         return Some((FullCodeCharKind::StartComment, item));
980                     }
981                     Some(next) if next.get_char() == '/' => {
982                         self.status = CharClassesStatus::LineComment;
983                         return Some((FullCodeCharKind::StartComment, item));
984                     }
985                     _ => CharClassesStatus::Normal,
986                 },
987                 _ => CharClassesStatus::Normal,
988             },
989             CharClassesStatus::BlockComment(deepness) => {
990                 assert_ne!(deepness, 0);
991                 self.status = match self.base.peek() {
992                     Some(next) if next.get_char() == '/' && chr == '*' => {
993                         CharClassesStatus::BlockCommentClosing(deepness - 1)
994                     }
995                     Some(next) if next.get_char() == '*' && chr == '/' => {
996                         CharClassesStatus::BlockCommentOpening(deepness + 1)
997                     }
998                     _ => CharClassesStatus::BlockComment(deepness),
999                 };
1000                 return Some((FullCodeCharKind::InComment, item));
1001             }
1002             CharClassesStatus::BlockCommentOpening(deepness) => {
1003                 assert_eq!(chr, '*');
1004                 self.status = CharClassesStatus::BlockComment(deepness);
1005                 return Some((FullCodeCharKind::InComment, item));
1006             }
1007             CharClassesStatus::BlockCommentClosing(deepness) => {
1008                 assert_eq!(chr, '/');
1009                 if deepness == 0 {
1010                     self.status = CharClassesStatus::Normal;
1011                     return Some((FullCodeCharKind::EndComment, item));
1012                 } else {
1013                     self.status = CharClassesStatus::BlockComment(deepness);
1014                     return Some((FullCodeCharKind::InComment, item));
1015                 }
1016             }
1017             CharClassesStatus::LineComment => match chr {
1018                 '\n' => {
1019                     self.status = CharClassesStatus::Normal;
1020                     return Some((FullCodeCharKind::EndComment, item));
1021                 }
1022                 _ => {
1023                     self.status = CharClassesStatus::LineComment;
1024                     return Some((FullCodeCharKind::InComment, item));
1025                 }
1026             },
1027         };
1028         Some((char_kind, item))
1029     }
1030 }
1031
1032 /// An iterator over the lines of a string, paired with the char kind at the
1033 /// end of the line.
1034 pub struct LineClasses<'a> {
1035     base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
1036     kind: FullCodeCharKind,
1037 }
1038
1039 impl<'a> LineClasses<'a> {
1040     pub fn new(s: &'a str) -> Self {
1041         LineClasses {
1042             base: CharClasses::new(s.chars()).peekable(),
1043             kind: FullCodeCharKind::Normal,
1044         }
1045     }
1046 }
1047
1048 impl<'a> Iterator for LineClasses<'a> {
1049     type Item = (FullCodeCharKind, String);
1050
1051     fn next(&mut self) -> Option<Self::Item> {
1052         self.base.peek()?;
1053
1054         let mut line = String::new();
1055
1056         let start_class = match self.base.peek() {
1057             Some((kind, _)) => *kind,
1058             None => FullCodeCharKind::Normal,
1059         };
1060
1061         while let Some((kind, c)) = self.base.next() {
1062             if c == '\n' {
1063                 self.kind = match (start_class, kind) {
1064                     (FullCodeCharKind::Normal, FullCodeCharKind::InString) => {
1065                         FullCodeCharKind::StartString
1066                     }
1067                     (FullCodeCharKind::InString, FullCodeCharKind::Normal) => {
1068                         FullCodeCharKind::EndString
1069                     }
1070                     _ => kind,
1071                 };
1072                 break;
1073             } else {
1074                 line.push(c);
1075             }
1076         }
1077
1078         Some((self.kind, line))
1079     }
1080 }
1081
1082 /// Iterator over functional and commented parts of a string. Any part of a string is either
1083 /// functional code, either *one* block comment, either *one* line comment. Whitespace between
1084 /// comments is functional code. Line comments contain their ending newlines.
1085 struct UngroupedCommentCodeSlices<'a> {
1086     slice: &'a str,
1087     iter: iter::Peekable<CharClasses<std::str::CharIndices<'a>>>,
1088 }
1089
1090 impl<'a> UngroupedCommentCodeSlices<'a> {
1091     fn new(code: &'a str) -> UngroupedCommentCodeSlices<'a> {
1092         UngroupedCommentCodeSlices {
1093             slice: code,
1094             iter: CharClasses::new(code.char_indices()).peekable(),
1095         }
1096     }
1097 }
1098
1099 impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1100     type Item = (CodeCharKind, usize, &'a str);
1101
1102     fn next(&mut self) -> Option<Self::Item> {
1103         let (kind, (start_idx, _)) = self.iter.next()?;
1104         match kind {
1105             FullCodeCharKind::Normal | FullCodeCharKind::InString => {
1106                 // Consume all the Normal code
1107                 while let Some(&(char_kind, _)) = self.iter.peek() {
1108                     if char_kind.is_comment() {
1109                         break;
1110                     }
1111                     let _ = self.iter.next();
1112                 }
1113             }
1114             FullCodeCharKind::StartComment => {
1115                 // Consume the whole comment
1116                 while let Some((FullCodeCharKind::InComment, (_, _))) = self.iter.next() {}
1117             }
1118             _ => panic!(),
1119         }
1120         let slice = match self.iter.peek() {
1121             Some(&(_, (end_idx, _))) => &self.slice[start_idx..end_idx],
1122             None => &self.slice[start_idx..],
1123         };
1124         Some((
1125             if kind.is_comment() {
1126                 CodeCharKind::Comment
1127             } else {
1128                 CodeCharKind::Normal
1129             },
1130             start_idx,
1131             slice,
1132         ))
1133     }
1134 }
1135
1136 /// Iterator over an alternating sequence of functional and commented parts of
1137 /// a string. The first item is always a, possibly zero length, subslice of
1138 /// functional text. Line style comments contain their ending newlines.
1139 pub struct CommentCodeSlices<'a> {
1140     slice: &'a str,
1141     last_slice_kind: CodeCharKind,
1142     last_slice_end: usize,
1143 }
1144
1145 impl<'a> CommentCodeSlices<'a> {
1146     pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
1147         CommentCodeSlices {
1148             slice,
1149             last_slice_kind: CodeCharKind::Comment,
1150             last_slice_end: 0,
1151         }
1152     }
1153 }
1154
1155 impl<'a> Iterator for CommentCodeSlices<'a> {
1156     type Item = (CodeCharKind, usize, &'a str);
1157
1158     fn next(&mut self) -> Option<Self::Item> {
1159         if self.last_slice_end == self.slice.len() {
1160             return None;
1161         }
1162
1163         let mut sub_slice_end = self.last_slice_end;
1164         let mut first_whitespace = None;
1165         let subslice = &self.slice[self.last_slice_end..];
1166         let mut iter = CharClasses::new(subslice.char_indices());
1167
1168         for (kind, (i, c)) in &mut iter {
1169             let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal
1170                 && &subslice[..2] == "//"
1171                 && [' ', '\t'].contains(&c);
1172
1173             if is_comment_connector && first_whitespace.is_none() {
1174                 first_whitespace = Some(i);
1175             }
1176
1177             if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
1178                 let last_index = match first_whitespace {
1179                     Some(j) => j,
1180                     None => i,
1181                 };
1182                 sub_slice_end = self.last_slice_end + last_index;
1183                 break;
1184             }
1185
1186             if !is_comment_connector {
1187                 first_whitespace = None;
1188             }
1189         }
1190
1191         if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
1192             // This was the last subslice.
1193             sub_slice_end = match first_whitespace {
1194                 Some(i) => self.last_slice_end + i,
1195                 None => self.slice.len(),
1196             };
1197         }
1198
1199         let kind = match self.last_slice_kind {
1200             CodeCharKind::Comment => CodeCharKind::Normal,
1201             CodeCharKind::Normal => CodeCharKind::Comment,
1202         };
1203         let res = (
1204             kind,
1205             self.last_slice_end,
1206             &self.slice[self.last_slice_end..sub_slice_end],
1207         );
1208         self.last_slice_end = sub_slice_end;
1209         self.last_slice_kind = kind;
1210
1211         Some(res)
1212     }
1213 }
1214
1215 /// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1216 /// (if it fits in the width/offset, else return None), else return `new`
1217 pub fn recover_comment_removed(
1218     new: String,
1219     span: Span,
1220     context: &RewriteContext,
1221 ) -> Option<String> {
1222     let snippet = context.snippet(span);
1223     if snippet != new && changed_comment_content(snippet, &new) {
1224         // We missed some comments. Warn and keep the original text.
1225         if context.config.error_on_unformatted() {
1226             context.report.append(
1227                 context.source_map.span_to_filename(span).into(),
1228                 vec![FormattingError::from_span(
1229                     span,
1230                     &context.source_map,
1231                     ErrorKind::LostComment,
1232                 )],
1233             );
1234         }
1235         Some(snippet.to_owned())
1236     } else {
1237         Some(new)
1238     }
1239 }
1240
1241 pub fn filter_normal_code(code: &str) -> String {
1242     let mut buffer = String::with_capacity(code.len());
1243     LineClasses::new(code).for_each(|(kind, line)| match kind {
1244         FullCodeCharKind::Normal
1245         | FullCodeCharKind::StartString
1246         | FullCodeCharKind::InString
1247         | FullCodeCharKind::EndString => {
1248             buffer.push_str(&line);
1249             buffer.push('\n');
1250         }
1251         _ => (),
1252     });
1253     if !code.ends_with('\n') && buffer.ends_with('\n') {
1254         buffer.pop();
1255     }
1256     buffer
1257 }
1258
1259 /// Return true if the two strings of code have the same payload of comments.
1260 /// The payload of comments is everything in the string except:
1261 ///     - actual code (not comments)
1262 ///     - comment start/end marks
1263 ///     - whitespace
1264 ///     - '*' at the beginning of lines in block comments
1265 fn changed_comment_content(orig: &str, new: &str) -> bool {
1266     // Cannot write this as a fn since we cannot return types containing closures
1267     let code_comment_content = |code| {
1268         let slices = UngroupedCommentCodeSlices::new(code);
1269         slices
1270             .filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
1271             .flat_map(|(_, _, s)| CommentReducer::new(s))
1272     };
1273     let res = code_comment_content(orig).ne(code_comment_content(new));
1274     debug!(
1275         "comment::changed_comment_content: {}\norig: '{}'\nnew: '{}'\nraw_old: {}\nraw_new: {}",
1276         res,
1277         orig,
1278         new,
1279         code_comment_content(orig).collect::<String>(),
1280         code_comment_content(new).collect::<String>()
1281     );
1282     res
1283 }
1284
1285 /// Iterator over the 'payload' characters of a comment.
1286 /// It skips whitespace, comment start/end marks, and '*' at the beginning of lines.
1287 /// The comment must be one comment, ie not more than one start mark (no multiple line comments,
1288 /// for example).
1289 struct CommentReducer<'a> {
1290     is_block: bool,
1291     at_start_line: bool,
1292     iter: std::str::Chars<'a>,
1293 }
1294
1295 impl<'a> CommentReducer<'a> {
1296     fn new(comment: &'a str) -> CommentReducer<'a> {
1297         let is_block = comment.starts_with("/*");
1298         let comment = remove_comment_header(comment);
1299         CommentReducer {
1300             is_block,
1301             at_start_line: false, // There are no supplementary '*' on the first line
1302             iter: comment.chars(),
1303         }
1304     }
1305 }
1306
1307 impl<'a> Iterator for CommentReducer<'a> {
1308     type Item = char;
1309
1310     fn next(&mut self) -> Option<Self::Item> {
1311         loop {
1312             let mut c = self.iter.next()?;
1313             if self.is_block && self.at_start_line {
1314                 while c.is_whitespace() {
1315                     c = self.iter.next()?;
1316                 }
1317                 // Ignore leading '*'
1318                 if c == '*' {
1319                     c = self.iter.next()?;
1320                 }
1321             } else if c == '\n' {
1322                 self.at_start_line = true;
1323             }
1324             if !c.is_whitespace() {
1325                 return Some(c);
1326             }
1327         }
1328     }
1329 }
1330
1331 fn remove_comment_header(comment: &str) -> &str {
1332     if comment.starts_with("///") || comment.starts_with("//!") {
1333         &comment[3..]
1334     } else if comment.starts_with("//") {
1335         &comment[2..]
1336     } else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
1337         || comment.starts_with("/*!")
1338     {
1339         &comment[3..comment.len() - 2]
1340     } else {
1341         assert!(
1342             comment.starts_with("/*"),
1343             format!("string '{}' is not a comment", comment)
1344         );
1345         &comment[2..comment.len() - 2]
1346     }
1347 }
1348
1349 #[cfg(test)]
1350 mod test {
1351     use super::*;
1352     use shape::{Indent, Shape};
1353
1354     #[test]
1355     fn char_classes() {
1356         let mut iter = CharClasses::new("//\n\n".chars());
1357
1358         assert_eq!((FullCodeCharKind::StartComment, '/'), iter.next().unwrap());
1359         assert_eq!((FullCodeCharKind::InComment, '/'), iter.next().unwrap());
1360         assert_eq!((FullCodeCharKind::EndComment, '\n'), iter.next().unwrap());
1361         assert_eq!((FullCodeCharKind::Normal, '\n'), iter.next().unwrap());
1362         assert_eq!(None, iter.next());
1363     }
1364
1365     #[test]
1366     fn comment_code_slices() {
1367         let input = "code(); /* test */ 1 + 1";
1368         let mut iter = CommentCodeSlices::new(input);
1369
1370         assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
1371         assert_eq!(
1372             (CodeCharKind::Comment, 8, "/* test */"),
1373             iter.next().unwrap()
1374         );
1375         assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
1376         assert_eq!(None, iter.next());
1377     }
1378
1379     #[test]
1380     fn comment_code_slices_two() {
1381         let input = "// comment\n    test();";
1382         let mut iter = CommentCodeSlices::new(input);
1383
1384         assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
1385         assert_eq!(
1386             (CodeCharKind::Comment, 0, "// comment\n"),
1387             iter.next().unwrap()
1388         );
1389         assert_eq!(
1390             (CodeCharKind::Normal, 11, "    test();"),
1391             iter.next().unwrap()
1392         );
1393         assert_eq!(None, iter.next());
1394     }
1395
1396     #[test]
1397     fn comment_code_slices_three() {
1398         let input = "1 // comment\n    // comment2\n\n";
1399         let mut iter = CommentCodeSlices::new(input);
1400
1401         assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
1402         assert_eq!(
1403             (CodeCharKind::Comment, 2, "// comment\n    // comment2\n"),
1404             iter.next().unwrap()
1405         );
1406         assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
1407         assert_eq!(None, iter.next());
1408     }
1409
1410     #[test]
1411     #[rustfmt::skip]
1412     fn format_comments() {
1413         let mut config: ::config::Config = Default::default();
1414         config.set().wrap_comments(true);
1415         config.set().normalize_comments(true);
1416
1417         let comment = rewrite_comment(" //test",
1418                                       true,
1419                                       Shape::legacy(100, Indent::new(0, 100)),
1420                                       &config).unwrap();
1421         assert_eq!("/* test */", comment);
1422
1423         let comment = rewrite_comment("// comment on a",
1424                                       false,
1425                                       Shape::legacy(10, Indent::empty()),
1426                                       &config).unwrap();
1427         assert_eq!("// comment\n// on a", comment);
1428
1429         let comment = rewrite_comment("//  A multi line comment\n             // between args.",
1430                                       false,
1431                                       Shape::legacy(60, Indent::new(0, 12)),
1432                                       &config).unwrap();
1433         assert_eq!("//  A multi line comment\n            // between args.", comment);
1434
1435         let input = "// comment";
1436         let expected =
1437             "/* comment */";
1438         let comment = rewrite_comment(input,
1439                                       true,
1440                                       Shape::legacy(9, Indent::new(0, 69)),
1441                                       &config).unwrap();
1442         assert_eq!(expected, comment);
1443
1444         let comment = rewrite_comment("/*   trimmed    */",
1445                                       true,
1446                                       Shape::legacy(100, Indent::new(0, 100)),
1447                                       &config).unwrap();
1448         assert_eq!("/* trimmed */", comment);
1449     }
1450
1451     // This is probably intended to be a non-test fn, but it is not used. I'm
1452     // keeping it around unless it helps us test stuff.
1453     fn uncommented(text: &str) -> String {
1454         CharClasses::new(text.chars())
1455             .filter_map(|(s, c)| match s {
1456                 FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
1457                 _ => None,
1458             }).collect()
1459     }
1460
1461     #[test]
1462     fn test_uncommented() {
1463         assert_eq!(&uncommented("abc/*...*/"), "abc");
1464         assert_eq!(
1465             &uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
1466             "..ac\n"
1467         );
1468         assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
1469     }
1470
1471     #[test]
1472     fn test_contains_comment() {
1473         assert_eq!(contains_comment("abc"), false);
1474         assert_eq!(contains_comment("abc // qsdf"), true);
1475         assert_eq!(contains_comment("abc /* kqsdf"), true);
1476         assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
1477     }
1478
1479     #[test]
1480     fn test_find_uncommented() {
1481         fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1482             assert_eq!(expected, haystack.find_uncommented(needle));
1483         }
1484
1485         check("/*/ */test", "test", Some(6));
1486         check("//test\ntest", "test", Some(7));
1487         check("/* comment only */", "whatever", None);
1488         check(
1489             "/* comment */ some text /* more commentary */ result",
1490             "result",
1491             Some(46),
1492         );
1493         check("sup // sup", "p", Some(2));
1494         check("sup", "x", None);
1495         check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
1496         check("/*sup yo? \n sup*/ sup", "p", Some(20));
1497         check("hel/*lohello*/lo", "hello", None);
1498         check("acb", "ab", None);
1499         check(",/*A*/ ", ",", Some(0));
1500         check("abc", "abc", Some(0));
1501         check("/* abc */", "abc", None);
1502         check("/**/abc/* */", "abc", Some(4));
1503         check("\"/* abc */\"", "abc", Some(4));
1504         check("\"/* abc", "abc", Some(4));
1505     }
1506
1507     #[test]
1508     fn test_remove_trailing_white_spaces() {
1509         let s = "    r#\"\n        test\n    \"#";
1510         assert_eq!(remove_trailing_white_spaces(&s), s);
1511     }
1512
1513     #[test]
1514     fn test_filter_normal_code() {
1515         let s = r#"
1516 fn main() {
1517     println!("hello, world");
1518 }
1519 "#;
1520         assert_eq!(s, filter_normal_code(s));
1521         let s_with_comment = r#"
1522 fn main() {
1523     // hello, world
1524     println!("hello, world");
1525 }
1526 "#;
1527         assert_eq!(s, filter_normal_code(s_with_comment));
1528     }
1529 }