]> git.lizzy.rs Git - rust.git/blobdiff - src/missed_spans.rs
handle hard tabs when formatting trailing comments (#3836)
[rust.git] / src / missed_spans.rs
index 20cd5e6dca1d35a5c20f60ec1308d9fdccb653ce..a957d3b3e30256524c4df2e510094e98707cec27 100644 (file)
@@ -1,27 +1,17 @@
-// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-use std::borrow::Cow;
-use std::iter::repeat;
-
-use syntax::codemap::{BytePos, FileName, Pos, Span};
-
-use codemap::LineRangeUtils;
-use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
-use config::WriteMode;
-use shape::{Indent, Shape};
-use utils::{count_newlines, last_line_width, mk_sp};
-use visitor::FmtVisitor;
+use syntax::source_map::{BytePos, Pos, Span};
+
+use crate::comment::{is_last_comment_block, rewrite_comment, CodeCharKind, CommentCodeSlices};
+use crate::config::file_lines::FileLines;
+use crate::config::FileName;
+use crate::config::Version;
+use crate::coverage::transform_missing_snippet;
+use crate::shape::{Indent, Shape};
+use crate::source_map::LineRangeUtils;
+use crate::utils::{count_lf_crlf, count_newlines, last_line_width, mk_sp};
+use crate::visitor::FmtVisitor;
 
 struct SnippetStatus {
-    /// An offset to the current line from the beginnig of the original snippet.
+    /// An offset to the current line from the beginning of the original snippet.
     line_start: usize,
     /// A length of trailing whitespaces on the current line.
     last_wspace: Option<usize>,
@@ -44,16 +34,26 @@ fn output_at_start(&self) -> bool {
         self.buffer.is_empty()
     }
 
-    // TODO these format_missing methods are ugly. Refactor and add unit tests
-    // for the central whitespace stripping loop.
-    pub fn format_missing(&mut self, end: BytePos) {
+    pub(crate) fn format_missing(&mut self, end: BytePos) {
+        // HACK(topecongiro): we use `format_missing()` to extract a missing comment between
+        // a macro (or similar) and a trailing semicolon. Here we just try to avoid calling
+        // `format_missing_inner` in the common case where there is no such comment.
+        // This is a hack, ideally we should fix a possible bug in `format_missing_inner`
+        // or refactor `visit_mac` and `rewrite_macro`, but this should suffice to fix the
+        // issue (#2727).
+        let missing_snippet = self.snippet(mk_sp(self.last_pos, end));
+        if missing_snippet.trim() == ";" {
+            self.push_str(";");
+            self.last_pos = end;
+            return;
+        }
         self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
     }
 
-    pub fn format_missing_with_indent(&mut self, end: BytePos) {
+    pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) {
         let config = self.config;
         self.format_missing_inner(end, |this, last_snippet, snippet| {
-            this.push_str(last_snippet.trim_right());
+            this.push_str(last_snippet.trim_end());
             if last_snippet == snippet && !this.output_at_start() {
                 // No new lines in the snippet.
                 this.push_str("\n");
@@ -63,13 +63,13 @@ pub fn format_missing_with_indent(&mut self, end: BytePos) {
         })
     }
 
-    pub fn format_missing_no_indent(&mut self, end: BytePos) {
+    pub(crate) fn format_missing_no_indent(&mut self, end: BytePos) {
         self.format_missing_inner(end, |this, last_snippet, _| {
-            this.push_str(last_snippet.trim_right());
+            this.push_str(last_snippet.trim_end());
         })
     }
 
-    fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
+    fn format_missing_inner<F: Fn(&mut FmtVisitor<'_>, &str, &str)>(
         &mut self,
         end: BytePos,
         process_last_snippet: F,
@@ -87,8 +87,8 @@ fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
         assert!(
             start < end,
             "Request to format inverted span: {:?} to {:?}",
-            self.codemap.lookup_char_pos(start),
-            self.codemap.lookup_char_pos(end)
+            self.source_map.lookup_char_pos(start),
+            self.source_map.lookup_char_pos(end)
         );
 
         self.last_pos = end;
@@ -110,7 +110,7 @@ fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
     }
 
     fn push_vertical_spaces(&mut self, mut newline_count: usize) {
-        let offset = self.count_trailing_newlines();
+        let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();
         let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
         let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
 
@@ -128,33 +128,23 @@ fn push_vertical_spaces(&mut self, mut newline_count: usize) {
             }
         }
 
-        let blank_lines: String = repeat('\n').take(newline_count).collect();
+        let blank_lines = "\n".repeat(newline_count);
         self.push_str(&blank_lines);
     }
 
-    fn count_trailing_newlines(&self) -> usize {
-        let mut buf = &*self.buffer;
-        let mut result = 0;
-        while buf.ends_with('\n') {
-            buf = &buf[..buf.len() - 1];
-            result += 1;
-        }
-        result
-    }
-
     fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
     where
-        F: Fn(&mut FmtVisitor, &str, &str),
+        F: Fn(&mut FmtVisitor<'_>, &str, &str),
     {
         // Get a snippet from the file start to the span's hi without allocating.
         // We need it to determine what precedes the current comment. If the comment
         // follows code on the same line, we won't touch it.
-        let big_span_lo = self.codemap.lookup_char_pos(span.lo()).file.start_pos;
-        let local_begin = self.codemap.lookup_byte_offset(big_span_lo);
-        let local_end = self.codemap.lookup_byte_offset(span.hi());
+        let big_span_lo = self.source_map.lookup_char_pos(span.lo()).file.start_pos;
+        let local_begin = self.source_map.lookup_byte_offset(big_span_lo);
+        let local_end = self.source_map.lookup_byte_offset(span.hi());
         let start_index = local_begin.pos.to_usize();
         let end_index = local_end.pos.to_usize();
-        let big_snippet = &local_begin.fm.src.as_ref().unwrap()[start_index..end_index];
+        let big_snippet = &local_begin.sf.src.as_ref().unwrap()[start_index..end_index];
 
         let big_diff = (span.lo() - big_span_lo).to_usize();
         let snippet = self.snippet(span);
@@ -172,30 +162,37 @@ fn write_snippet_inner<F>(
         span: Span,
         process_last_snippet: F,
     ) where
-        F: Fn(&mut FmtVisitor, &str, &str),
+        F: Fn(&mut FmtVisitor<'_>, &str, &str),
     {
         // Trim whitespace from the right hand side of each line.
         // Annoyingly, the library functions for splitting by lines etc. are not
         // quite right, so we must do it ourselves.
-        let char_pos = self.codemap.lookup_char_pos(span.lo());
-        let file_name = &char_pos.file.name;
+        let char_pos = self.source_map.lookup_char_pos(span.lo());
+        let file_name = &char_pos.file.name.clone().into();
         let mut status = SnippetStatus::new(char_pos.line);
 
-        let snippet = &*match self.config.write_mode() {
-            WriteMode::Coverage => replace_chars(old_snippet),
-            _ => Cow::from(old_snippet),
-        };
-
+        let snippet = &*transform_missing_snippet(self.config, old_snippet);
+
+        let slice_within_file_lines_range =
+            |file_lines: FileLines, cur_line, s| -> (usize, usize, bool) {
+                let (lf_count, crlf_count) = count_lf_crlf(s);
+                let newline_count = lf_count + crlf_count;
+                let within_file_lines_range = file_lines.contains_range(
+                    file_name,
+                    cur_line,
+                    // if a newline character is at the end of the slice, then the number of
+                    // newlines needs to be decreased by 1 so that the range checked against
+                    // the file_lines is the visual range one would expect.
+                    cur_line + newline_count - if s.ends_with('\n') { 1 } else { 0 },
+                );
+                (lf_count, crlf_count, within_file_lines_range)
+            };
         for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
             debug!("{:?}: {:?}", kind, subslice);
 
-            let newline_count = count_newlines(subslice);
-            let within_file_lines_range = self.config.file_lines().intersects_range(
-                file_name,
-                status.cur_line,
-                status.cur_line + newline_count,
-            );
-
+            let (lf_count, crlf_count, within_file_lines_range) =
+                slice_within_file_lines_range(self.config.file_lines(), status.cur_line, subslice);
+            let newline_count = lf_count + crlf_count;
             if CodeCharKind::Comment == kind && within_file_lines_range {
                 // 1: comment.
                 self.process_comment(
@@ -209,14 +206,22 @@ fn write_snippet_inner<F>(
                 // 2: blank lines.
                 self.push_vertical_spaces(newline_count);
                 status.cur_line += newline_count;
-                status.line_start = offset + newline_count;
+                status.line_start = offset + lf_count + crlf_count * 2;
             } else {
                 // 3: code which we failed to format or which is not within file-lines range.
                 self.process_missing_code(&mut status, snippet, subslice, offset, file_name);
             }
         }
 
-        process_last_snippet(self, &snippet[status.line_start..], snippet);
+        let last_snippet = &snippet[status.line_start..];
+        let (_, _, within_file_lines_range) =
+            slice_within_file_lines_range(self.config.file_lines(), status.cur_line, last_snippet);
+        if within_file_lines_range {
+            process_last_snippet(self, last_snippet, snippet);
+        } else {
+            // just append what's left
+            self.push_str(last_snippet);
+        }
     }
 
     fn process_comment(
@@ -234,6 +239,7 @@ fn process_comment(
             .next();
 
         let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
+        let mut on_same_line = false;
 
         let comment_indent = if fix_indent {
             if let Some('{') = last_char {
@@ -242,6 +248,13 @@ fn process_comment(
             let indent_str = self.block_indent.to_string(self.config);
             self.push_str(&indent_str);
             self.block_indent
+        } else if self.config.version() == Version::Two && !snippet.starts_with('\n') {
+            // The comment appears on the same line as the previous formatted code.
+            // Assuming that comment is logically associated with that code, we want to keep it on
+            // the same level and avoid mixing it with possible other comment.
+            on_same_line = true;
+            self.push_str(" ");
+            self.block_indent
         } else {
             self.push_str(" ");
             Indent::from_width(self.config, last_line_width(&self.buffer))
@@ -252,27 +265,53 @@ fn process_comment(
             self.config.max_width() - self.block_indent.width(),
         );
         let comment_shape = Shape::legacy(comment_width, comment_indent);
-        let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
-            .unwrap_or_else(|| String::from(subslice));
-        self.push_str(&comment_str);
+
+        if on_same_line {
+            match subslice.find("\n") {
+                None => {
+                    self.push_str(subslice);
+                }
+                Some(offset) if offset + 1 == subslice.len() => {
+                    self.push_str(&subslice[..offset]);
+                }
+                Some(offset) => {
+                    // keep first line as is: if it were too long and wrapped, it may get mixed
+                    // with the other lines.
+                    let first_line = &subslice[..offset];
+                    self.push_str(first_line);
+                    self.push_str(&comment_indent.to_string_with_newline(self.config));
+
+                    let other_lines = &subslice[offset + 1..];
+                    let comment_str =
+                        rewrite_comment(other_lines, false, comment_shape, self.config)
+                            .unwrap_or_else(|| String::from(other_lines));
+                    self.push_str(&comment_str);
+                }
+            }
+        } else {
+            let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
+                .unwrap_or_else(|| String::from(subslice));
+            self.push_str(&comment_str);
+        }
 
         status.last_wspace = None;
         status.line_start = offset + subslice.len();
 
-        if let Some('/') = subslice.chars().nth(1) {
-            // check that there are no contained block comments
-            if !subslice
-                .split('\n')
-                .map(|s| s.trim_left())
-                .any(|s| s.len() >= 2 && &s[0..2] == "/*")
+        // Add a newline:
+        // - if there isn't one already
+        // - otherwise, only if the last line is a line comment
+        if status.line_start <= snippet.len() {
+            match snippet[status.line_start..]
+                .chars()
+                // skip trailing whitespaces
+                .skip_while(|c| *c == ' ' || *c == '\t')
+                .next()
             {
-                // Add a newline after line comments
-                self.push_str("\n");
-            }
-        } else if status.line_start <= snippet.len() {
-            // For other comments add a newline if there isn't one at the end already
-            match snippet[status.line_start..].chars().next() {
-                Some('\n') | Some('\r') => (),
+                Some('\n') | Some('\r') => {
+                    if !is_last_comment_block(subslice) {
+                        self.push_str("\n");
+                    }
+                }
                 _ => self.push_str("\n"),
             }
         }
@@ -292,7 +331,8 @@ fn process_missing_code(
             i += offset;
 
             if c == '\n' {
-                let skip_this_line = !self.config
+                let skip_this_line = !self
+                    .config
                     .file_lines()
                     .contains_line(file_name, status.cur_line);
                 if skip_this_line {
@@ -304,16 +344,13 @@ fn process_missing_code(
                     self.push_str("\n");
                     status.last_wspace = None;
                 } else {
-                    self.push_str(&snippet[status.line_start..i + 1]);
+                    self.push_str(&snippet[status.line_start..=i]);
                 }
 
                 status.cur_line += 1;
                 status.line_start = i + 1;
             } else if c.is_whitespace() && status.last_wspace.is_none() {
                 status.last_wspace = Some(i);
-            } else if c == ';' && status.last_wspace.is_some() {
-                status.line_start = i;
-                status.last_wspace = None;
             } else {
                 status.last_wspace = None;
             }
@@ -326,12 +363,3 @@ fn process_missing_code(
         }
     }
 }
-
-fn replace_chars(string: &str) -> Cow<str> {
-    Cow::from(
-        string
-            .chars()
-            .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
-            .collect::<String>(),
-    )
-}