]> git.lizzy.rs Git - rust.git/blob - src/comment.rs
Prevent right-shifting of block comments with bare lines.
[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     /// Returns true if the commenting style covers a line only.
56     pub fn is_line_comment(&self) -> bool {
57         match *self {
58             CommentStyle::DoubleSlash
59             | CommentStyle::TripleSlash
60             | CommentStyle::Doc
61             | CommentStyle::Custom(_) => true,
62             _ => false,
63         }
64     }
65
66     /// Returns true if the commenting style can span over multiple lines.
67     pub fn is_block_comment(&self) -> bool {
68         match *self {
69             CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
70                 true
71             }
72             _ => false,
73         }
74     }
75
76     pub fn is_doc_comment(&self) -> bool {
77         match *self {
78             CommentStyle::TripleSlash | CommentStyle::Doc => true,
79             _ => false,
80         }
81     }
82
83     pub fn opener(&self) -> &'a str {
84         match *self {
85             CommentStyle::DoubleSlash => "// ",
86             CommentStyle::TripleSlash => "/// ",
87             CommentStyle::Doc => "//! ",
88             CommentStyle::SingleBullet => "/* ",
89             CommentStyle::DoubleBullet => "/** ",
90             CommentStyle::Exclamation => "/*! ",
91             CommentStyle::Custom(opener) => opener,
92         }
93     }
94
95     pub fn closer(&self) -> &'a str {
96         match *self {
97             CommentStyle::DoubleSlash
98             | CommentStyle::TripleSlash
99             | CommentStyle::Custom(..)
100             | CommentStyle::Doc => "",
101             CommentStyle::DoubleBullet => " **/",
102             CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
103         }
104     }
105
106     pub fn line_start(&self) -> &'a str {
107         match *self {
108             CommentStyle::DoubleSlash => "// ",
109             CommentStyle::TripleSlash => "/// ",
110             CommentStyle::Doc => "//! ",
111             CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
112             CommentStyle::DoubleBullet => " ** ",
113             CommentStyle::Custom(opener) => opener,
114         }
115     }
116
117     pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
118         (self.opener(), self.closer(), self.line_start())
119     }
120 }
121
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("//!") {
133             CommentStyle::Doc
134         } else if is_custom_comment(orig) {
135             CommentStyle::Custom(custom_opener(orig))
136         } else {
137             CommentStyle::DoubleSlash
138         }
139     } else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/'))
140         || (orig.starts_with("/**") && !orig.starts_with("/**/"))
141     {
142         CommentStyle::TripleSlash
143     } else if orig.starts_with("//!") || orig.starts_with("/*!") {
144         CommentStyle::Doc
145     } else if is_custom_comment(orig) {
146         CommentStyle::Custom(custom_opener(orig))
147     } else {
148         CommentStyle::DoubleSlash
149     }
150 }
151
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,
159     prev_str: &str,
160     next_str: &str,
161     span: Span,
162     shape: Shape,
163     allow_extend: bool,
164 ) -> Option<String> {
165     let mut result =
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() {
170         ""
171     } else {
172         " "
173     };
174     let mut one_line_width =
175         last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
176
177     let config = context.config;
178     let indent = shape.indent;
179     let missing_comment = rewrite_missing_comment(span, shape, context)?;
180
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))
186         }
187         result.push_str(next_str);
188         return Some(result);
189     }
190
191     // We have a missing comment between the first expression and the second expression.
192
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')
199     } else {
200         !original_snippet.contains('\n')
201     };
202
203     one_line_width -= first_sep.len();
204     let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
205         Cow::from("")
206     } else {
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 {
209             Cow::from(" ")
210         } else {
211             indent.to_string_with_newline(config)
212         }
213     };
214     result.push_str(&first_sep);
215     result.push_str(&missing_comment);
216
217     let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
218         Cow::from("")
219     } else if missing_comment.starts_with("//") {
220         indent.to_string_with_newline(config)
221     } else {
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 {
225             Cow::from(" ")
226         } else {
227             indent.to_string_with_newline(config)
228         }
229     };
230     result.push_str(&second_sep);
231     result.push_str(next_str);
232
233     Some(result)
234 }
235
236 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
237     identify_comment(orig, false, shape, config, true)
238 }
239
240 pub fn rewrite_comment(
241     orig: &str,
242     block_style: bool,
243     shape: Shape,
244     config: &Config,
245 ) -> Option<String> {
246     identify_comment(orig, block_style, shape, config, false)
247 }
248
249 fn identify_comment(
250     orig: &str,
251     block_style: bool,
252     shape: Shape,
253     config: &Config,
254     is_doc_comment: bool,
255 ) -> Option<String> {
256     let style = comment_style(orig, false);
257
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' {
262                 line.len() + 2
263             } else {
264                 line.len() + 1
265             }
266         } else {
267             line.len()
268         }
269     }
270
271     // Get the first group of line comments having the same commenting style.
272     //
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(
277         style: CommentStyle,
278         orig: &str,
279         line_start: &str,
280     ) -> (bool, usize) {
281         let mut first_group_ending = 0;
282         let mut hbl = false;
283
284         for line in orig.lines() {
285             let trimmed_line = line.trim_left();
286             if trimmed_line.is_empty() {
287                 hbl = true;
288                 break;
289             } else if trimmed_line.starts_with(line_start)
290                 || comment_style(trimmed_line, false) == style
291             {
292                 first_group_ending += compute_len(&orig[first_group_ending..], line);
293             } else {
294                 break;
295             }
296         }
297         (hbl, first_group_ending)
298     }
299
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)
304         }
305         CommentStyle::Custom(opener) => {
306             let trimmed_opener = opener.trim_right();
307             consume_same_line_comments(style, orig, trimmed_opener)
308         }
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;
313             let mut hbl = false;
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("/*")
320                 {
321                     hbl = true;
322                 }
323                 if trimmed_line.ends_with(closer) {
324                     break;
325                 }
326             }
327             (hbl, closing_symbol_offset)
328         }
329     };
330
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)?
337         } else {
338             rewrite_comment_inner(
339                 first_group,
340                 block_style,
341                 style,
342                 shape,
343                 config,
344                 is_doc_comment || style.is_doc_comment(),
345             )?
346         };
347     if rest.is_empty() {
348         Some(rewritten_first_group)
349     } else {
350         identify_comment(rest.trim_left(), block_style, shape, config, is_doc_comment).map(
351             |rest_str| {
352                 format!(
353                     "{}\n{}{}{}",
354                     rewritten_first_group,
355                     // insert back the blank line
356                     if has_bare_lines && style.is_line_comment() {
357                         "\n"
358                     } else {
359                         ""
360                     },
361                     shape.indent.to_string(config),
362                     rest_str
363                 )
364             },
365         )
366     }
367 }
368
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(
371     orig: &str,
372     shape: Shape,
373     config: &Config,
374 ) -> Option<String> {
375     let prefix_whitespace_min = orig
376         .lines()
377         // skip the line with the starting sigil since the leading whitespace is removed
378         // otherwise, the minimum would always be zero
379         .skip(1)
380         .filter(|line| !line.is_empty())
381         .map(|line| {
382             let mut width = 0;
383             for c in line.chars() {
384                 match c {
385                     ' ' => width += 1,
386                     '\t' => width += config.tab_spaces(),
387                     _ => break,
388                 }
389             }
390             width
391         })
392         .min()?;
393
394     let indent_str = shape.indent.to_string(config);
395     let mut lines = orig.lines();
396     let first_line = lines.next()?;
397     let rest = lines
398         .map(|line| {
399             if line.is_empty() {
400                 line
401             } else {
402                 &line[prefix_whitespace_min..]
403             }
404         })
405         .collect::<Vec<&str>>()
406         .join(&format!("\n{}", indent_str));
407     Some(format!("{}\n{}{}", first_line, indent_str, rest))
408 }
409
410 fn rewrite_comment_inner(
411     orig: &str,
412     block_style: bool,
413     style: CommentStyle,
414     shape: Shape,
415     config: &Config,
416     is_doc_comment: bool,
417 ) -> Option<String> {
418     let (opener, closer, line_start) = if block_style {
419         CommentStyle::SingleBullet.to_str_tuplet()
420     } else {
421         comment_style(orig, config.normalize_comments()).to_str_tuplet()
422     };
423
424     let max_chars = shape
425         .width
426         .checked_sub(closer.len() + opener.len())
427         .unwrap_or(1);
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 {
431         opener: "",
432         closer: "",
433         line_start,
434         line_end: "",
435         shape: Shape::legacy(max_chars, fmt_indent),
436         trim_end: true,
437         config,
438     };
439
440     let line_breaks = count_newlines(orig.trim_right());
441     let lines = orig
442         .lines()
443         .enumerate()
444         .map(|(i, mut line)| {
445             line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
446             // Drop old closer.
447             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
448                 line = line[..(line.len() - 2)].trim_right();
449             }
450
451             line
452         })
453         .map(|s| left_trim_comment_line(s, &style))
454         .map(|(line, has_leading_whitespace)| {
455             if orig.starts_with("/*") && line_breaks == 0 {
456                 (
457                     line.trim_left(),
458                     has_leading_whitespace || config.normalize_comments(),
459                 )
460             } else {
461                 (line, has_leading_whitespace || config.normalize_comments())
462             }
463         });
464
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,
479                 None => "",
480             });
481         }
482         result
483     };
484
485     for (i, (line, has_leading_whitespace)) in lines.enumerate() {
486         let is_last = i == count_newlines(orig);
487
488         if inside_code_block {
489             if line.starts_with("```") {
490                 inside_code_block = false;
491                 result.push_str(&comment_line_separator);
492                 let code_block = {
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),
498                     }
499                 };
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);
504             } else {
505                 code_block_buffer.push_str(&hide_sharp_behind_comment(line));
506                 code_block_buffer.push('\n');
507
508                 if is_last {
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),
514                     ));
515                 }
516             }
517
518             continue;
519         } else {
520             inside_code_block = line.starts_with("```");
521
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(' ') {
525                     result.pop();
526                 }
527                 if line.is_empty() {
528                     continue;
529                 }
530             } else if is_prev_line_multi_line && !line.is_empty() {
531                 result.push(' ')
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);
536                 }
537                 break;
538             } else {
539                 result.push_str(&comment_line_separator);
540                 if !has_leading_whitespace && result.ends_with(' ') {
541                     result.pop();
542                 }
543             }
544         }
545
546         if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
547             match rewrite_string(line, &fmt) {
548                 Some(ref s) => {
549                     is_prev_line_multi_line = s.contains('\n');
550                     result.push_str(s);
551                 }
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.
555                     result.pop();
556                     result.push_str(&comment_line_separator);
557                     fmt.shape = Shape::legacy(max_chars, fmt_indent);
558                     match rewrite_string(line, &fmt) {
559                         Some(ref s) => {
560                             is_prev_line_multi_line = s.contains('\n');
561                             result.push_str(s);
562                         }
563                         None => {
564                             is_prev_line_multi_line = false;
565                             result.push_str(line);
566                         }
567                     }
568                 }
569                 None => {
570                     is_prev_line_multi_line = false;
571                     result.push_str(line);
572                 }
573             }
574
575             fmt.shape = if is_prev_line_multi_line {
576                 // 1 = " "
577                 let offset = 1 + last_line_width(&result) - line_start.len();
578                 Shape {
579                     width: max_chars.saturating_sub(offset),
580                     indent: fmt_indent,
581                     offset: fmt.shape.offset + offset,
582                 }
583             } else {
584                 Shape::legacy(max_chars, fmt_indent)
585             };
586         } else {
587             if line.is_empty() && result.ends_with(' ') && !is_last {
588                 // Remove space if this is an empty comment or a doc comment.
589                 result.pop();
590             }
591             result.push_str(line);
592             fmt.shape = Shape::legacy(max_chars, fmt_indent);
593             is_prev_line_multi_line = false;
594         }
595     }
596
597     result.push_str(closer);
598     if result.ends_with(opener) && opener.ends_with(' ') {
599         // Trailing space.
600         result.pop();
601     }
602
603     Some(result)
604 }
605
606 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
607
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))
611     } else {
612         Cow::from(s)
613     }
614 }
615
616 fn trim_custom_comment_prefix(s: &str) -> String {
617     s.lines()
618         .map(|line| {
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)
622             } else {
623                 line
624             }
625         })
626         .collect::<Vec<_>>()
627         .join("\n")
628 }
629
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://")
634 }
635
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(
639     span: Span,
640     shape: Shape,
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)
647     } else {
648         Some(String::new())
649     }
650 }
651
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(
656     span: Span,
657     shape: Shape,
658     context: &RewriteContext,
659     used_width: usize,
660 ) -> Option<String> {
661     let missing_comment = rewrite_missing_comment(span, shape, context)?;
662     if missing_comment.is_empty() {
663         Some(String::new())
664     } else {
665         let missing_snippet = context.snippet(span);
666         let pos = missing_snippet.find('/').unwrap_or(0);
667         // 1 = ` `
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)
673         } else {
674             Cow::from(" ")
675         };
676         Some(format!("{}{}", sep, missing_comment))
677     }
678 }
679
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("  ") {
683         s
684     } else {
685         s.trim_right()
686     }
687 }
688
689 /// Trims whitespace and aligns to indent, but otherwise does not change comments.
690 fn light_rewrite_comment(
691     orig: &str,
692     offset: Indent,
693     config: &Config,
694     is_doc_comment: bool,
695 ) -> Option<String> {
696     let lines: Vec<&str> = orig
697         .lines()
698         .map(|l| {
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
701             // `*` in `/*`.
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 {
705                     &l[fnw - 1..]
706                 } else {
707                     &l[fnw..]
708                 }
709             } else {
710                 ""
711             };
712             // Preserve markdown's double-space line break syntax in doc comment.
713             trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
714         })
715         .collect();
716     Some(lines.join(&format!("\n{}", offset.to_string(config))))
717 }
718
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("/** ")
727     {
728         (&line[4..], true)
729     } else if let CommentStyle::Custom(opener) = *style {
730         if line.starts_with(opener) {
731             (&line[opener.len()..], true)
732         } else {
733             (&line[opener.trim_right().len()..], false)
734         }
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("/**/"))
742     {
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("**")
748     {
749         (&line[2..], line.chars().nth(1).unwrap() == ' ')
750     } else if line.starts_with('*') {
751         (&line[1..], false)
752     } else {
753         (line, line.starts_with(' '))
754     }
755 }
756
757 pub trait FindUncommented {
758     fn find_uncommented(&self, pat: &str) -> Option<usize>;
759 }
760
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() {
766                 None => {
767                     return Some(i - pat.len());
768                 }
769                 Some(c) => match kind {
770                     FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
771                     _ => {
772                         needle_iter = pat.chars();
773                     }
774                 },
775             }
776         }
777
778         // Handle case where the pattern is a suffix of the search string
779         match needle_iter.next() {
780             Some(_) => None,
781             None => Some(self.len() - pat.len()),
782         }
783     }
784 }
785
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 {
794             return Some(i);
795         }
796     }
797
798     // Handle case where the comment ends at the end of s.
799     if iter.status == CharClassesStatus::Normal {
800         Some(s.len())
801     } else {
802         None
803     }
804 }
805
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())
809 }
810
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()) {
817         match c {
818             '\n' => {
819                 if char_kind == FullCodeCharKind::InString {
820                     buffer.push_str(&space_buffer);
821                 }
822                 space_buffer.clear();
823                 buffer.push('\n');
824             }
825             _ if c.is_whitespace() => {
826                 space_buffer.push(c);
827             }
828             _ => {
829                 if !space_buffer.is_empty() {
830                     buffer.push_str(&space_buffer);
831                     space_buffer.clear();
832                 }
833                 buffer.push(c);
834             }
835         }
836     }
837     buffer
838 }
839
840 pub struct CharClasses<T>
841 where
842     T: Iterator,
843     T::Item: RichChar,
844 {
845     base: MultiPeek<T>,
846     status: CharClassesStatus,
847 }
848
849 pub trait RichChar {
850     fn get_char(&self) -> char;
851 }
852
853 impl RichChar for char {
854     fn get_char(&self) -> char {
855         *self
856     }
857 }
858
859 impl RichChar for (usize, char) {
860     fn get_char(&self) -> char {
861         self.1
862     }
863 }
864
865 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
866 enum CharClassesStatus {
867     Normal,
868     LitString,
869     LitStringEscape,
870     LitRawString(u32),
871     RawStringPrefix(u32),
872     RawStringSuffix(u32),
873     LitChar,
874     LitCharEscape,
875     // The u32 is the nesting deepness of the comment
876     BlockComment(u32),
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),
883     LineComment,
884 }
885
886 /// Distinguish between functional part of code and comments
887 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
888 pub enum CodeCharKind {
889     Normal,
890     Comment,
891 }
892
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 {
898     Normal,
899     /// The first character of a comment, there is only one for a comment (always '/')
900     StartComment,
901     /// Any character inside a comment including the second character of comment
902     /// marks ("//", "/*")
903     InComment,
904     /// Last character of a comment, '\n' for a line comment, '/' for a block comment.
905     EndComment,
906     /// Start of a mutlitine string
907     StartString,
908     /// End of a mutlitine string
909     EndString,
910     /// Inside a string.
911     InString,
912 }
913
914 impl FullCodeCharKind {
915     pub fn is_comment(self) -> bool {
916         match self {
917             FullCodeCharKind::StartComment
918             | FullCodeCharKind::InComment
919             | FullCodeCharKind::EndComment => true,
920             _ => false,
921         }
922     }
923
924     pub fn is_string(self) -> bool {
925         self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
926     }
927
928     fn to_codecharkind(self) -> CodeCharKind {
929         if self.is_comment() {
930             CodeCharKind::Comment
931         } else {
932             CodeCharKind::Normal
933         }
934     }
935 }
936
937 impl<T> CharClasses<T>
938 where
939     T: Iterator,
940     T::Item: RichChar,
941 {
942     pub fn new(base: T) -> CharClasses<T> {
943         CharClasses {
944             base: multipeek(base),
945             status: CharClassesStatus::Normal,
946         }
947     }
948 }
949
950 fn is_raw_string_suffix<T>(iter: &mut MultiPeek<T>, count: u32) -> bool
951 where
952     T: Iterator,
953     T::Item: RichChar,
954 {
955     for _ in 0..count {
956         match iter.peek() {
957             Some(c) if c.get_char() == '#' => continue,
958             _ => return false,
959         }
960     }
961     true
962 }
963
964 impl<T> Iterator for CharClasses<T>
965 where
966     T: Iterator,
967     T::Item: RichChar,
968 {
969     type Item = (FullCodeCharKind, T::Item);
970
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;
978                 match chr {
979                     '"' => {
980                         if sharps == 0 {
981                             char_kind = FullCodeCharKind::Normal;
982                             CharClassesStatus::Normal
983                         } else if is_raw_string_suffix(&mut self.base, sharps) {
984                             CharClassesStatus::RawStringSuffix(sharps)
985                         } else {
986                             CharClassesStatus::LitRawString(sharps)
987                         }
988                     }
989                     _ => CharClassesStatus::LitRawString(sharps),
990                 }
991             }
992             CharClassesStatus::RawStringPrefix(sharps) => {
993                 char_kind = FullCodeCharKind::InString;
994                 match chr {
995                     '#' => CharClassesStatus::RawStringPrefix(sharps + 1),
996                     '"' => CharClassesStatus::LitRawString(sharps),
997                     _ => CharClassesStatus::Normal, // Unreachable.
998                 }
999             }
1000             CharClassesStatus::RawStringSuffix(sharps) => {
1001                 match chr {
1002                     '#' => {
1003                         if sharps == 1 {
1004                             CharClassesStatus::Normal
1005                         } else {
1006                             char_kind = FullCodeCharKind::InString;
1007                             CharClassesStatus::RawStringSuffix(sharps - 1)
1008                         }
1009                     }
1010                     _ => CharClassesStatus::Normal, // Unreachable
1011                 }
1012             }
1013             CharClassesStatus::LitString => {
1014                 char_kind = FullCodeCharKind::InString;
1015                 match chr {
1016                     '"' => CharClassesStatus::Normal,
1017                     '\\' => CharClassesStatus::LitStringEscape,
1018                     _ => CharClassesStatus::LitString,
1019                 }
1020             }
1021             CharClassesStatus::LitStringEscape => {
1022                 char_kind = FullCodeCharKind::InString;
1023                 CharClassesStatus::LitString
1024             }
1025             CharClassesStatus::LitChar => match chr {
1026                 '\\' => CharClassesStatus::LitCharEscape,
1027                 '\'' => CharClassesStatus::Normal,
1028                 _ => CharClassesStatus::LitChar,
1029             },
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)
1036                     }
1037                     _ => CharClassesStatus::Normal,
1038                 },
1039                 '"' => {
1040                     char_kind = FullCodeCharKind::InString;
1041                     CharClassesStatus::LitString
1042                 }
1043                 '\'' => {
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));
1049                         }
1050                         _ => (),
1051                     }
1052
1053                     match self.base.peek() {
1054                         Some(next) if next.get_char() == '\'' => CharClassesStatus::LitChar,
1055                         _ => CharClassesStatus::Normal,
1056                     }
1057                 }
1058                 '/' => match self.base.peek() {
1059                     Some(next) if next.get_char() == '*' => {
1060                         self.status = CharClassesStatus::BlockCommentOpening(1);
1061                         return Some((FullCodeCharKind::StartComment, item));
1062                     }
1063                     Some(next) if next.get_char() == '/' => {
1064                         self.status = CharClassesStatus::LineComment;
1065                         return Some((FullCodeCharKind::StartComment, item));
1066                     }
1067                     _ => CharClassesStatus::Normal,
1068                 },
1069                 _ => CharClassesStatus::Normal,
1070             },
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)
1076                     }
1077                     Some(next) if next.get_char() == '*' && chr == '/' => {
1078                         CharClassesStatus::BlockCommentOpening(deepness + 1)
1079                     }
1080                     _ => CharClassesStatus::BlockComment(deepness),
1081                 };
1082                 return Some((FullCodeCharKind::InComment, item));
1083             }
1084             CharClassesStatus::BlockCommentOpening(deepness) => {
1085                 assert_eq!(chr, '*');
1086                 self.status = CharClassesStatus::BlockComment(deepness);
1087                 return Some((FullCodeCharKind::InComment, item));
1088             }
1089             CharClassesStatus::BlockCommentClosing(deepness) => {
1090                 assert_eq!(chr, '/');
1091                 if deepness == 0 {
1092                     self.status = CharClassesStatus::Normal;
1093                     return Some((FullCodeCharKind::EndComment, item));
1094                 } else {
1095                     self.status = CharClassesStatus::BlockComment(deepness);
1096                     return Some((FullCodeCharKind::InComment, item));
1097                 }
1098             }
1099             CharClassesStatus::LineComment => match chr {
1100                 '\n' => {
1101                     self.status = CharClassesStatus::Normal;
1102                     return Some((FullCodeCharKind::EndComment, item));
1103                 }
1104                 _ => {
1105                     self.status = CharClassesStatus::LineComment;
1106                     return Some((FullCodeCharKind::InComment, item));
1107                 }
1108             },
1109         };
1110         Some((char_kind, item))
1111     }
1112 }
1113
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,
1119 }
1120
1121 impl<'a> LineClasses<'a> {
1122     pub fn new(s: &'a str) -> Self {
1123         LineClasses {
1124             base: CharClasses::new(s.chars()).peekable(),
1125             kind: FullCodeCharKind::Normal,
1126         }
1127     }
1128 }
1129
1130 impl<'a> Iterator for LineClasses<'a> {
1131     type Item = (FullCodeCharKind, String);
1132
1133     fn next(&mut self) -> Option<Self::Item> {
1134         self.base.peek()?;
1135
1136         let mut line = String::new();
1137
1138         let start_class = match self.base.peek() {
1139             Some((kind, _)) => *kind,
1140             None => FullCodeCharKind::Normal,
1141         };
1142
1143         while let Some((kind, c)) = self.base.next() {
1144             if c == '\n' {
1145                 self.kind = match (start_class, kind) {
1146                     (FullCodeCharKind::Normal, FullCodeCharKind::InString) => {
1147                         FullCodeCharKind::StartString
1148                     }
1149                     (FullCodeCharKind::InString, FullCodeCharKind::Normal) => {
1150                         FullCodeCharKind::EndString
1151                     }
1152                     _ => kind,
1153                 };
1154                 break;
1155             } else {
1156                 line.push(c);
1157             }
1158         }
1159
1160         Some((self.kind, line))
1161     }
1162 }
1163
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> {
1168     slice: &'a str,
1169     iter: iter::Peekable<CharClasses<std::str::CharIndices<'a>>>,
1170 }
1171
1172 impl<'a> UngroupedCommentCodeSlices<'a> {
1173     fn new(code: &'a str) -> UngroupedCommentCodeSlices<'a> {
1174         UngroupedCommentCodeSlices {
1175             slice: code,
1176             iter: CharClasses::new(code.char_indices()).peekable(),
1177         }
1178     }
1179 }
1180
1181 impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1182     type Item = (CodeCharKind, usize, &'a str);
1183
1184     fn next(&mut self) -> Option<Self::Item> {
1185         let (kind, (start_idx, _)) = self.iter.next()?;
1186         match kind {
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() {
1191                         break;
1192                     }
1193                     let _ = self.iter.next();
1194                 }
1195             }
1196             FullCodeCharKind::StartComment => {
1197                 // Consume the whole comment
1198                 while let Some((FullCodeCharKind::InComment, (_, _))) = self.iter.next() {}
1199             }
1200             _ => panic!(),
1201         }
1202         let slice = match self.iter.peek() {
1203             Some(&(_, (end_idx, _))) => &self.slice[start_idx..end_idx],
1204             None => &self.slice[start_idx..],
1205         };
1206         Some((
1207             if kind.is_comment() {
1208                 CodeCharKind::Comment
1209             } else {
1210                 CodeCharKind::Normal
1211             },
1212             start_idx,
1213             slice,
1214         ))
1215     }
1216 }
1217
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> {
1222     slice: &'a str,
1223     last_slice_kind: CodeCharKind,
1224     last_slice_end: usize,
1225 }
1226
1227 impl<'a> CommentCodeSlices<'a> {
1228     pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
1229         CommentCodeSlices {
1230             slice,
1231             last_slice_kind: CodeCharKind::Comment,
1232             last_slice_end: 0,
1233         }
1234     }
1235 }
1236
1237 impl<'a> Iterator for CommentCodeSlices<'a> {
1238     type Item = (CodeCharKind, usize, &'a str);
1239
1240     fn next(&mut self) -> Option<Self::Item> {
1241         if self.last_slice_end == self.slice.len() {
1242             return None;
1243         }
1244
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());
1249
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);
1254
1255             if is_comment_connector && first_whitespace.is_none() {
1256                 first_whitespace = Some(i);
1257             }
1258
1259             if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
1260                 let last_index = match first_whitespace {
1261                     Some(j) => j,
1262                     None => i,
1263                 };
1264                 sub_slice_end = self.last_slice_end + last_index;
1265                 break;
1266             }
1267
1268             if !is_comment_connector {
1269                 first_whitespace = None;
1270             }
1271         }
1272
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(),
1278             };
1279         }
1280
1281         let kind = match self.last_slice_kind {
1282             CodeCharKind::Comment => CodeCharKind::Normal,
1283             CodeCharKind::Normal => CodeCharKind::Comment,
1284         };
1285         let res = (
1286             kind,
1287             self.last_slice_end,
1288             &self.slice[self.last_slice_end..sub_slice_end],
1289         );
1290         self.last_slice_end = sub_slice_end;
1291         self.last_slice_kind = kind;
1292
1293         Some(res)
1294     }
1295 }
1296
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(
1300     new: String,
1301     span: Span,
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(
1311                     span,
1312                     &context.source_map,
1313                     ErrorKind::LostComment,
1314                 )],
1315             );
1316         }
1317         Some(snippet.to_owned())
1318     } else {
1319         Some(new)
1320     }
1321 }
1322
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);
1331             buffer.push('\n');
1332         }
1333         _ => (),
1334     });
1335     if !code.ends_with('\n') && buffer.ends_with('\n') {
1336         buffer.pop();
1337     }
1338     buffer
1339 }
1340
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
1345 ///     - whitespace
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);
1351         slices
1352             .filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
1353             .flat_map(|(_, _, s)| CommentReducer::new(s))
1354     };
1355     let res = code_comment_content(orig).ne(code_comment_content(new));
1356     debug!(
1357         "comment::changed_comment_content: {}\norig: '{}'\nnew: '{}'\nraw_old: {}\nraw_new: {}",
1358         res,
1359         orig,
1360         new,
1361         code_comment_content(orig).collect::<String>(),
1362         code_comment_content(new).collect::<String>()
1363     );
1364     res
1365 }
1366
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,
1370 /// for example).
1371 struct CommentReducer<'a> {
1372     is_block: bool,
1373     at_start_line: bool,
1374     iter: std::str::Chars<'a>,
1375 }
1376
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);
1381         CommentReducer {
1382             is_block,
1383             at_start_line: false, // There are no supplementary '*' on the first line
1384             iter: comment.chars(),
1385         }
1386     }
1387 }
1388
1389 impl<'a> Iterator for CommentReducer<'a> {
1390     type Item = char;
1391
1392     fn next(&mut self) -> Option<Self::Item> {
1393         loop {
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()?;
1398                 }
1399                 // Ignore leading '*'
1400                 if c == '*' {
1401                     c = self.iter.next()?;
1402                 }
1403             } else if c == '\n' {
1404                 self.at_start_line = true;
1405             }
1406             if !c.is_whitespace() {
1407                 return Some(c);
1408             }
1409         }
1410     }
1411 }
1412
1413 fn remove_comment_header(comment: &str) -> &str {
1414     if comment.starts_with("///") || comment.starts_with("//!") {
1415         &comment[3..]
1416     } else if comment.starts_with("//") {
1417         &comment[2..]
1418     } else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
1419         || comment.starts_with("/*!")
1420     {
1421         &comment[3..comment.len() - 2]
1422     } else {
1423         assert!(
1424             comment.starts_with("/*"),
1425             format!("string '{}' is not a comment", comment)
1426         );
1427         &comment[2..comment.len() - 2]
1428     }
1429 }
1430
1431 #[cfg(test)]
1432 mod test {
1433     use super::*;
1434     use shape::{Indent, Shape};
1435
1436     #[test]
1437     fn char_classes() {
1438         let mut iter = CharClasses::new("//\n\n".chars());
1439
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());
1445     }
1446
1447     #[test]
1448     fn comment_code_slices() {
1449         let input = "code(); /* test */ 1 + 1";
1450         let mut iter = CommentCodeSlices::new(input);
1451
1452         assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
1453         assert_eq!(
1454             (CodeCharKind::Comment, 8, "/* test */"),
1455             iter.next().unwrap()
1456         );
1457         assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
1458         assert_eq!(None, iter.next());
1459     }
1460
1461     #[test]
1462     fn comment_code_slices_two() {
1463         let input = "// comment\n    test();";
1464         let mut iter = CommentCodeSlices::new(input);
1465
1466         assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
1467         assert_eq!(
1468             (CodeCharKind::Comment, 0, "// comment\n"),
1469             iter.next().unwrap()
1470         );
1471         assert_eq!(
1472             (CodeCharKind::Normal, 11, "    test();"),
1473             iter.next().unwrap()
1474         );
1475         assert_eq!(None, iter.next());
1476     }
1477
1478     #[test]
1479     fn comment_code_slices_three() {
1480         let input = "1 // comment\n    // comment2\n\n";
1481         let mut iter = CommentCodeSlices::new(input);
1482
1483         assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
1484         assert_eq!(
1485             (CodeCharKind::Comment, 2, "// comment\n    // comment2\n"),
1486             iter.next().unwrap()
1487         );
1488         assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
1489         assert_eq!(None, iter.next());
1490     }
1491
1492     #[test]
1493     #[rustfmt::skip]
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);
1498
1499         let mut wrap_config: ::config::Config = Default::default();
1500         wrap_config.set().wrap_comments(true);
1501
1502         let comment = rewrite_comment(" //test",
1503                                       true,
1504                                       Shape::legacy(100, Indent::new(0, 100)),
1505                                       &wrap_normalize_config).unwrap();
1506         assert_eq!("/* test */", comment);
1507
1508         let comment = rewrite_comment("// comment on a",
1509                                       false,
1510                                       Shape::legacy(10, Indent::empty()),
1511                                       &wrap_normalize_config).unwrap();
1512         assert_eq!("// comment\n// on a", comment);
1513
1514         let comment = rewrite_comment("//  A multi line comment\n             // between args.",
1515                                       false,
1516                                       Shape::legacy(60, Indent::new(0, 12)),
1517                                       &wrap_normalize_config).unwrap();
1518         assert_eq!("//  A multi line comment\n            // between args.", comment);
1519
1520         let input = "// comment";
1521         let expected =
1522             "/* comment */";
1523         let comment = rewrite_comment(input,
1524                                       true,
1525                                       Shape::legacy(9, Indent::new(0, 69)),
1526                                       &wrap_normalize_config).unwrap();
1527         assert_eq!(expected, comment);
1528
1529         let comment = rewrite_comment("/*   trimmed    */",
1530                                       true,
1531                                       Shape::legacy(100, Indent::new(0, 100)),
1532                                       &wrap_normalize_config).unwrap();
1533         assert_eq!("/* trimmed */", comment);
1534
1535         // check that different comment style are properly recognised
1536         let comment = rewrite_comment(r#"/// test1
1537                                          /// test2
1538                                          /*
1539                                           * test3
1540                                           */"#,
1541                                       false,
1542                                       Shape::legacy(100, Indent::new(0, 0)),
1543                                       &wrap_normalize_config).unwrap();
1544         assert_eq!("/// test1\n/// test2\n// test3", comment);
1545
1546         // check that the blank line marks the end of a commented paragraph
1547         let comment = rewrite_comment(r#"// test1
1548
1549                                          // test2"#,
1550                                       false,
1551                                       Shape::legacy(100, Indent::new(0, 0)),
1552                                       &wrap_normalize_config).unwrap();
1553         assert_eq!("// test1\n\n// test2", comment);
1554
1555         // check that the blank line marks the end of a custom-commented paragraph
1556         let comment = rewrite_comment(r#"//@ test1
1557
1558                                          //@ test2"#,
1559                                       false,
1560                                       Shape::legacy(100, Indent::new(0, 0)),
1561                                       &wrap_normalize_config).unwrap();
1562         assert_eq!("//@ test1\n\n//@ test2", comment);
1563
1564         // check that bare lines are just indented but left unchanged otherwise
1565         let comment = rewrite_comment(r#"// test1
1566                                          /*
1567                                            a bare line!
1568
1569                                                 another bare line!
1570                                           */"#,
1571                                       false,
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);
1575     }
1576
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),
1583                 _ => None,
1584             })
1585             .collect()
1586     }
1587
1588     #[test]
1589     fn test_uncommented() {
1590         assert_eq!(&uncommented("abc/*...*/"), "abc");
1591         assert_eq!(
1592             &uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
1593             "..ac\n"
1594         );
1595         assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
1596     }
1597
1598     #[test]
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);
1604     }
1605
1606     #[test]
1607     fn test_find_uncommented() {
1608         fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1609             assert_eq!(expected, haystack.find_uncommented(needle));
1610         }
1611
1612         check("/*/ */test", "test", Some(6));
1613         check("//test\ntest", "test", Some(7));
1614         check("/* comment only */", "whatever", None);
1615         check(
1616             "/* comment */ some text /* more commentary */ result",
1617             "result",
1618             Some(46),
1619         );
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));
1632     }
1633
1634     #[test]
1635     fn test_remove_trailing_white_spaces() {
1636         let s = "    r#\"\n        test\n    \"#";
1637         assert_eq!(remove_trailing_white_spaces(&s), s);
1638     }
1639
1640     #[test]
1641     fn test_filter_normal_code() {
1642         let s = r#"
1643 fn main() {
1644     println!("hello, world");
1645 }
1646 "#;
1647         assert_eq!(s, filter_normal_code(s));
1648         let s_with_comment = r#"
1649 fn main() {
1650     // hello, world
1651     println!("hello, world");
1652 }
1653 "#;
1654         assert_eq!(s, filter_normal_code(s_with_comment));
1655     }
1656 }