]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
Use correct span for match arms with the leading pipe and attributes (#3975)
[rust.git] / src / comment.rs
index 0a3062583c58a9cffef9d439ced48503ee62ad6b..1da62d176817171b46b433b06978f6c6fc2fcd74 100644 (file)
@@ -3,7 +3,7 @@
 use std::{self, borrow::Cow, iter};
 
 use itertools::{multipeek, MultiPeek};
-use syntax::source_map::Span;
+use rustc_span::Span;
 
 use crate::config::Config;
 use crate::rewrite::RewriteContext;
@@ -25,7 +25,7 @@ fn is_custom_comment(comment: &str) -> bool {
 }
 
 #[derive(Copy, Clone, PartialEq, Eq)]
-pub enum CommentStyle<'a> {
+pub(crate) enum CommentStyle<'a> {
     DoubleSlash,
     TripleSlash,
     Doc,
@@ -45,7 +45,7 @@ fn custom_opener(s: &str) -> &str {
 
 impl<'a> CommentStyle<'a> {
     /// Returns `true` if the commenting style covers a line only.
-    pub fn is_line_comment(&self) -> bool {
+    pub(crate) fn is_line_comment(&self) -> bool {
         match *self {
             CommentStyle::DoubleSlash
             | CommentStyle::TripleSlash
@@ -56,7 +56,7 @@ pub fn is_line_comment(&self) -> bool {
     }
 
     /// Returns `true` if the commenting style can span over multiple lines.
-    pub fn is_block_comment(&self) -> bool {
+    pub(crate) fn is_block_comment(&self) -> bool {
         match *self {
             CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
                 true
@@ -66,14 +66,14 @@ pub fn is_block_comment(&self) -> bool {
     }
 
     /// Returns `true` if the commenting style is for documentation.
-    pub fn is_doc_comment(&self) -> bool {
+    pub(crate) fn is_doc_comment(&self) -> bool {
         match *self {
             CommentStyle::TripleSlash | CommentStyle::Doc => true,
             _ => false,
         }
     }
 
-    pub fn opener(&self) -> &'a str {
+    pub(crate) fn opener(&self) -> &'a str {
         match *self {
             CommentStyle::DoubleSlash => "// ",
             CommentStyle::TripleSlash => "/// ",
@@ -85,34 +85,36 @@ pub fn opener(&self) -> &'a str {
         }
     }
 
-    pub fn closer(&self) -> &'a str {
+    pub(crate) fn closer(&self) -> &'a str {
         match *self {
             CommentStyle::DoubleSlash
             | CommentStyle::TripleSlash
             | CommentStyle::Custom(..)
             | CommentStyle::Doc => "",
-            CommentStyle::DoubleBullet => " **/",
-            CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
+            CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
+                " */"
+            }
         }
     }
 
-    pub fn line_start(&self) -> &'a str {
+    pub(crate) fn line_start(&self) -> &'a str {
         match *self {
             CommentStyle::DoubleSlash => "// ",
             CommentStyle::TripleSlash => "/// ",
             CommentStyle::Doc => "//! ",
-            CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
-            CommentStyle::DoubleBullet => " ** ",
+            CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
+                " * "
+            }
             CommentStyle::Custom(opener) => opener,
         }
     }
 
-    pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
+    pub(crate) fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
         (self.opener(), self.closer(), self.line_start())
     }
 }
 
-fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
+pub(crate) fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
     if !normalize_comments {
         if orig.starts_with("/**") && !orig.starts_with("/**/") {
             CommentStyle::DoubleBullet
@@ -143,7 +145,7 @@ fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
 }
 
 /// Returns true if the last line of the passed string finishes with a block-comment.
-pub fn is_last_comment_block(s: &str) -> bool {
+pub(crate) fn is_last_comment_block(s: &str) -> bool {
     s.trim_end().ends_with("*/")
 }
 
@@ -152,7 +154,7 @@ pub fn is_last_comment_block(s: &str) -> bool {
 /// recovered. If `allow_extend` is true and there is no comment between the two
 /// strings, then they will be put on a single line as long as doing so does not
 /// exceed max width.
-pub fn combine_strs_with_missing_comments(
+pub(crate) fn combine_strs_with_missing_comments(
     context: &RewriteContext<'_>,
     prev_str: &str,
     next_str: &str,
@@ -239,11 +241,11 @@ pub fn combine_strs_with_missing_comments(
     Some(result)
 }
 
-pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
+pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
     identify_comment(orig, false, shape, config, true)
 }
 
-pub fn rewrite_comment(
+pub(crate) fn rewrite_comment(
     orig: &str,
     block_style: bool,
     shape: Shape,
@@ -527,7 +529,6 @@ fn new(
             .checked_sub(closer.len() + opener.len())
             .unwrap_or(1);
         let indent_str = shape.indent.to_string_with_newline(config).to_string();
-        let fmt_indent = shape.indent + (opener.len() - line_start.len());
 
         let mut cr = CommentRewrite {
             result: String::with_capacity(orig.len() * 2),
@@ -538,14 +539,14 @@ fn new(
             comment_line_separator: format!("{}{}", indent_str, line_start),
             max_width,
             indent_str,
-            fmt_indent,
+            fmt_indent: shape.indent,
 
             fmt: StringFormat {
                 opener: "",
                 closer: "",
                 line_start,
                 line_end: "",
-                shape: Shape::legacy(max_width, fmt_indent),
+                shape: Shape::legacy(max_width, shape.indent),
                 trim_end: true,
                 config,
             },
@@ -823,7 +824,8 @@ fn rewrite_comment_inner(
 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
 
 fn hide_sharp_behind_comment(s: &str) -> Cow<'_, str> {
-    if s.trim_start().starts_with("# ") {
+    let s_trimmed = s.trim();
+    if s_trimmed.starts_with("# ") || s_trimmed == "#" {
         Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
     } else {
         Cow::from(s)
@@ -852,14 +854,16 @@ fn has_url(s: &str) -> bool {
 
 /// Given the span, rewrite the missing comment inside it if available.
 /// Note that the given span must only include comments (or leading/trailing whitespaces).
-pub fn rewrite_missing_comment(
+pub(crate) fn rewrite_missing_comment(
     span: Span,
     shape: Shape,
     context: &RewriteContext<'_>,
 ) -> Option<String> {
     let missing_snippet = context.snippet(span);
     let trimmed_snippet = missing_snippet.trim();
-    if !trimmed_snippet.is_empty() {
+    // check the span starts with a comment
+    let pos = trimmed_snippet.find('/');
+    if !trimmed_snippet.is_empty() && pos.is_some() {
         rewrite_comment(trimmed_snippet, false, shape, context.config)
     } else {
         Some(String::new())
@@ -869,7 +873,7 @@ pub fn rewrite_missing_comment(
 /// Recover the missing comments in the specified span, if available.
 /// The layout of the comments will be preserved as long as it does not break the code
 /// and its total width does not exceed the max width.
-pub fn recover_missing_comment_in_span(
+pub(crate) fn recover_missing_comment_in_span(
     span: Span,
     shape: Shape,
     context: &RewriteContext<'_>,
@@ -880,7 +884,7 @@ pub fn recover_missing_comment_in_span(
         Some(String::new())
     } else {
         let missing_snippet = context.snippet(span);
-        let pos = missing_snippet.find('/').unwrap_or(0);
+        let pos = missing_snippet.find('/')?;
         // 1 = ` `
         let total_width = missing_comment.len() + used_width + 1;
         let force_new_line_before_comment =
@@ -971,7 +975,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
     }
 }
 
-pub trait FindUncommented {
+pub(crate) trait FindUncommented {
     fn find_uncommented(&self, pat: &str) -> Option<usize>;
 }
 
@@ -1004,7 +1008,7 @@ fn find_uncommented(&self, pat: &str) -> Option<usize> {
 // is expected to be prefixed by a comment, including delimiters.
 // Good: `/* /* inner */ outer */ code();`
 // Bad:  `code(); // hello\n world!`
-pub fn find_comment_end(s: &str) -> Option<usize> {
+pub(crate) fn find_comment_end(s: &str) -> Option<usize> {
     let mut iter = CharClasses::new(s.char_indices());
     for (kind, (i, _c)) in &mut iter {
         if kind == FullCodeCharKind::Normal || kind == FullCodeCharKind::InString {
@@ -1021,11 +1025,11 @@ pub fn find_comment_end(s: &str) -> Option<usize> {
 }
 
 /// Returns `true` if text contains any comment.
-pub fn contains_comment(text: &str) -> bool {
+pub(crate) fn contains_comment(text: &str) -> bool {
     CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
 }
 
-pub struct CharClasses<T>
+pub(crate) struct CharClasses<T>
 where
     T: Iterator,
     T::Item: RichChar,
@@ -1034,7 +1038,7 @@ pub struct CharClasses<T>
     status: CharClassesStatus,
 }
 
-pub trait RichChar {
+pub(crate) trait RichChar {
     fn get_char(&self) -> char;
 }
 
@@ -1080,7 +1084,7 @@ enum CharClassesStatus {
 
 /// Distinguish between functional part of code and comments
 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
-pub enum CodeCharKind {
+pub(crate) enum CodeCharKind {
     Normal,
     Comment,
 }
@@ -1089,7 +1093,7 @@ pub enum CodeCharKind {
 /// describing opening and closing of comments for ease when chunking
 /// code from tagged characters
 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
-pub enum FullCodeCharKind {
+pub(crate) enum FullCodeCharKind {
     Normal,
     /// The first character of a comment, there is only one for a comment (always '/')
     StartComment,
@@ -1113,7 +1117,7 @@ pub enum FullCodeCharKind {
 }
 
 impl FullCodeCharKind {
-    pub fn is_comment(self) -> bool {
+    pub(crate) fn is_comment(self) -> bool {
         match self {
             FullCodeCharKind::StartComment
             | FullCodeCharKind::InComment
@@ -1126,7 +1130,7 @@ pub fn is_comment(self) -> bool {
     }
 
     /// Returns true if the character is inside a comment
-    pub fn inside_comment(self) -> bool {
+    pub(crate) fn inside_comment(self) -> bool {
         match self {
             FullCodeCharKind::InComment
             | FullCodeCharKind::StartStringCommented
@@ -1136,12 +1140,12 @@ pub fn inside_comment(self) -> bool {
         }
     }
 
-    pub fn is_string(self) -> bool {
+    pub(crate) fn is_string(self) -> bool {
         self == FullCodeCharKind::InString || self == FullCodeCharKind::StartString
     }
 
     /// Returns true if the character is within a commented string
-    pub fn is_commented_string(self) -> bool {
+    pub(crate) fn is_commented_string(self) -> bool {
         self == FullCodeCharKind::InStringCommented
             || self == FullCodeCharKind::StartStringCommented
     }
@@ -1160,7 +1164,7 @@ impl<T> CharClasses<T>
     T: Iterator,
     T::Item: RichChar,
 {
-    pub fn new(base: T) -> CharClasses<T> {
+    pub(crate) fn new(base: T) -> CharClasses<T> {
         CharClasses {
             base: multipeek(base),
             status: CharClassesStatus::Normal,
@@ -1343,13 +1347,13 @@ fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
 
 /// An iterator over the lines of a string, paired with the char kind at the
 /// end of the line.
-pub struct LineClasses<'a> {
+pub(crate) struct LineClasses<'a> {
     base: iter::Peekable<CharClasses<std::str::Chars<'a>>>,
     kind: FullCodeCharKind,
 }
 
 impl<'a> LineClasses<'a> {
-    pub fn new(s: &'a str) -> Self {
+    pub(crate) fn new(s: &'a str) -> Self {
         LineClasses {
             base: CharClasses::new(s.chars()).peekable(),
             kind: FullCodeCharKind::Normal,
@@ -1465,14 +1469,14 @@ fn next(&mut self) -> Option<Self::Item> {
 /// Iterator over an alternating sequence of functional and commented parts of
 /// a string. The first item is always a, possibly zero length, subslice of
 /// functional text. Line style comments contain their ending newlines.
-pub struct CommentCodeSlices<'a> {
+pub(crate) struct CommentCodeSlices<'a> {
     slice: &'a str,
     last_slice_kind: CodeCharKind,
     last_slice_end: usize,
 }
 
 impl<'a> CommentCodeSlices<'a> {
-    pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
+    pub(crate) fn new(slice: &'a str) -> CommentCodeSlices<'a> {
         CommentCodeSlices {
             slice,
             last_slice_kind: CodeCharKind::Comment,
@@ -1543,7 +1547,7 @@ fn next(&mut self) -> Option<Self::Item> {
 
 /// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
 /// (if it fits in the width/offset, else return `None`), else return `new`
-pub fn recover_comment_removed(
+pub(crate) fn recover_comment_removed(
     new: String,
     span: Span,
     context: &RewriteContext<'_>,
@@ -1553,10 +1557,10 @@ pub fn recover_comment_removed(
         // We missed some comments. Warn and keep the original text.
         if context.config.error_on_unformatted() {
             context.report.append(
-                context.source_map.span_to_filename(span).into(),
+                context.parse_sess.span_to_filename(span),
                 vec![FormattingError::from_span(
                     span,
-                    &context.source_map,
+                    &context.parse_sess,
                     ErrorKind::LostComment,
                 )],
             );
@@ -1567,7 +1571,7 @@ pub fn recover_comment_removed(
     }
 }
 
-pub fn filter_normal_code(code: &str) -> String {
+pub(crate) fn filter_normal_code(code: &str) -> String {
     let mut buffer = String::with_capacity(code.len());
     LineClasses::new(code).for_each(|(kind, line)| match kind {
         FullCodeCharKind::Normal