]> git.lizzy.rs Git - rust.git/blob - src/comment.rs
recognize strings inside comments in order to avoid indenting them
[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, trim_left_preserve_layout};
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     /// Returns true if the commenting style is for documentation.
77     pub fn is_doc_comment(&self) -> bool {
78         match *self {
79             CommentStyle::TripleSlash | CommentStyle::Doc => true,
80             _ => false,
81         }
82     }
83
84     pub fn opener(&self) -> &'a str {
85         match *self {
86             CommentStyle::DoubleSlash => "// ",
87             CommentStyle::TripleSlash => "/// ",
88             CommentStyle::Doc => "//! ",
89             CommentStyle::SingleBullet => "/* ",
90             CommentStyle::DoubleBullet => "/** ",
91             CommentStyle::Exclamation => "/*! ",
92             CommentStyle::Custom(opener) => opener,
93         }
94     }
95
96     pub fn closer(&self) -> &'a str {
97         match *self {
98             CommentStyle::DoubleSlash
99             | CommentStyle::TripleSlash
100             | CommentStyle::Custom(..)
101             | CommentStyle::Doc => "",
102             CommentStyle::DoubleBullet => " **/",
103             CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
104         }
105     }
106
107     pub fn line_start(&self) -> &'a str {
108         match *self {
109             CommentStyle::DoubleSlash => "// ",
110             CommentStyle::TripleSlash => "/// ",
111             CommentStyle::Doc => "//! ",
112             CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
113             CommentStyle::DoubleBullet => " ** ",
114             CommentStyle::Custom(opener) => opener,
115         }
116     }
117
118     pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
119         (self.opener(), self.closer(), self.line_start())
120     }
121 }
122
123 fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
124     if !normalize_comments {
125         if orig.starts_with("/**") && !orig.starts_with("/**/") {
126             CommentStyle::DoubleBullet
127         } else if orig.starts_with("/*!") {
128             CommentStyle::Exclamation
129         } else if orig.starts_with("/*") {
130             CommentStyle::SingleBullet
131         } else if orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/') {
132             CommentStyle::TripleSlash
133         } else if orig.starts_with("//!") {
134             CommentStyle::Doc
135         } else if is_custom_comment(orig) {
136             CommentStyle::Custom(custom_opener(orig))
137         } else {
138             CommentStyle::DoubleSlash
139         }
140     } else if (orig.starts_with("///") && orig.chars().nth(3).map_or(true, |c| c != '/'))
141         || (orig.starts_with("/**") && !orig.starts_with("/**/"))
142     {
143         CommentStyle::TripleSlash
144     } else if orig.starts_with("//!") || orig.starts_with("/*!") {
145         CommentStyle::Doc
146     } else if is_custom_comment(orig) {
147         CommentStyle::Custom(custom_opener(orig))
148     } else {
149         CommentStyle::DoubleSlash
150     }
151 }
152
153 /// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
154 /// comments between two strings. If there are such comments, then that will be
155 /// recovered. If `allow_extend` is true and there is no comment between the two
156 /// strings, then they will be put on a single line as long as doing so does not
157 /// exceed max width.
158 pub fn combine_strs_with_missing_comments(
159     context: &RewriteContext,
160     prev_str: &str,
161     next_str: &str,
162     span: Span,
163     shape: Shape,
164     allow_extend: bool,
165 ) -> Option<String> {
166     trace!(
167         "combine_strs_with_missing_comments `{}` `{}` {:?} {:?}",
168         prev_str,
169         next_str,
170         span,
171         shape
172     );
173
174     let mut result =
175         String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
176     result.push_str(prev_str);
177     let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
178     let first_sep = if prev_str.is_empty() || next_str.is_empty() {
179         ""
180     } else {
181         " "
182     };
183     let mut one_line_width =
184         last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
185
186     let config = context.config;
187     let indent = shape.indent;
188     let missing_comment = rewrite_missing_comment(span, shape, context)?;
189
190     if missing_comment.is_empty() {
191         if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
192             result.push_str(first_sep);
193         } else if !prev_str.is_empty() {
194             result.push_str(&indent.to_string_with_newline(config))
195         }
196         result.push_str(next_str);
197         return Some(result);
198     }
199
200     // We have a missing comment between the first expression and the second expression.
201
202     // Peek the the original source code and find out whether there is a newline between the first
203     // expression and the second expression or the missing comment. We will preserve the original
204     // layout whenever possible.
205     let original_snippet = context.snippet(span);
206     let prefer_same_line = if let Some(pos) = original_snippet.find('/') {
207         !original_snippet[..pos].contains('\n')
208     } else {
209         !original_snippet.contains('\n')
210     };
211
212     one_line_width -= first_sep.len();
213     let first_sep = if prev_str.is_empty() || missing_comment.is_empty() {
214         Cow::from("")
215     } else {
216         let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
217         if prefer_same_line && one_line_width <= shape.width {
218             Cow::from(" ")
219         } else {
220             indent.to_string_with_newline(config)
221         }
222     };
223     result.push_str(&first_sep);
224     result.push_str(&missing_comment);
225
226     let second_sep = if missing_comment.is_empty() || next_str.is_empty() {
227         Cow::from("")
228     } else if missing_comment.starts_with("//") {
229         indent.to_string_with_newline(config)
230     } else {
231         one_line_width += missing_comment.len() + first_sep.len() + 1;
232         allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
233         if prefer_same_line && allow_one_line && one_line_width <= shape.width {
234             Cow::from(" ")
235         } else {
236             indent.to_string_with_newline(config)
237         }
238     };
239     result.push_str(&second_sep);
240     result.push_str(next_str);
241
242     Some(result)
243 }
244
245 pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
246     identify_comment(orig, false, shape, config, true)
247 }
248
249 pub fn rewrite_comment(
250     orig: &str,
251     block_style: bool,
252     shape: Shape,
253     config: &Config,
254 ) -> Option<String> {
255     identify_comment(orig, block_style, shape, config, false)
256 }
257
258 fn identify_comment(
259     orig: &str,
260     block_style: bool,
261     shape: Shape,
262     config: &Config,
263     is_doc_comment: bool,
264 ) -> Option<String> {
265     let style = comment_style(orig, false);
266
267     // Computes the len of line taking into account a newline if the line is part of a paragraph.
268     fn compute_len(orig: &str, line: &str) -> usize {
269         if orig.len() > line.len() {
270             if orig.as_bytes()[line.len()] == b'\r' {
271                 line.len() + 2
272             } else {
273                 line.len() + 1
274             }
275         } else {
276             line.len()
277         }
278     }
279
280     // Get the first group of line comments having the same commenting style.
281     //
282     // Returns a tuple with:
283     // - a boolean indicating if there is a blank line
284     // - a number indicating the size of the first group of comments
285     fn consume_same_line_comments(
286         style: CommentStyle,
287         orig: &str,
288         line_start: &str,
289     ) -> (bool, usize) {
290         let mut first_group_ending = 0;
291         let mut hbl = false;
292
293         for line in orig.lines() {
294             let trimmed_line = line.trim_start();
295             if trimmed_line.is_empty() {
296                 hbl = true;
297                 break;
298             } else if trimmed_line.starts_with(line_start)
299                 || comment_style(trimmed_line, false) == style
300             {
301                 first_group_ending += compute_len(&orig[first_group_ending..], line);
302             } else {
303                 break;
304             }
305         }
306         (hbl, first_group_ending)
307     }
308
309     let (has_bare_lines, first_group_ending) = match style {
310         CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
311             let line_start = style.line_start().trim_start();
312             consume_same_line_comments(style, orig, line_start)
313         }
314         CommentStyle::Custom(opener) => {
315             let trimmed_opener = opener.trim_end();
316             consume_same_line_comments(style, orig, trimmed_opener)
317         }
318         // for a block comment, search for the closing symbol
319         CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
320             let closer = style.closer().trim_start();
321             let mut closing_symbol_offset = 0;
322             let mut hbl = false;
323             let mut first = true;
324             for line in orig.lines() {
325                 closing_symbol_offset += compute_len(&orig[closing_symbol_offset..], line);
326                 let mut trimmed_line = line.trim_start();
327                 if !trimmed_line.starts_with('*')
328                     && !trimmed_line.starts_with("//")
329                     && !trimmed_line.starts_with("/*")
330                 {
331                     hbl = true;
332                 }
333
334                 // Remove opener from consideration when searching for closer
335                 if first {
336                     let opener = style.opener().trim_end();
337                     trimmed_line = &trimmed_line[opener.len()..];
338                     first = false;
339                 }
340                 if trimmed_line.ends_with(closer) {
341                     break;
342                 }
343             }
344             (hbl, closing_symbol_offset)
345         }
346     };
347
348     let (first_group, rest) = orig.split_at(first_group_ending);
349     let rewritten_first_group =
350         if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
351             trim_left_preserve_layout(first_group, shape.indent, config)?
352         } else if !config.normalize_comments()
353             && !config.wrap_comments()
354             && !config.format_doc_comments()
355         {
356             light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
357         } else {
358             rewrite_comment_inner(
359                 first_group,
360                 block_style,
361                 style,
362                 shape,
363                 config,
364                 is_doc_comment || style.is_doc_comment(),
365             )?
366         };
367     if rest.is_empty() {
368         Some(rewritten_first_group)
369     } else {
370         identify_comment(
371             rest.trim_start(),
372             block_style,
373             shape,
374             config,
375             is_doc_comment,
376         )
377         .map(|rest_str| {
378             format!(
379                 "{}\n{}{}{}",
380                 rewritten_first_group,
381                 // insert back the blank line
382                 if has_bare_lines && style.is_line_comment() {
383                     "\n"
384                 } else {
385                     ""
386                 },
387                 shape.indent.to_string(config),
388                 rest_str
389             )
390         })
391     }
392 }
393
394 /// Attributes for code blocks in rustdoc.
395 /// See https://doc.rust-lang.org/rustdoc/print.html#attributes
396 enum CodeBlockAttribute {
397     Rust,
398     Ignore,
399     Text,
400     ShouldPanic,
401     NoRun,
402     CompileFail,
403 }
404
405 impl CodeBlockAttribute {
406     fn new(attribute: &str) -> CodeBlockAttribute {
407         match attribute {
408             "rust" | "" => CodeBlockAttribute::Rust,
409             "ignore" => CodeBlockAttribute::Ignore,
410             "text" => CodeBlockAttribute::Text,
411             "should_panic" => CodeBlockAttribute::ShouldPanic,
412             "no_run" => CodeBlockAttribute::NoRun,
413             "compile_fail" => CodeBlockAttribute::CompileFail,
414             _ => CodeBlockAttribute::Text,
415         }
416     }
417 }
418
419 /// Block that is formatted as an item.
420 ///
421 /// An item starts with either a star `*` or a dash `-`. Different level of indentation are
422 /// handled by shrinking the shape accordingly.
423 struct ItemizedBlock {
424     /// the number of whitespaces up to the item sigil
425     indent: usize,
426     /// the string that marks the start of an item
427     opener: String,
428     /// sequence of whitespaces to prefix new lines that are part of the item
429     line_start: String,
430 }
431
432 impl ItemizedBlock {
433     /// Returns true if the line is formatted as an item
434     fn is_itemized_line(line: &str) -> bool {
435         let trimmed = line.trim_start();
436         trimmed.starts_with("* ") || trimmed.starts_with("- ")
437     }
438
439     /// Creates a new ItemizedBlock described with the given line.
440     /// The `is_itemized_line` needs to be called first.
441     fn new(line: &str) -> ItemizedBlock {
442         let space_to_sigil = line.chars().take_while(|c| c.is_whitespace()).count();
443         let indent = space_to_sigil + 2;
444         ItemizedBlock {
445             indent,
446             opener: line[..indent].to_string(),
447             line_start: " ".repeat(indent),
448         }
449     }
450
451     /// Returns a `StringFormat` used for formatting the content of an item
452     fn create_string_format<'a>(&'a self, fmt: &'a StringFormat) -> StringFormat<'a> {
453         StringFormat {
454             opener: "",
455             closer: "",
456             line_start: "",
457             line_end: "",
458             shape: Shape::legacy(fmt.shape.width.saturating_sub(self.indent), Indent::empty()),
459             trim_end: true,
460             config: fmt.config,
461         }
462     }
463
464     /// Returns true if the line is part of the current itemized block
465     fn in_block(&self, line: &str) -> bool {
466         !ItemizedBlock::is_itemized_line(line)
467             && self.indent <= line.chars().take_while(|c| c.is_whitespace()).count()
468     }
469 }
470
471 struct CommentRewrite<'a> {
472     result: String,
473     code_block_buffer: String,
474     is_prev_line_multi_line: bool,
475     code_block_attr: Option<CodeBlockAttribute>,
476     item_block_buffer: String,
477     item_block: Option<ItemizedBlock>,
478     comment_line_separator: String,
479     indent_str: String,
480     max_chars: usize,
481     fmt_indent: Indent,
482     fmt: StringFormat<'a>,
483
484     opener: String,
485     closer: String,
486     line_start: String,
487 }
488
489 impl<'a> CommentRewrite<'a> {
490     fn new(
491         orig: &'a str,
492         block_style: bool,
493         shape: Shape,
494         config: &'a Config,
495     ) -> CommentRewrite<'a> {
496         let (opener, closer, line_start) = if block_style {
497             CommentStyle::SingleBullet.to_str_tuplet()
498         } else {
499             comment_style(orig, config.normalize_comments()).to_str_tuplet()
500         };
501
502         let max_chars = shape
503             .width
504             .checked_sub(closer.len() + opener.len())
505             .unwrap_or(1);
506         let indent_str = shape.indent.to_string_with_newline(config).to_string();
507         let fmt_indent = shape.indent + (opener.len() - line_start.len());
508
509         let mut cr = CommentRewrite {
510             result: String::with_capacity(orig.len() * 2),
511             code_block_buffer: String::with_capacity(128),
512             is_prev_line_multi_line: false,
513             code_block_attr: None,
514             item_block_buffer: String::with_capacity(128),
515             item_block: None,
516             comment_line_separator: format!("{}{}", indent_str, line_start),
517             max_chars,
518             indent_str,
519             fmt_indent,
520
521             fmt: StringFormat {
522                 opener: "",
523                 closer: "",
524                 line_start,
525                 line_end: "",
526                 shape: Shape::legacy(max_chars, fmt_indent),
527                 trim_end: true,
528                 config,
529             },
530
531             opener: opener.to_owned(),
532             closer: closer.to_owned(),
533             line_start: line_start.to_owned(),
534         };
535         cr.result.push_str(opener);
536         cr
537     }
538
539     fn join_block(s: &str, sep: &str) -> String {
540         let mut result = String::with_capacity(s.len() + 128);
541         let mut iter = s.lines().peekable();
542         while let Some(line) = iter.next() {
543             result.push_str(line);
544             result.push_str(match iter.peek() {
545                 Some(next_line) if next_line.is_empty() => sep.trim_end(),
546                 Some(..) => &sep,
547                 None => "",
548             });
549         }
550         result
551     }
552
553     fn finish(mut self) -> String {
554         if !self.code_block_buffer.is_empty() {
555             // There is a code block that is not properly enclosed by backticks.
556             // We will leave them untouched.
557             self.result.push_str(&self.comment_line_separator);
558             self.result.push_str(&Self::join_block(
559                 &trim_custom_comment_prefix(&self.code_block_buffer),
560                 &self.comment_line_separator,
561             ));
562         }
563
564         if !self.item_block_buffer.is_empty() {
565             // the last few lines are part of an itemized block
566             self.fmt.shape = Shape::legacy(self.max_chars, self.fmt_indent);
567             let mut ib = None;
568             ::std::mem::swap(&mut ib, &mut self.item_block);
569             let ib = ib.unwrap();
570             let item_fmt = ib.create_string_format(&self.fmt);
571             self.result.push_str(&self.comment_line_separator);
572             self.result.push_str(&ib.opener);
573             match rewrite_string(
574                 &self.item_block_buffer.replace("\n", " "),
575                 &item_fmt,
576                 self.max_chars.saturating_sub(ib.indent),
577             ) {
578                 Some(s) => self.result.push_str(&Self::join_block(
579                     &s,
580                     &format!("{}{}", &self.comment_line_separator, ib.line_start),
581                 )),
582                 None => self.result.push_str(&Self::join_block(
583                     &self.item_block_buffer,
584                     &self.comment_line_separator,
585                 )),
586             };
587         }
588
589         self.result.push_str(&self.closer);
590         if self.result.ends_with(&self.opener) && self.opener.ends_with(' ') {
591             // Trailing space.
592             self.result.pop();
593         }
594
595         self.result
596     }
597
598     fn handle_line(
599         &mut self,
600         orig: &'a str,
601         i: usize,
602         line: &'a str,
603         has_leading_whitespace: bool,
604     ) -> bool {
605         let is_last = i == count_newlines(orig);
606
607         if let Some(ref ib) = self.item_block {
608             if ib.in_block(&line) {
609                 self.item_block_buffer.push_str(line.trim_start());
610                 self.item_block_buffer.push('\n');
611                 return false;
612             }
613             self.is_prev_line_multi_line = false;
614             self.fmt.shape = Shape::legacy(self.max_chars, self.fmt_indent);
615             let item_fmt = ib.create_string_format(&self.fmt);
616             self.result.push_str(&self.comment_line_separator);
617             self.result.push_str(&ib.opener);
618             match rewrite_string(
619                 &self.item_block_buffer.replace("\n", " "),
620                 &item_fmt,
621                 self.max_chars.saturating_sub(ib.indent),
622             ) {
623                 Some(s) => self.result.push_str(&Self::join_block(
624                     &s,
625                     &format!("{}{}", &self.comment_line_separator, ib.line_start),
626                 )),
627                 None => self.result.push_str(&Self::join_block(
628                     &self.item_block_buffer,
629                     &self.comment_line_separator,
630                 )),
631             };
632             self.item_block_buffer.clear();
633         } else if self.code_block_attr.is_some() {
634             if line.starts_with("```") {
635                 let code_block = match self.code_block_attr.as_ref().unwrap() {
636                     CodeBlockAttribute::Ignore | CodeBlockAttribute::Text => {
637                         trim_custom_comment_prefix(&self.code_block_buffer)
638                     }
639                     _ if self.code_block_buffer.is_empty() => String::new(),
640                     _ => {
641                         let mut config = self.fmt.config.clone();
642                         config.set().wrap_comments(false);
643                         match ::format_code_block(&self.code_block_buffer, &config) {
644                             Some(ref s) => trim_custom_comment_prefix(&s.snippet),
645                             None => trim_custom_comment_prefix(&self.code_block_buffer),
646                         }
647                     }
648                 };
649                 if !code_block.is_empty() {
650                     self.result.push_str(&self.comment_line_separator);
651                     self.result
652                         .push_str(&Self::join_block(&code_block, &self.comment_line_separator));
653                 }
654                 self.code_block_buffer.clear();
655                 self.result.push_str(&self.comment_line_separator);
656                 self.result.push_str(line);
657                 self.code_block_attr = None;
658             } else {
659                 self.code_block_buffer
660                     .push_str(&hide_sharp_behind_comment(line));
661                 self.code_block_buffer.push('\n');
662             }
663             return false;
664         }
665
666         self.code_block_attr = None;
667         self.item_block = None;
668         if line.starts_with("```") {
669             self.code_block_attr = Some(CodeBlockAttribute::new(&line[3..]))
670         } else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(&line) {
671             let ib = ItemizedBlock::new(&line);
672             self.item_block_buffer.push_str(&line[ib.indent..]);
673             self.item_block_buffer.push('\n');
674             self.item_block = Some(ib);
675             return false;
676         }
677
678         if self.result == self.opener {
679             let force_leading_whitespace = &self.opener == "/* " && count_newlines(orig) == 0;
680             if !has_leading_whitespace && !force_leading_whitespace && self.result.ends_with(' ') {
681                 self.result.pop();
682             }
683             if line.is_empty() {
684                 return false;
685             }
686         } else if self.is_prev_line_multi_line && !line.is_empty() {
687             self.result.push(' ')
688         } else if is_last && line.is_empty() {
689             // trailing blank lines are unwanted
690             if !self.closer.is_empty() {
691                 self.result.push_str(&self.indent_str);
692             }
693             return true;
694         } else {
695             self.result.push_str(&self.comment_line_separator);
696             if !has_leading_whitespace && self.result.ends_with(' ') {
697                 self.result.pop();
698             }
699         }
700
701         if self.fmt.config.wrap_comments() && line.len() > self.fmt.shape.width && !has_url(line) {
702             match rewrite_string(line, &self.fmt, self.max_chars) {
703                 Some(ref s) => {
704                     self.is_prev_line_multi_line = s.contains('\n');
705                     self.result.push_str(s);
706                 }
707                 None if self.is_prev_line_multi_line => {
708                     // We failed to put the current `line` next to the previous `line`.
709                     // Remove the trailing space, then start rewrite on the next line.
710                     self.result.pop();
711                     self.result.push_str(&self.comment_line_separator);
712                     self.fmt.shape = Shape::legacy(self.max_chars, self.fmt_indent);
713                     match rewrite_string(line, &self.fmt, self.max_chars) {
714                         Some(ref s) => {
715                             self.is_prev_line_multi_line = s.contains('\n');
716                             self.result.push_str(s);
717                         }
718                         None => {
719                             self.is_prev_line_multi_line = false;
720                             self.result.push_str(line);
721                         }
722                     }
723                 }
724                 None => {
725                     self.is_prev_line_multi_line = false;
726                     self.result.push_str(line);
727                 }
728             }
729
730             self.fmt.shape = if self.is_prev_line_multi_line {
731                 // 1 = " "
732                 let offset = 1 + last_line_width(&self.result) - self.line_start.len();
733                 Shape {
734                     width: self.max_chars.saturating_sub(offset),
735                     indent: self.fmt_indent,
736                     offset: self.fmt.shape.offset + offset,
737                 }
738             } else {
739                 Shape::legacy(self.max_chars, self.fmt_indent)
740             };
741         } else {
742             if line.is_empty() && self.result.ends_with(' ') && !is_last {
743                 // Remove space if this is an empty comment or a doc comment.
744                 self.result.pop();
745             }
746             self.result.push_str(line);
747             self.fmt.shape = Shape::legacy(self.max_chars, self.fmt_indent);
748             self.is_prev_line_multi_line = false;
749         }
750
751         false
752     }
753 }
754
755 fn rewrite_comment_inner(
756     orig: &str,
757     block_style: bool,
758     style: CommentStyle,
759     shape: Shape,
760     config: &Config,
761     is_doc_comment: bool,
762 ) -> Option<String> {
763     let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);
764
765     let line_breaks = count_newlines(orig.trim_end());
766     let lines = orig
767         .lines()
768         .enumerate()
769         .map(|(i, mut line)| {
770             line = trim_end_unless_two_whitespaces(line.trim_start(), is_doc_comment);
771             // Drop old closer.
772             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
773                 line = line[..(line.len() - 2)].trim_end();
774             }
775
776             line
777         })
778         .map(|s| left_trim_comment_line(s, &style))
779         .map(|(line, has_leading_whitespace)| {
780             if orig.starts_with("/*") && line_breaks == 0 {
781                 (
782                     line.trim_start(),
783                     has_leading_whitespace || config.normalize_comments(),
784                 )
785             } else {
786                 (line, has_leading_whitespace || config.normalize_comments())
787             }
788         });
789
790     for (i, (line, has_leading_whitespace)) in lines.enumerate() {
791         if rewriter.handle_line(orig, i, line, has_leading_whitespace) {
792             break;
793         }
794     }
795
796     Some(rewriter.finish())
797 }
798
799 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
800
801 fn hide_sharp_behind_comment(s: &str) -> Cow<str> {
802     if s.trim_start().starts_with("# ") {
803         Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
804     } else {
805         Cow::from(s)
806     }
807 }
808
809 fn trim_custom_comment_prefix(s: &str) -> String {
810     s.lines()
811         .map(|line| {
812             let left_trimmed = line.trim_start();
813             if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
814                 left_trimmed.trim_start_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
815             } else {
816                 line
817             }
818         })
819         .collect::<Vec<_>>()
820         .join("\n")
821 }
822
823 /// Returns true if the given string MAY include URLs or alike.
824 fn has_url(s: &str) -> bool {
825     // This function may return false positive, but should get its job done in most cases.
826     s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
827 }
828
829 /// Given the span, rewrite the missing comment inside it if available.
830 /// Note that the given span must only include comments (or leading/trailing whitespaces).
831 pub fn rewrite_missing_comment(
832     span: Span,
833     shape: Shape,
834     context: &RewriteContext,
835 ) -> Option<String> {
836     let missing_snippet = context.snippet(span);
837     let trimmed_snippet = missing_snippet.trim();
838     if !trimmed_snippet.is_empty() {
839         rewrite_comment(trimmed_snippet, false, shape, context.config)
840     } else {
841         Some(String::new())
842     }
843 }
844
845 /// Recover the missing comments in the specified span, if available.
846 /// The layout of the comments will be preserved as long as it does not break the code
847 /// and its total width does not exceed the max width.
848 pub fn recover_missing_comment_in_span(
849     span: Span,
850     shape: Shape,
851     context: &RewriteContext,
852     used_width: usize,
853 ) -> Option<String> {
854     let missing_comment = rewrite_missing_comment(span, shape, context)?;
855     if missing_comment.is_empty() {
856         Some(String::new())
857     } else {
858         let missing_snippet = context.snippet(span);
859         let pos = missing_snippet.find('/').unwrap_or(0);
860         // 1 = ` `
861         let total_width = missing_comment.len() + used_width + 1;
862         let force_new_line_before_comment =
863             missing_snippet[..pos].contains('\n') || total_width > context.config.max_width();
864         let sep = if force_new_line_before_comment {
865             shape.indent.to_string_with_newline(context.config)
866         } else {
867             Cow::from(" ")
868         };
869         Some(format!("{}{}", sep, missing_comment))
870     }
871 }
872
873 /// Trim trailing whitespaces unless they consist of two or more whitespaces.
874 fn trim_end_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
875     if is_doc_comment && s.ends_with("  ") {
876         s
877     } else {
878         s.trim_end()
879     }
880 }
881
882 /// Trims whitespace and aligns to indent, but otherwise does not change comments.
883 fn light_rewrite_comment(
884     orig: &str,
885     offset: Indent,
886     config: &Config,
887     is_doc_comment: bool,
888 ) -> String {
889     let lines: Vec<&str> = orig
890         .lines()
891         .map(|l| {
892             // This is basically just l.trim(), but in the case that a line starts
893             // with `*` we want to leave one space before it, so it aligns with the
894             // `*` in `/*`.
895             let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
896             let left_trimmed = if let Some(fnw) = first_non_whitespace {
897                 if l.as_bytes()[fnw] == b'*' && fnw > 0 {
898                     &l[fnw - 1..]
899                 } else {
900                     &l[fnw..]
901                 }
902             } else {
903                 ""
904             };
905             // Preserve markdown's double-space line break syntax in doc comment.
906             trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
907         })
908         .collect();
909     lines.join(&format!("\n{}", offset.to_string(config)))
910 }
911
912 /// Trims comment characters and possibly a single space from the left of a string.
913 /// Does not trim all whitespace. If a single space is trimmed from the left of the string,
914 /// this function returns true.
915 fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle) -> (&'a str, bool) {
916     if line.starts_with("//! ")
917         || line.starts_with("/// ")
918         || line.starts_with("/*! ")
919         || line.starts_with("/** ")
920     {
921         (&line[4..], true)
922     } else if let CommentStyle::Custom(opener) = *style {
923         if line.starts_with(opener) {
924             (&line[opener.len()..], true)
925         } else {
926             (&line[opener.trim_end().len()..], false)
927         }
928     } else if line.starts_with("/* ")
929         || line.starts_with("// ")
930         || line.starts_with("//!")
931         || line.starts_with("///")
932         || line.starts_with("** ")
933         || line.starts_with("/*!")
934         || (line.starts_with("/**") && !line.starts_with("/**/"))
935     {
936         (&line[3..], line.chars().nth(2).unwrap() == ' ')
937     } else if line.starts_with("/*")
938         || line.starts_with("* ")
939         || line.starts_with("//")
940         || line.starts_with("**")
941     {
942         (&line[2..], line.chars().nth(1).unwrap() == ' ')
943     } else if line.starts_with('*') {
944         (&line[1..], false)
945     } else {
946         (line, line.starts_with(' '))
947     }
948 }
949
950 pub trait FindUncommented {
951     fn find_uncommented(&self, pat: &str) -> Option<usize>;
952 }
953
954 impl FindUncommented for str {
955     fn find_uncommented(&self, pat: &str) -> Option<usize> {
956         let mut needle_iter = pat.chars();
957         for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
958             match needle_iter.next() {
959                 None => {
960                     return Some(i - pat.len());
961                 }
962                 Some(c) => match kind {
963                     FullCodeCharKind::Normal | FullCodeCharKind::InString if b == c => {}
964                     _ => {
965                         needle_iter = pat.chars();
966                     }
967                 },
968             }
969         }
970
971         // Handle case where the pattern is a suffix of the search string
972         match needle_iter.next() {
973             Some(_) => None,
974             None => Some(self.len() - pat.len()),
975         }
976     }
977 }
978
979 // Returns the first byte position after the first comment. The given string
980 // is expected to be prefixed by a comment, including delimiters.
981 // Good: "/* /* inner */ outer */ code();"
982 // Bad:  "code(); // hello\n world!"
983 pub fn find_comment_end(s: &str) -> Option<usize> {
984     let mut iter = CharClasses::new(s.char_indices());
985     for (kind, (i, _c)) in &mut iter {
986         if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
987             return Some(i);
988         }
989     }
990
991     // Handle case where the comment ends at the end of s.
992     if iter.status == CharClassesStatus::Normal {
993         Some(s.len())
994     } else {
995         None
996     }
997 }
998
999 /// Returns true if text contains any comment.
1000 pub fn contains_comment(text: &str) -> bool {
1001     CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
1002 }
1003
1004 pub struct CharClasses<T>
1005 where
1006     T: Iterator,
1007     T::Item: RichChar,
1008 {
1009     base: MultiPeek<T>,
1010     status: CharClassesStatus,
1011 }
1012
1013 pub trait RichChar {
1014     fn get_char(&self) -> char;
1015 }
1016
1017 impl RichChar for char {
1018     fn get_char(&self) -> char {
1019         *self
1020     }
1021 }
1022
1023 impl RichChar for (usize, char) {
1024     fn get_char(&self) -> char {
1025         self.1
1026     }
1027 }
1028
1029 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
1030 enum CharClassesStatus {
1031     Normal,
1032     /// Character is within a string
1033     LitString,
1034     LitStringEscape,
1035     /// Character is within a raw string
1036     LitRawString(u32),
1037     RawStringPrefix(u32),
1038     RawStringSuffix(u32),
1039     LitChar,
1040     LitCharEscape,
1041     /// Character inside a block comment, with the integer indicating the nesting deepness of the
1042     /// comment
1043     BlockComment(u32),
1044     /// Character inside a block-commented string, with the integer indicating the nesting deepness
1045     /// of the comment
1046     StringInBlockComment(u32),
1047     /// Status when the '/' has been consumed, but not yet the '*', deepness is
1048     /// the new deepness (after the comment opening).
1049     BlockCommentOpening(u32),
1050     /// Status when the '*' has been consumed, but not yet the '/', deepness is
1051     /// the new deepness (after the comment closing).
1052     BlockCommentClosing(u32),
1053     /// Character is within a line comment
1054     LineComment,
1055 }
1056
1057 /// Distinguish between functional part of code and comments
1058 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
1059 pub enum CodeCharKind {
1060     Normal,
1061     Comment,
1062 }
1063
1064 /// Distinguish between functional part of code and comments,
1065 /// describing opening and closing of comments for ease when chunking
1066 /// code from tagged characters
1067 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
1068 pub enum FullCodeCharKind {
1069     Normal,
1070     /// The first character of a comment, there is only one for a comment (always '/')
1071     StartComment,
1072     /// Any character inside a comment including the second character of comment
1073     /// marks ("//", "/*")
1074     InComment,
1075     /// Last character of a comment, '\n' for a line comment, '/' for a block comment.
1076     EndComment,
1077     /// Start of a mutlitine string inside a comment
1078     StartStringCommented,
1079     /// End of a mutlitine string inside a comment
1080     EndStringCommented,
1081     /// Inside a commented string
1082     InStringCommented,
1083     /// Start of a mutlitine string
1084     StartString,
1085     /// End of a mutlitine string
1086     EndString,
1087     /// Inside a string.
1088     InString,
1089 }
1090
1091 impl FullCodeCharKind {
1092     pub fn is_comment(self) -> bool {
1093         match self {
1094             FullCodeCharKind::StartComment
1095             | FullCodeCharKind::InComment
1096             | FullCodeCharKind::EndComment
1097             | FullCodeCharKind::StartStringCommented
1098             | FullCodeCharKind::InStringCommented
1099             | FullCodeCharKind::EndStringCommented => true,
1100             _ => false,
1101         }
1102     }
1103
1104     /// Returns true if the character is inside a comment
1105     pub fn inside_comment(self) -> bool {
1106         match self {
1107             FullCodeCharKind::InComment
1108             | FullCodeCharKind::StartStringCommented
1109             | FullCodeCharKind::InStringCommented
1110             | FullCodeCharKind::EndStringCommented => true,
1111             _ => false,
1112         }
1113     }
1114
1115     pub fn is_string(self) -> bool {
1116         self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
1117     }
1118
1119     /// Returns true if the character is within a commented string
1120     pub fn is_commented_string(self) -> bool {
1121         self == FullCodeCharKind::InStringCommented
1122             || self == FullCodeCharKind::StartStringCommented
1123     }
1124
1125     fn to_codecharkind(self) -> CodeCharKind {
1126         if self.is_comment() {
1127             CodeCharKind::Comment
1128         } else {
1129             CodeCharKind::Normal
1130         }
1131     }
1132 }
1133
1134 impl<T> CharClasses<T>
1135 where
1136     T: Iterator,
1137     T::Item: RichChar,
1138 {
1139     pub fn new(base: T) -> CharClasses<T> {
1140         CharClasses {
1141             base: multipeek(base),
1142             status: CharClassesStatus::Normal,
1143         }
1144     }
1145 }
1146
1147 fn is_raw_string_suffix<T>(iter: &mut MultiPeek<T>, count: u32) -> bool
1148 where
1149     T: Iterator,
1150     T::Item: RichChar,
1151 {
1152     for _ in 0..count {
1153         match iter.peek() {
1154             Some(c) if c.get_char() == '#' => continue,
1155             _ => return false,
1156         }
1157     }
1158     true
1159 }
1160
1161 impl<T> Iterator for CharClasses<T>
1162 where
1163     T: Iterator,
1164     T::Item: RichChar,
1165 {
1166     type Item = (FullCodeCharKind, T::Item);
1167
1168     fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
1169         let item = self.base.next()?;
1170         let chr = item.get_char();
1171         let mut char_kind = FullCodeCharKind::Normal;
1172         self.status = match self.status {
1173             CharClassesStatus::LitRawString(sharps) => {
1174                 char_kind = FullCodeCharKind::InString;
1175                 match chr {
1176                     '"' => {
1177                         if sharps == 0 {
1178                             char_kind = FullCodeCharKind::Normal;
1179                             CharClassesStatus::Normal
1180                         } else if is_raw_string_suffix(&mut self.base, sharps) {
1181                             CharClassesStatus::RawStringSuffix(sharps)
1182                         } else {
1183                             CharClassesStatus::LitRawString(sharps)
1184                         }
1185                     }
1186                     _ => CharClassesStatus::LitRawString(sharps),
1187                 }
1188             }
1189             CharClassesStatus::RawStringPrefix(sharps) => {
1190                 char_kind = FullCodeCharKind::InString;
1191                 match chr {
1192                     '#' => CharClassesStatus::RawStringPrefix(sharps + 1),
1193                     '"' => CharClassesStatus::LitRawString(sharps),
1194                     _ => CharClassesStatus::Normal, // Unreachable.
1195                 }
1196             }
1197             CharClassesStatus::RawStringSuffix(sharps) => {
1198                 match chr {
1199                     '#' => {
1200                         if sharps == 1 {
1201                             CharClassesStatus::Normal
1202                         } else {
1203                             char_kind = FullCodeCharKind::InString;
1204                             CharClassesStatus::RawStringSuffix(sharps - 1)
1205                         }
1206                     }
1207                     _ => CharClassesStatus::Normal, // Unreachable
1208                 }
1209             }
1210             CharClassesStatus::LitString => {
1211                 char_kind = FullCodeCharKind::InString;
1212                 match chr {
1213                     '"' => CharClassesStatus::Normal,
1214                     '\\' => CharClassesStatus::LitStringEscape,
1215                     _ => CharClassesStatus::LitString,
1216                 }
1217             }
1218             CharClassesStatus::LitStringEscape => {
1219                 char_kind = FullCodeCharKind::InString;
1220                 CharClassesStatus::LitString
1221             }
1222             CharClassesStatus::LitChar => match chr {
1223                 '\\' => CharClassesStatus::LitCharEscape,
1224                 '\'' => CharClassesStatus::Normal,
1225                 _ => CharClassesStatus::LitChar,
1226             },
1227             CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
1228             CharClassesStatus::Normal => match chr {
1229                 'r' => match self.base.peek().map(|c| c.get_char()) {
1230                     Some('#') | Some('"') => {
1231                         char_kind = FullCodeCharKind::InString;
1232                         CharClassesStatus::RawStringPrefix(0)
1233                     }
1234                     _ => CharClassesStatus::Normal,
1235                 },
1236                 '"' => {
1237                     char_kind = FullCodeCharKind::InString;
1238                     CharClassesStatus::LitString
1239                 }
1240                 '\'' => {
1241                     // HACK: Work around mut borrow.
1242                     match self.base.peek() {
1243                         Some(next) if next.get_char() == '\\' => {
1244                             self.status = CharClassesStatus::LitChar;
1245                             return Some((char_kind, item));
1246                         }
1247                         _ => (),
1248                     }
1249
1250                     match self.base.peek() {
1251                         Some(next) if next.get_char() == '\'' => CharClassesStatus::LitChar,
1252                         _ => CharClassesStatus::Normal,
1253                     }
1254                 }
1255                 '/' => match self.base.peek() {
1256                     Some(next) if next.get_char() == '*' => {
1257                         self.status = CharClassesStatus::BlockCommentOpening(1);
1258                         return Some((FullCodeCharKind::StartComment, item));
1259                     }
1260                     Some(next) if next.get_char() == '/' => {
1261                         self.status = CharClassesStatus::LineComment;
1262                         return Some((FullCodeCharKind::StartComment, item));
1263                     }
1264                     _ => CharClassesStatus::Normal,
1265                 },
1266                 _ => CharClassesStatus::Normal,
1267             },
1268             CharClassesStatus::StringInBlockComment(deepness) => {
1269                 char_kind = FullCodeCharKind::InStringCommented;
1270                 if chr == '"' {
1271                     CharClassesStatus::BlockComment(deepness)
1272                 } else {
1273                     CharClassesStatus::StringInBlockComment(deepness)
1274                 }
1275             }
1276             CharClassesStatus::BlockComment(deepness) => {
1277                 assert_ne!(deepness, 0);
1278                 char_kind = FullCodeCharKind::InComment;
1279                 match self.base.peek() {
1280                     Some(next) if next.get_char() == '/' && chr == '*' => {
1281                         CharClassesStatus::BlockCommentClosing(deepness - 1)
1282                     }
1283                     Some(next) if next.get_char() == '*' && chr == '/' => {
1284                         CharClassesStatus::BlockCommentOpening(deepness + 1)
1285                     }
1286                     _ if chr == '"' => CharClassesStatus::StringInBlockComment(deepness),
1287                     _ => self.status,
1288                 }
1289             }
1290             CharClassesStatus::BlockCommentOpening(deepness) => {
1291                 assert_eq!(chr, '*');
1292                 self.status = CharClassesStatus::BlockComment(deepness);
1293                 return Some((FullCodeCharKind::InComment, item));
1294             }
1295             CharClassesStatus::BlockCommentClosing(deepness) => {
1296                 assert_eq!(chr, '/');
1297                 if deepness == 0 {
1298                     self.status = CharClassesStatus::Normal;
1299                     return Some((FullCodeCharKind::EndComment, item));
1300                 } else {
1301                     self.status = CharClassesStatus::BlockComment(deepness);
1302                     return Some((FullCodeCharKind::InComment, item));
1303                 }
1304             }
1305             CharClassesStatus::LineComment => match chr {
1306                 '\n' => {
1307                     self.status = CharClassesStatus::Normal;
1308                     return Some((FullCodeCharKind::EndComment, item));
1309                 }
1310                 _ => {
1311                     self.status = CharClassesStatus::LineComment;
1312                     return Some((FullCodeCharKind::InComment, item));
1313                 }
1314             },
1315         };
1316         Some((char_kind, item))
1317     }
1318 }
1319
1320 /// An iterator over the lines of a string, paired with the char kind at the
1321 /// end of the line.
1322 pub struct LineClasses<'a> {
1323     base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
1324     kind: FullCodeCharKind,
1325 }
1326
1327 impl<'a> LineClasses<'a> {
1328     pub fn new(s: &'a str) -> Self {
1329         LineClasses {
1330             base: CharClasses::new(s.chars()).peekable(),
1331             kind: FullCodeCharKind::Normal,
1332         }
1333     }
1334 }
1335
1336 impl<'a> Iterator for LineClasses<'a> {
1337     type Item = (FullCodeCharKind, String);
1338
1339     fn next(&mut self) -> Option<Self::Item> {
1340         self.base.peek()?;
1341
1342         let mut line = String::new();
1343
1344         let start_kind = match self.base.peek() {
1345             Some((kind, _)) => *kind,
1346             None => unreachable!(),
1347         };
1348
1349         while let Some((kind, c)) = self.base.next() {
1350             // needed to set the kind of the ending character on the last line
1351             self.kind = kind;
1352             if c == '\n' {
1353                 self.kind = match (start_kind, kind) {
1354                     (FullCodeCharKind::Normal, FullCodeCharKind::InString) => {
1355                         FullCodeCharKind::StartString
1356                     }
1357                     (FullCodeCharKind::InString, FullCodeCharKind::Normal) => {
1358                         FullCodeCharKind::EndString
1359                     }
1360                     (FullCodeCharKind::InComment, FullCodeCharKind::InStringCommented) => {
1361                         FullCodeCharKind::StartStringCommented
1362                     }
1363                     (FullCodeCharKind::InStringCommented, FullCodeCharKind::InComment) => {
1364                         FullCodeCharKind::EndStringCommented
1365                     }
1366                     _ => kind,
1367                 };
1368                 break;
1369             }
1370             line.push(c);
1371         }
1372
1373         // Workaround for CRLF newline.
1374         if line.ends_with('\r') {
1375             line.pop();
1376         }
1377
1378         Some((self.kind, line))
1379     }
1380 }
1381
1382 /// Iterator over functional and commented parts of a string. Any part of a string is either
1383 /// functional code, either *one* block comment, either *one* line comment. Whitespace between
1384 /// comments is functional code. Line comments contain their ending newlines.
1385 struct UngroupedCommentCodeSlices<'a> {
1386     slice: &'a str,
1387     iter: iter::Peekable<CharClasses<std::str::CharIndices<'a>>>,
1388 }
1389
1390 impl<'a> UngroupedCommentCodeSlices<'a> {
1391     fn new(code: &'a str) -> UngroupedCommentCodeSlices<'a> {
1392         UngroupedCommentCodeSlices {
1393             slice: code,
1394             iter: CharClasses::new(code.char_indices()).peekable(),
1395         }
1396     }
1397 }
1398
1399 impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1400     type Item = (CodeCharKind, usize, &'a str);
1401
1402     fn next(&mut self) -> Option<Self::Item> {
1403         let (kind, (start_idx, _)) = self.iter.next()?;
1404         match kind {
1405             FullCodeCharKind::Normal | FullCodeCharKind::InString => {
1406                 // Consume all the Normal code
1407                 while let Some(&(char_kind, _)) = self.iter.peek() {
1408                     if char_kind.is_comment() {
1409                         break;
1410                     }
1411                     let _ = self.iter.next();
1412                 }
1413             }
1414             FullCodeCharKind::StartComment => {
1415                 // Consume the whole comment
1416                 loop {
1417                     match self.iter.next() {
1418                         Some((kind, ..)) if kind.inside_comment() => continue,
1419                         _ => break,
1420                     }
1421                 }
1422             }
1423             _ => panic!(),
1424         }
1425         let slice = match self.iter.peek() {
1426             Some(&(_, (end_idx, _))) => &self.slice[start_idx..end_idx],
1427             None => &self.slice[start_idx..],
1428         };
1429         Some((
1430             if kind.is_comment() {
1431                 CodeCharKind::Comment
1432             } else {
1433                 CodeCharKind::Normal
1434             },
1435             start_idx,
1436             slice,
1437         ))
1438     }
1439 }
1440
1441 /// Iterator over an alternating sequence of functional and commented parts of
1442 /// a string. The first item is always a, possibly zero length, subslice of
1443 /// functional text. Line style comments contain their ending newlines.
1444 pub struct CommentCodeSlices<'a> {
1445     slice: &'a str,
1446     last_slice_kind: CodeCharKind,
1447     last_slice_end: usize,
1448 }
1449
1450 impl<'a> CommentCodeSlices<'a> {
1451     pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
1452         CommentCodeSlices {
1453             slice,
1454             last_slice_kind: CodeCharKind::Comment,
1455             last_slice_end: 0,
1456         }
1457     }
1458 }
1459
1460 impl<'a> Iterator for CommentCodeSlices<'a> {
1461     type Item = (CodeCharKind, usize, &'a str);
1462
1463     fn next(&mut self) -> Option<Self::Item> {
1464         if self.last_slice_end == self.slice.len() {
1465             return None;
1466         }
1467
1468         let mut sub_slice_end = self.last_slice_end;
1469         let mut first_whitespace = None;
1470         let subslice = &self.slice[self.last_slice_end..];
1471         let mut iter = CharClasses::new(subslice.char_indices());
1472
1473         for (kind, (i, c)) in &mut iter {
1474             let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal
1475                 && &subslice[..2] == "//"
1476                 && [' ', '\t'].contains(&c);
1477
1478             if is_comment_connector && first_whitespace.is_none() {
1479                 first_whitespace = Some(i);
1480             }
1481
1482             if kind.to_codecharkind() == self.last_slice_kind && !is_comment_connector {
1483                 let last_index = match first_whitespace {
1484                     Some(j) => j,
1485                     None => i,
1486                 };
1487                 sub_slice_end = self.last_slice_end + last_index;
1488                 break;
1489             }
1490
1491             if !is_comment_connector {
1492                 first_whitespace = None;
1493             }
1494         }
1495
1496         if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
1497             // This was the last subslice.
1498             sub_slice_end = match first_whitespace {
1499                 Some(i) => self.last_slice_end + i,
1500                 None => self.slice.len(),
1501             };
1502         }
1503
1504         let kind = match self.last_slice_kind {
1505             CodeCharKind::Comment => CodeCharKind::Normal,
1506             CodeCharKind::Normal => CodeCharKind::Comment,
1507         };
1508         let res = (
1509             kind,
1510             self.last_slice_end,
1511             &self.slice[self.last_slice_end..sub_slice_end],
1512         );
1513         self.last_slice_end = sub_slice_end;
1514         self.last_slice_kind = kind;
1515
1516         Some(res)
1517     }
1518 }
1519
1520 /// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1521 /// (if it fits in the width/offset, else return None), else return `new`
1522 pub fn recover_comment_removed(
1523     new: String,
1524     span: Span,
1525     context: &RewriteContext,
1526 ) -> Option<String> {
1527     let snippet = context.snippet(span);
1528     if snippet != new && changed_comment_content(snippet, &new) {
1529         // We missed some comments. Warn and keep the original text.
1530         if context.config.error_on_unformatted() {
1531             context.report.append(
1532                 context.source_map.span_to_filename(span).into(),
1533                 vec![FormattingError::from_span(
1534                     span,
1535                     &context.source_map,
1536                     ErrorKind::LostComment,
1537                 )],
1538             );
1539         }
1540         Some(snippet.to_owned())
1541     } else {
1542         Some(new)
1543     }
1544 }
1545
1546 pub fn filter_normal_code(code: &str) -> String {
1547     let mut buffer = String::with_capacity(code.len());
1548     LineClasses::new(code).for_each(|(kind, line)| match kind {
1549         FullCodeCharKind::Normal
1550         | FullCodeCharKind::StartString
1551         | FullCodeCharKind::InString
1552         | FullCodeCharKind::EndString => {
1553             buffer.push_str(&line);
1554             buffer.push('\n');
1555         }
1556         _ => (),
1557     });
1558     if !code.ends_with('\n') && buffer.ends_with('\n') {
1559         buffer.pop();
1560     }
1561     buffer
1562 }
1563
1564 /// Return true if the two strings of code have the same payload of comments.
1565 /// The payload of comments is everything in the string except:
1566 ///     - actual code (not comments)
1567 ///     - comment start/end marks
1568 ///     - whitespace
1569 ///     - '*' at the beginning of lines in block comments
1570 fn changed_comment_content(orig: &str, new: &str) -> bool {
1571     // Cannot write this as a fn since we cannot return types containing closures
1572     let code_comment_content = |code| {
1573         let slices = UngroupedCommentCodeSlices::new(code);
1574         slices
1575             .filter(|&(ref kind, _, _)| *kind == CodeCharKind::Comment)
1576             .flat_map(|(_, _, s)| CommentReducer::new(s))
1577     };
1578     let res = code_comment_content(orig).ne(code_comment_content(new));
1579     debug!(
1580         "comment::changed_comment_content: {}\norig: '{}'\nnew: '{}'\nraw_old: {}\nraw_new: {}",
1581         res,
1582         orig,
1583         new,
1584         code_comment_content(orig).collect::<String>(),
1585         code_comment_content(new).collect::<String>()
1586     );
1587     res
1588 }
1589
1590 /// Iterator over the 'payload' characters of a comment.
1591 /// It skips whitespace, comment start/end marks, and '*' at the beginning of lines.
1592 /// The comment must be one comment, ie not more than one start mark (no multiple line comments,
1593 /// for example).
1594 struct CommentReducer<'a> {
1595     is_block: bool,
1596     at_start_line: bool,
1597     iter: std::str::Chars<'a>,
1598 }
1599
1600 impl<'a> CommentReducer<'a> {
1601     fn new(comment: &'a str) -> CommentReducer<'a> {
1602         let is_block = comment.starts_with("/*");
1603         let comment = remove_comment_header(comment);
1604         CommentReducer {
1605             is_block,
1606             at_start_line: false, // There are no supplementary '*' on the first line
1607             iter: comment.chars(),
1608         }
1609     }
1610 }
1611
1612 impl<'a> Iterator for CommentReducer<'a> {
1613     type Item = char;
1614
1615     fn next(&mut self) -> Option<Self::Item> {
1616         loop {
1617             let mut c = self.iter.next()?;
1618             if self.is_block && self.at_start_line {
1619                 while c.is_whitespace() {
1620                     c = self.iter.next()?;
1621                 }
1622                 // Ignore leading '*'
1623                 if c == '*' {
1624                     c = self.iter.next()?;
1625                 }
1626             } else if c == '\n' {
1627                 self.at_start_line = true;
1628             }
1629             if !c.is_whitespace() {
1630                 return Some(c);
1631             }
1632         }
1633     }
1634 }
1635
1636 fn remove_comment_header(comment: &str) -> &str {
1637     if comment.starts_with("///") || comment.starts_with("//!") {
1638         &comment[3..]
1639     } else if comment.starts_with("//") {
1640         &comment[2..]
1641     } else if (comment.starts_with("/**") && !comment.starts_with("/**/"))
1642         || comment.starts_with("/*!")
1643     {
1644         &comment[3..comment.len() - 2]
1645     } else {
1646         assert!(
1647             comment.starts_with("/*"),
1648             format!("string '{}' is not a comment", comment)
1649         );
1650         &comment[2..comment.len() - 2]
1651     }
1652 }
1653
1654 #[cfg(test)]
1655 mod test {
1656     use super::*;
1657     use shape::{Indent, Shape};
1658
1659     #[test]
1660     fn char_classes() {
1661         let mut iter = CharClasses::new("//\n\n".chars());
1662
1663         assert_eq!((FullCodeCharKind::StartComment, '/'), iter.next().unwrap());
1664         assert_eq!((FullCodeCharKind::InComment, '/'), iter.next().unwrap());
1665         assert_eq!((FullCodeCharKind::EndComment, '\n'), iter.next().unwrap());
1666         assert_eq!((FullCodeCharKind::Normal, '\n'), iter.next().unwrap());
1667         assert_eq!(None, iter.next());
1668     }
1669
1670     #[test]
1671     fn comment_code_slices() {
1672         let input = "code(); /* test */ 1 + 1";
1673         let mut iter = CommentCodeSlices::new(input);
1674
1675         assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
1676         assert_eq!(
1677             (CodeCharKind::Comment, 8, "/* test */"),
1678             iter.next().unwrap()
1679         );
1680         assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
1681         assert_eq!(None, iter.next());
1682     }
1683
1684     #[test]
1685     fn comment_code_slices_two() {
1686         let input = "// comment\n    test();";
1687         let mut iter = CommentCodeSlices::new(input);
1688
1689         assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
1690         assert_eq!(
1691             (CodeCharKind::Comment, 0, "// comment\n"),
1692             iter.next().unwrap()
1693         );
1694         assert_eq!(
1695             (CodeCharKind::Normal, 11, "    test();"),
1696             iter.next().unwrap()
1697         );
1698         assert_eq!(None, iter.next());
1699     }
1700
1701     #[test]
1702     fn comment_code_slices_three() {
1703         let input = "1 // comment\n    // comment2\n\n";
1704         let mut iter = CommentCodeSlices::new(input);
1705
1706         assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
1707         assert_eq!(
1708             (CodeCharKind::Comment, 2, "// comment\n    // comment2\n"),
1709             iter.next().unwrap()
1710         );
1711         assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
1712         assert_eq!(None, iter.next());
1713     }
1714
1715     #[test]
1716     #[rustfmt::skip]
1717     fn format_doc_comments() {
1718         let mut wrap_normalize_config: ::config::Config = Default::default();
1719         wrap_normalize_config.set().wrap_comments(true);
1720         wrap_normalize_config.set().normalize_comments(true);
1721
1722         let mut wrap_config: ::config::Config = Default::default();
1723         wrap_config.set().wrap_comments(true);
1724
1725         let comment = rewrite_comment(" //test",
1726                                       true,
1727                                       Shape::legacy(100, Indent::new(0, 100)),
1728                                       &wrap_normalize_config).unwrap();
1729         assert_eq!("/* test */", comment);
1730
1731         let comment = rewrite_comment("// comment on a",
1732                                       false,
1733                                       Shape::legacy(10, Indent::empty()),
1734                                       &wrap_normalize_config).unwrap();
1735         assert_eq!("// comment\n// on a", comment);
1736
1737         let comment = rewrite_comment("//  A multi line comment\n             // between args.",
1738                                       false,
1739                                       Shape::legacy(60, Indent::new(0, 12)),
1740                                       &wrap_normalize_config).unwrap();
1741         assert_eq!("//  A multi line comment\n            // between args.", comment);
1742
1743         let input = "// comment";
1744         let expected =
1745             "/* comment */";
1746         let comment = rewrite_comment(input,
1747                                       true,
1748                                       Shape::legacy(9, Indent::new(0, 69)),
1749                                       &wrap_normalize_config).unwrap();
1750         assert_eq!(expected, comment);
1751
1752         let comment = rewrite_comment("/*   trimmed    */",
1753                                       true,
1754                                       Shape::legacy(100, Indent::new(0, 100)),
1755                                       &wrap_normalize_config).unwrap();
1756         assert_eq!("/* trimmed */", comment);
1757
1758         // check that different comment style are properly recognised
1759         let comment = rewrite_comment(r#"/// test1
1760                                          /// test2
1761                                          /*
1762                                           * test3
1763                                           */"#,
1764                                       false,
1765                                       Shape::legacy(100, Indent::new(0, 0)),
1766                                       &wrap_normalize_config).unwrap();
1767         assert_eq!("/// test1\n/// test2\n// test3", comment);
1768
1769         // check that the blank line marks the end of a commented paragraph
1770         let comment = rewrite_comment(r#"// test1
1771
1772                                          // test2"#,
1773                                       false,
1774                                       Shape::legacy(100, Indent::new(0, 0)),
1775                                       &wrap_normalize_config).unwrap();
1776         assert_eq!("// test1\n\n// test2", comment);
1777
1778         // check that the blank line marks the end of a custom-commented paragraph
1779         let comment = rewrite_comment(r#"//@ test1
1780
1781                                          //@ test2"#,
1782                                       false,
1783                                       Shape::legacy(100, Indent::new(0, 0)),
1784                                       &wrap_normalize_config).unwrap();
1785         assert_eq!("//@ test1\n\n//@ test2", comment);
1786
1787         // check that bare lines are just indented but left unchanged otherwise
1788         let comment = rewrite_comment(r#"// test1
1789                                          /*
1790                                            a bare line!
1791
1792                                                 another bare line!
1793                                           */"#,
1794                                       false,
1795                                       Shape::legacy(100, Indent::new(0, 0)),
1796                                       &wrap_config).unwrap();
1797         assert_eq!("// test1\n/*\n a bare line!\n\n      another bare line!\n*/", comment);
1798     }
1799
1800     // This is probably intended to be a non-test fn, but it is not used. I'm
1801     // keeping it around unless it helps us test stuff.
1802     fn uncommented(text: &str) -> String {
1803         CharClasses::new(text.chars())
1804             .filter_map(|(s, c)| match s {
1805                 FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
1806                 _ => None,
1807             })
1808             .collect()
1809     }
1810
1811     #[test]
1812     fn test_uncommented() {
1813         assert_eq!(&uncommented("abc/*...*/"), "abc");
1814         assert_eq!(
1815             &uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
1816             "..ac\n"
1817         );
1818         assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
1819     }
1820
1821     #[test]
1822     fn test_contains_comment() {
1823         assert_eq!(contains_comment("abc"), false);
1824         assert_eq!(contains_comment("abc // qsdf"), true);
1825         assert_eq!(contains_comment("abc /* kqsdf"), true);
1826         assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
1827     }
1828
1829     #[test]
1830     fn test_find_uncommented() {
1831         fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1832             assert_eq!(expected, haystack.find_uncommented(needle));
1833         }
1834
1835         check("/*/ */test", "test", Some(6));
1836         check("//test\ntest", "test", Some(7));
1837         check("/* comment only */", "whatever", None);
1838         check(
1839             "/* comment */ some text /* more commentary */ result",
1840             "result",
1841             Some(46),
1842         );
1843         check("sup // sup", "p", Some(2));
1844         check("sup", "x", None);
1845         check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
1846         check("/*sup yo? \n sup*/ sup", "p", Some(20));
1847         check("hel/*lohello*/lo", "hello", None);
1848         check("acb", "ab", None);
1849         check(",/*A*/ ", ",", Some(0));
1850         check("abc", "abc", Some(0));
1851         check("/* abc */", "abc", None);
1852         check("/**/abc/* */", "abc", Some(4));
1853         check("\"/* abc */\"", "abc", Some(4));
1854         check("\"/* abc", "abc", Some(4));
1855     }
1856
1857     #[test]
1858     fn test_filter_normal_code() {
1859         let s = r#"
1860 fn main() {
1861     println!("hello, world");
1862 }
1863 "#;
1864         assert_eq!(s, filter_normal_code(s));
1865         let s_with_comment = r#"
1866 fn main() {
1867     // hello, world
1868     println!("hello, world");
1869 }
1870 "#;
1871         assert_eq!(s, filter_normal_code(s_with_comment));
1872     }
1873 }