Remove trailing whitespaces in macro def

This commit is contained in:
topecongiro 2017-10-05 16:17:59 +09:00
parent 9e963b87fc
commit 106625bc5c
2 changed files with 32 additions and 3 deletions

View File

@ -505,6 +505,35 @@ pub fn contains_comment(text: &str) -> bool {
CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
}
/// Remove trailing spaces from the specified snippet. We do not remove spaces
/// inside strings or comments.
pub fn remove_trailing_white_spaces(text: &str) -> String {
let mut buffer = String::with_capacity(text.len());
let mut space_buffer = String::with_capacity(128);
for (char_kind, c) in CharClasses::new(text.chars()) {
match c {
'\n' => {
if char_kind == FullCodeCharKind::InString {
buffer.push_str(&space_buffer);
}
space_buffer.clear();
buffer.push('\n');
}
_ if c.is_whitespace() => {
space_buffer.push(c);
}
_ => {
if !space_buffer.is_empty() {
buffer.push_str(&space_buffer);
space_buffer.clear();
}
buffer.push(c);
}
}
}
buffer
}
struct CharClasses<T>
where
T: Iterator,

View File

@ -18,8 +18,8 @@ use syntax::parse::ParseSess;
use spanned::Spanned;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
FindUncommented};
use comment::{contains_comment, recover_missing_comment_in_span, remove_trailing_white_spaces,
CodeCharKind, CommentCodeSlices, FindUncommented};
use comment::rewrite_comment;
use config::{BraceStyle, Config};
use items::{format_impl, format_struct, format_struct_struct, format_trait,
@ -470,7 +470,7 @@ impl<'a> FmtVisitor<'a> {
}
ast::ItemKind::MacroDef(..) => {
// FIXME(#1539): macros 2.0
let mac_snippet = Some(self.snippet(item.span));
let mac_snippet = Some(remove_trailing_white_spaces(&self.snippet(item.span)));
self.push_rewrite(item.span, mac_snippet);
}
}