2015-04-21 16:47:15 +12:00
|
|
|
// 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.
|
|
|
|
|
2017-09-15 18:11:24 +09:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2017-09-17 15:23:25 +09:00
|
|
|
use syntax::codemap::{BytePos, Pos, Span};
|
|
|
|
|
2017-07-13 18:42:14 +09:00
|
|
|
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
|
|
|
|
use config::WriteMode;
|
2017-09-17 15:23:25 +09:00
|
|
|
use shape::{Indent, Shape};
|
2017-06-06 06:54:22 +02:00
|
|
|
use utils::mk_sp;
|
2017-07-13 18:42:14 +09:00
|
|
|
use visitor::FmtVisitor;
|
2015-04-21 16:47:15 +12:00
|
|
|
|
|
|
|
impl<'a> FmtVisitor<'a> {
|
2016-08-24 21:32:04 +01:00
|
|
|
fn output_at_start(&self) -> bool {
|
|
|
|
self.buffer.len == 0
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:47:15 +12:00
|
|
|
// 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) {
|
2017-06-03 22:50:44 +09:00
|
|
|
self.format_missing_inner(end, |this, last_snippet, _| {
|
|
|
|
this.buffer.push_str(last_snippet)
|
|
|
|
})
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2015-09-19 10:44:28 -07:00
|
|
|
pub fn format_missing_with_indent(&mut self, end: BytePos) {
|
|
|
|
let config = self.config;
|
2015-10-08 23:07:19 +02:00
|
|
|
self.format_missing_inner(end, |this, last_snippet, snippet| {
|
|
|
|
this.buffer.push_str(last_snippet.trim_right());
|
2016-08-24 21:32:04 +01:00
|
|
|
if last_snippet == snippet && !this.output_at_start() {
|
2015-10-08 23:07:19 +02:00
|
|
|
// No new lines in the snippet.
|
|
|
|
this.buffer.push_str("\n");
|
|
|
|
}
|
|
|
|
let indent = this.block_indent.to_string(config);
|
|
|
|
this.buffer.push_str(&indent);
|
|
|
|
})
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2016-08-23 21:00:43 +09:00
|
|
|
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());
|
|
|
|
})
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(
|
|
|
|
&mut self,
|
|
|
|
end: BytePos,
|
|
|
|
process_last_snippet: F,
|
|
|
|
) {
|
2015-04-21 16:47:15 +12:00
|
|
|
let start = self.last_pos;
|
|
|
|
|
|
|
|
if start == end {
|
2015-08-22 18:08:11 -04:00
|
|
|
// Do nothing if this is the beginning of the file.
|
2016-08-24 21:32:04 +01:00
|
|
|
if !self.output_at_start() {
|
2015-10-19 21:40:00 +02:00
|
|
|
process_last_snippet(self, "", "");
|
2015-08-22 18:08:11 -04:00
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
assert!(
|
|
|
|
start < end,
|
|
|
|
"Request to format inverted span: {:?} to {:?}",
|
|
|
|
self.codemap.lookup_char_pos(start),
|
|
|
|
self.codemap.lookup_char_pos(end)
|
|
|
|
);
|
2015-04-21 16:47:15 +12:00
|
|
|
|
|
|
|
self.last_pos = end;
|
2017-06-06 06:54:22 +02:00
|
|
|
let span = mk_sp(start, end);
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-10-19 21:41:47 +02:00
|
|
|
self.write_snippet(span, &process_last_snippet);
|
2015-04-21 20:40:36 +12:00
|
|
|
}
|
2015-04-21 16:47:15 +12:00
|
|
|
|
2015-10-19 21:41:47 +02:00
|
|
|
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
|
2017-06-12 15:58:58 +12:00
|
|
|
where
|
|
|
|
F: Fn(&mut FmtVisitor, &str, &str),
|
2015-10-19 21:41:47 +02:00
|
|
|
{
|
|
|
|
// 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.
|
2017-08-19 21:47:40 +03:00
|
|
|
let big_span_lo = self.codemap.lookup_char_pos(span.lo()).file.start_pos;
|
2015-10-19 21:41:47 +02:00
|
|
|
let local_begin = self.codemap.lookup_byte_offset(big_span_lo);
|
2017-08-19 21:47:40 +03:00
|
|
|
let local_end = self.codemap.lookup_byte_offset(span.hi());
|
2015-10-19 21:41:47 +02:00
|
|
|
let start_index = local_begin.pos.to_usize();
|
|
|
|
let end_index = local_end.pos.to_usize();
|
2017-03-28 11:14:47 +13:00
|
|
|
let big_snippet = &local_begin.fm.src.as_ref().unwrap()[start_index..end_index];
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2017-08-19 21:47:40 +03:00
|
|
|
let big_diff = (span.lo() - big_span_lo).to_usize();
|
2017-08-29 22:16:04 +09:00
|
|
|
let snippet = self.snippet(span);
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2017-01-26 15:10:47 +13:00
|
|
|
debug!("write_snippet `{}`", snippet);
|
|
|
|
|
2017-05-06 17:59:51 -04:00
|
|
|
self.write_snippet_inner(big_snippet, big_diff, &snippet, span, process_last_snippet);
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
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),
|
2015-10-19 21:41:47 +02:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2017-08-19 21:47:40 +03:00
|
|
|
let char_pos = self.codemap.lookup_char_pos(span.lo());
|
2017-05-06 17:59:51 -04:00
|
|
|
let file_name = &char_pos.file.name;
|
|
|
|
let mut cur_line = char_pos.line;
|
|
|
|
|
2017-09-15 18:11:24 +09:00
|
|
|
fn replace_chars<'a>(string: &'a str) -> Cow<'a, str> {
|
|
|
|
if string.contains(char::is_whitespace) {
|
|
|
|
Cow::from(
|
|
|
|
string
|
|
|
|
.chars()
|
|
|
|
.map(|ch| if ch.is_whitespace() { ch } else { 'X' })
|
|
|
|
.collect::<String>(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Cow::from(string)
|
|
|
|
}
|
2015-10-21 12:56:24 +05:30
|
|
|
}
|
|
|
|
|
2017-09-15 18:11:24 +09:00
|
|
|
let snippet = &*match self.config.write_mode() {
|
2016-02-05 15:59:41 -05:00
|
|
|
WriteMode::Coverage => replace_chars(old_snippet),
|
2017-09-15 18:11:24 +09:00
|
|
|
_ => Cow::from(old_snippet),
|
2015-10-21 12:56:24 +05:30
|
|
|
};
|
|
|
|
|
2015-10-19 21:41:47 +02:00
|
|
|
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
|
2017-01-26 15:10:47 +13:00
|
|
|
debug!("{:?}: {:?}", kind, subslice);
|
|
|
|
|
2015-10-19 21:41:47 +02:00
|
|
|
if let CodeCharKind::Comment = kind {
|
|
|
|
let last_char = big_snippet[..(offset + big_diff)]
|
2016-04-22 18:53:39 +12:00
|
|
|
.chars()
|
|
|
|
.rev()
|
2016-08-23 23:14:45 +09:00
|
|
|
.skip_while(|rev_c| [' ', '\t'].contains(rev_c))
|
2016-04-22 18:53:39 +12:00
|
|
|
.next();
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2016-01-07 12:13:45 +05:30
|
|
|
let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2017-05-06 17:59:51 -04:00
|
|
|
let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
|
|
|
|
|
2017-09-15 12:10:58 +09:00
|
|
|
if rewrite_next_comment
|
|
|
|
&& !self.config.file_lines().intersects_range(
|
2017-08-07 15:17:57 +09:00
|
|
|
file_name,
|
|
|
|
cur_line,
|
|
|
|
cur_line + subslice_num_lines,
|
|
|
|
) {
|
2017-05-06 17:59:51 -04:00
|
|
|
rewrite_next_comment = false;
|
|
|
|
}
|
|
|
|
|
2017-01-17 12:01:10 +13:00
|
|
|
if rewrite_next_comment {
|
|
|
|
if fix_indent {
|
|
|
|
if let Some('{') = last_char {
|
|
|
|
self.buffer.push_str("\n");
|
|
|
|
}
|
2017-06-16 08:49:49 +09:00
|
|
|
self.buffer
|
|
|
|
.push_str(&self.block_indent.to_string(self.config));
|
2017-01-17 12:01:10 +13:00
|
|
|
} else {
|
|
|
|
self.buffer.push_str(" ");
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
let comment_width = ::std::cmp::min(
|
|
|
|
self.config.comment_width(),
|
|
|
|
self.config.max_width() - self.block_indent.width(),
|
|
|
|
);
|
2017-09-05 16:51:54 +09:00
|
|
|
let comment_indent = Indent::from_width(self.config, self.buffer.cur_offset());
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2017-06-12 15:58:58 +12:00
|
|
|
self.buffer.push_str(&rewrite_comment(
|
|
|
|
subslice,
|
|
|
|
false,
|
2017-09-05 16:51:54 +09:00
|
|
|
Shape::legacy(comment_width, comment_indent),
|
2017-06-12 15:58:58 +12:00
|
|
|
self.config,
|
|
|
|
).unwrap());
|
2015-10-19 21:41:47 +02:00
|
|
|
|
|
|
|
last_wspace = None;
|
|
|
|
line_start = offset + subslice.len();
|
|
|
|
|
2017-08-29 22:16:04 +09:00
|
|
|
if let Some('/') = subslice.chars().nth(1) {
|
2016-10-24 14:45:15 -05:00
|
|
|
// check that there are no contained block comments
|
2017-06-16 08:49:49 +09:00
|
|
|
if !subslice
|
|
|
|
.split('\n')
|
|
|
|
.map(|s| s.trim_left())
|
|
|
|
.any(|s| s.len() >= 2 && &s[0..2] == "/*")
|
2017-06-12 15:58:58 +12:00
|
|
|
{
|
2016-10-24 14:45:15 -05:00
|
|
|
// Add a newline after line comments
|
|
|
|
self.buffer.push_str("\n");
|
|
|
|
}
|
2016-01-27 02:18:05 +01:00
|
|
|
} else if line_start <= snippet.len() {
|
2015-11-20 23:31:05 +01:00
|
|
|
// For other comments add a newline if there isn't one at the end already
|
2016-01-27 02:18:05 +01:00
|
|
|
match snippet[line_start..].chars().next() {
|
|
|
|
Some('\n') | Some('\r') => (),
|
|
|
|
_ => self.buffer.push_str("\n"),
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 17:59:51 -04:00
|
|
|
cur_line += subslice_num_lines;
|
2015-10-19 21:41:47 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
rewrite_next_comment = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mut i, c) in subslice.char_indices() {
|
|
|
|
i += offset;
|
|
|
|
|
|
|
|
if c == '\n' {
|
2017-05-16 15:47:09 +07:00
|
|
|
if !self.config.file_lines().contains_line(file_name, cur_line) {
|
2017-05-06 17:59:51 -04:00
|
|
|
last_wspace = None;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:41:47 +02:00
|
|
|
if let Some(lw) = last_wspace {
|
|
|
|
self.buffer.push_str(&snippet[line_start..lw]);
|
|
|
|
self.buffer.push_str("\n");
|
|
|
|
} else {
|
2017-03-28 11:25:59 +13:00
|
|
|
self.buffer.push_str(&snippet[line_start..i + 1]);
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 17:59:51 -04:00
|
|
|
cur_line += 1;
|
2015-10-19 21:41:47 +02:00
|
|
|
line_start = i + 1;
|
|
|
|
last_wspace = None;
|
|
|
|
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
|
2016-08-23 23:14:45 +09:00
|
|
|
} else if c.is_whitespace() {
|
|
|
|
if last_wspace.is_none() {
|
|
|
|
last_wspace = Some(i);
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
2016-10-27 02:40:08 -05:00
|
|
|
} else if c == ';' {
|
|
|
|
if last_wspace.is_some() {
|
|
|
|
line_start = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
|
|
|
|
last_wspace = None;
|
2016-08-23 23:14:45 +09:00
|
|
|
} else {
|
|
|
|
rewrite_next_comment = rewrite_next_comment || kind == CodeCharKind::Normal;
|
|
|
|
last_wspace = None;
|
2015-10-19 21:41:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 15:10:47 +13:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-04-21 20:40:36 +12:00
|
|
|
}
|
2015-10-19 21:41:47 +02:00
|
|
|
|
2016-08-23 23:14:45 +09:00
|
|
|
process_last_snippet(self, &snippet[line_start..], snippet);
|
2015-04-21 16:47:15 +12:00
|
|
|
}
|
|
|
|
}
|