// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use Shape;
+use std::borrow::Cow;
+
+use syntax::source_map::{BytePos, Pos, Span};
+
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
-use config::WriteMode;
-use syntax::codemap::{BytePos, Pos, Span};
-use utils::mk_sp;
+use config::{EmitMode, FileName};
+use shape::{Indent, Shape};
+use source_map::LineRangeUtils;
+use utils::{count_newlines, last_line_width, mk_sp};
use visitor::FmtVisitor;
+struct SnippetStatus {
+ /// An offset to the current line from the beginnig of the original snippet.
+ line_start: usize,
+ /// A length of trailing whitespaces on the current line.
+ last_wspace: Option<usize>,
+ /// The current line number.
+ cur_line: usize,
+}
+
+impl SnippetStatus {
+ fn new(cur_line: usize) -> Self {
+ SnippetStatus {
+ line_start: 0,
+ last_wspace: None,
+ cur_line,
+ }
+ }
+}
+
impl<'a> FmtVisitor<'a> {
fn output_at_start(&self) -> bool {
- self.buffer.len == 0
+ 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) {
- self.format_missing_inner(end, |this, last_snippet, _| {
- this.buffer.push_str(last_snippet)
- })
+ // HACK(topecongiro)
+ // We use `format_missing()` to extract a missing comment between a macro
+ // (or alike) 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) {
let config = self.config;
self.format_missing_inner(end, |this, last_snippet, snippet| {
- this.buffer.push_str(last_snippet.trim_right());
+ this.push_str(last_snippet.trim_right());
if last_snippet == snippet && !this.output_at_start() {
// No new lines in the snippet.
- this.buffer.push_str("\n");
+ this.push_str("\n");
}
let indent = this.block_indent.to_string(config);
- this.buffer.push_str(&indent);
+ this.push_str(&indent);
})
}
pub fn format_missing_no_indent(&mut self, end: BytePos) {
self.format_missing_inner(end, |this, last_snippet, _| {
- this.buffer.push_str(last_snippet.trim_right());
+ this.push_str(last_snippet.trim_right());
})
}
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;
let span = mk_sp(start, end);
+ let snippet = self.snippet(span);
+
+ // Do nothing for spaces in the beginning of the file
+ if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
+ return;
+ }
- self.write_snippet(span, &process_last_snippet);
+ if snippet.trim().is_empty() && !out_of_file_lines_range!(self, span) {
+ // Keep vertical spaces within range.
+ self.push_vertical_spaces(count_newlines(snippet));
+ process_last_snippet(self, "", snippet);
+ } else {
+ self.write_snippet(span, &process_last_snippet);
+ }
+ }
+
+ fn push_vertical_spaces(&mut self, mut newline_count: usize) {
+ let offset = self.count_trailing_newlines();
+ let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
+ let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
+
+ if newline_count + offset > newline_upper_bound {
+ if offset >= newline_upper_bound {
+ newline_count = 0;
+ } else {
+ newline_count = newline_upper_bound - offset;
+ }
+ } else if newline_count + offset < newline_lower_bound {
+ if offset >= newline_lower_bound {
+ newline_count = 0;
+ } else {
+ newline_count = newline_lower_bound - offset;
+ }
+ }
+
+ 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)
// 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_diff = (span.lo - big_span_lo).to_usize();
- let snippet = self.snippet(span.clone());
+ let big_diff = (span.lo() - big_span_lo).to_usize();
+ let snippet = self.snippet(span);
debug!("write_snippet `{}`", snippet);
- self.write_snippet_inner(big_snippet, big_diff, &snippet, span, process_last_snippet);
+ self.write_snippet_inner(big_snippet, big_diff, snippet, span, process_last_snippet);
}
fn write_snippet_inner<F>(
// 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 mut line_start = 0;
- let mut last_wspace = None;
- let mut rewrite_next_comment = true;
-
- let char_pos = self.codemap.lookup_char_pos(span.lo);
- let file_name = &char_pos.file.name;
- let mut cur_line = char_pos.line;
-
- fn replace_chars(string: &str) -> String {
- string
- .chars()
- .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
- .collect()
- }
+ 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 replaced = match self.config.write_mode() {
- WriteMode::Coverage => replace_chars(old_snippet),
- _ => old_snippet.to_owned(),
+ let snippet = &*match self.config.emit_mode() {
+ EmitMode::Coverage => Cow::from(replace_chars(old_snippet)),
+ _ => Cow::from(old_snippet),
};
- let snippet = &*replaced;
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
debug!("{:?}: {:?}", kind, subslice);
- if let CodeCharKind::Comment = kind {
- let last_char = big_snippet[..(offset + big_diff)]
- .chars()
- .rev()
- .skip_while(|rev_c| [' ', '\t'].contains(rev_c))
- .next();
+ let newline_count = count_newlines(subslice);
+ let within_file_lines_range = self.config.file_lines().contains_range(
+ file_name,
+ status.cur_line,
+ status.cur_line + newline_count,
+ );
+
+ if CodeCharKind::Comment == kind && within_file_lines_range {
+ // 1: comment.
+ self.process_comment(
+ &mut status,
+ snippet,
+ &big_snippet[..(offset + big_diff)],
+ offset,
+ subslice,
+ );
+ } else if subslice.trim().is_empty() && newline_count > 0 && within_file_lines_range {
+ // 2: blank lines.
+ self.push_vertical_spaces(newline_count);
+ status.cur_line += newline_count;
+ status.line_start = offset + newline_count;
+ } 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);
+ }
+ }
- let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
+ process_last_snippet(self, &snippet[status.line_start..], snippet);
+ }
- let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
+ fn process_comment(
+ &mut self,
+ status: &mut SnippetStatus,
+ snippet: &str,
+ big_snippet: &str,
+ offset: usize,
+ subslice: &str,
+ ) {
+ let last_char = big_snippet
+ .chars()
+ .rev()
+ .skip_while(|rev_c| [' ', '\t'].contains(rev_c))
+ .next();
- if rewrite_next_comment &&
- !self.config
- .file_lines()
- .intersects_range(file_name, cur_line, cur_line + subslice_num_lines)
- {
- rewrite_next_comment = false;
- }
+ let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
- if rewrite_next_comment {
- if fix_indent {
- if let Some('{') = last_char {
- self.buffer.push_str("\n");
- }
- self.buffer
- .push_str(&self.block_indent.to_string(self.config));
- } else {
- self.buffer.push_str(" ");
- }
-
- let comment_width = ::std::cmp::min(
- self.config.comment_width(),
- self.config.max_width() - self.block_indent.width(),
- );
-
- self.buffer.push_str(&rewrite_comment(
- subslice,
- false,
- Shape::legacy(comment_width, self.block_indent),
- self.config,
- ).unwrap());
-
- last_wspace = None;
- line_start = offset + subslice.len();
-
- if let Some('/') = subslice.chars().skip(1).next() {
- // 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 after line comments
- self.buffer.push_str("\n");
- }
- } else if line_start <= snippet.len() {
- // For other comments add a newline if there isn't one at the end already
- match snippet[line_start..].chars().next() {
- Some('\n') | Some('\r') => (),
- _ => self.buffer.push_str("\n"),
- }
- }
-
- cur_line += subslice_num_lines;
- continue;
- } else {
- rewrite_next_comment = false;
- }
+ let comment_indent = if fix_indent {
+ if let Some('{') = last_char {
+ self.push_str("\n");
+ }
+ let indent_str = self.block_indent.to_string(self.config);
+ self.push_str(&indent_str);
+ self.block_indent
+ } else {
+ self.push_str(" ");
+ Indent::from_width(self.config, last_line_width(&self.buffer))
+ };
+
+ let comment_width = ::std::cmp::min(
+ self.config.comment_width(),
+ 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);
+
+ 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 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') => (),
+ _ => self.push_str("\n"),
}
+ }
- for (mut i, c) in subslice.char_indices() {
- i += offset;
-
- if c == '\n' {
- if !self.config.file_lines().contains_line(file_name, cur_line) {
- last_wspace = None;
- }
-
- if let Some(lw) = last_wspace {
- self.buffer.push_str(&snippet[line_start..lw]);
- self.buffer.push_str("\n");
- } else {
- self.buffer.push_str(&snippet[line_start..i + 1]);
- }
-
- cur_line += 1;
- line_start = i + 1;
- last_wspace = None;
- rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
- } else if c.is_whitespace() {
- if last_wspace.is_none() {
- last_wspace = Some(i);
- }
- } else if c == ';' {
- if last_wspace.is_some() {
- line_start = i;
- }
-
- rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
- last_wspace = None;
+ status.cur_line += count_newlines(subslice);
+ }
+
+ fn process_missing_code(
+ &mut self,
+ status: &mut SnippetStatus,
+ snippet: &str,
+ subslice: &str,
+ offset: usize,
+ file_name: &FileName,
+ ) {
+ for (mut i, c) in subslice.char_indices() {
+ i += offset;
+
+ if c == '\n' {
+ let skip_this_line = !self
+ .config
+ .file_lines()
+ .contains_line(file_name, status.cur_line);
+ if skip_this_line {
+ status.last_wspace = None;
+ }
+
+ if let Some(lw) = status.last_wspace {
+ self.push_str(&snippet[status.line_start..lw]);
+ self.push_str("\n");
+ status.last_wspace = None;
} else {
- rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
- last_wspace = None;
+ self.push_str(&snippet[status.line_start..i + 1]);
}
- }
- let remaining = snippet[line_start..subslice.len() + offset].trim();
- if !remaining.is_empty() {
- self.buffer.push_str(remaining);
- line_start = subslice.len() + offset;
- rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
+ 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;
}
}
- process_last_snippet(self, &snippet[line_start..], snippet);
+ let remaining = snippet[status.line_start..subslice.len() + offset].trim();
+ if !remaining.is_empty() {
+ self.push_str(remaining);
+ status.line_start = subslice.len() + offset;
+ }
}
}
+
+fn replace_chars(string: &str) -> String {
+ string
+ .chars()
+ .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
+ .collect()
+}