// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use {Indent, Shape};
+use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
use config::WriteMode;
+use syntax::codemap::{BytePos, Pos, Span};
+use utils::mk_sp;
use visitor::FmtVisitor;
-use syntax::codemap::{self, BytePos, Span, Pos};
-use comment::{CodeCharKind, CommentCodeSlices, rewrite_comment};
-use Shape;
impl<'a> FmtVisitor<'a> {
fn output_at_start(&self) -> bool {
// 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))
+ self.format_missing_inner(end, |this, last_snippet, _| {
+ this.buffer.push_str(last_snippet)
+ })
}
pub fn format_missing_with_indent(&mut self, end: BytePos) {
})
}
- fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
- end: BytePos,
- process_last_snippet: F) {
+ fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
+ &mut self,
+ end: BytePos,
+ process_last_snippet: F,
+ ) {
let start = self.last_pos;
if start == end {
return;
}
- assert!(start < end,
- "Request to format inverted span: {:?} to {:?}",
- self.codemap.lookup_char_pos(start),
- self.codemap.lookup_char_pos(end));
+ assert!(
+ start < end,
+ "Request to format inverted span: {:?} to {:?}",
+ self.codemap.lookup_char_pos(start),
+ self.codemap.lookup_char_pos(end)
+ );
self.last_pos = end;
- let span = codemap::mk_sp(start, end);
+ let span = mk_sp(start, end);
self.write_snippet(span, &process_last_snippet);
}
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
- where F: Fn(&mut FmtVisitor, &str, &str)
+ where
+ 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 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 local_end = self.codemap.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.fm.src.as_ref().unwrap()[start_index..end_index];
- let big_diff = (span.lo - big_span_lo).to_usize();
+ 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, process_last_snippet);
+ self.write_snippet_inner(big_snippet, big_diff, &snippet, span, process_last_snippet);
}
- fn write_snippet_inner<F>(&mut self,
- big_snippet: &str,
- big_diff: usize,
- old_snippet: &str,
- process_last_snippet: F)
- where F: Fn(&mut FmtVisitor, &str, &str)
+ fn write_snippet_inner<F>(
+ &mut self,
+ big_snippet: &str,
+ big_diff: usize,
+ old_snippet: &str,
+ span: Span,
+ process_last_snippet: F,
+ ) where
+ 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
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()
+ string
+ .chars()
+ .map(|ch| if ch.is_whitespace() { ch } else { 'X' })
+ .collect()
}
- let replaced = match self.config.write_mode {
+ let replaced = match self.config.write_mode() {
WriteMode::Coverage => replace_chars(old_snippet),
_ => old_snippet.to_owned(),
};
let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
+ let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
+
+ if rewrite_next_comment &&
+ !self.config.file_lines().intersects_range(
+ file_name,
+ cur_line,
+ cur_line + subslice_num_lines,
+ ) {
+ rewrite_next_comment = false;
+ }
+
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));
+ 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());
+ let comment_width = ::std::cmp::min(
+ self.config.comment_width(),
+ self.config.max_width() - self.block_indent.width(),
+ );
+ let comment_indent = Indent::from_width(self.config, self.buffer.cur_offset());
- self.buffer.push_str(&rewrite_comment(subslice,
- false,
- Shape::legacy(comment_width,
- self.block_indent),
- self.config)
- .unwrap());
+ self.buffer.push_str(&rewrite_comment(
+ subslice,
+ false,
+ Shape::legacy(comment_width, comment_indent),
+ self.config,
+ ).unwrap());
last_wspace = None;
line_start = offset + subslice.len();
- if let Some('/') = subslice.chars().skip(1).next() {
+ 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] == "/*"
- }) {
+ 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");
}
}
}
+ cur_line += subslice_num_lines;
continue;
} else {
rewrite_next_comment = false;
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");
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;