Skip to content

Commit 106625b

Browse files
committed
Remove trailing whitespaces in macro def
1 parent 9e963b8 commit 106625b

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/comment.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,35 @@ pub fn contains_comment(text: &str) -> bool {
505505
CharClasses::new(text.chars()).any(|(kind, _)| kind.is_comment())
506506
}
507507

508+
/// Remove trailing spaces from the specified snippet. We do not remove spaces
509+
/// inside strings or comments.
510+
pub fn remove_trailing_white_spaces(text: &str) -> String {
511+
let mut buffer = String::with_capacity(text.len());
512+
let mut space_buffer = String::with_capacity(128);
513+
for (char_kind, c) in CharClasses::new(text.chars()) {
514+
match c {
515+
'\n' => {
516+
if char_kind == FullCodeCharKind::InString {
517+
buffer.push_str(&space_buffer);
518+
}
519+
space_buffer.clear();
520+
buffer.push('\n');
521+
}
522+
_ if c.is_whitespace() => {
523+
space_buffer.push(c);
524+
}
525+
_ => {
526+
if !space_buffer.is_empty() {
527+
buffer.push_str(&space_buffer);
528+
space_buffer.clear();
529+
}
530+
buffer.push(c);
531+
}
532+
}
533+
}
534+
buffer
535+
}
536+
508537
struct CharClasses<T>
509538
where
510539
T: Iterator,

src/visitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use syntax::parse::ParseSess;
1818

1919
use spanned::Spanned;
2020
use codemap::{LineRangeUtils, SpanUtils};
21-
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
22-
FindUncommented};
21+
use comment::{contains_comment, recover_missing_comment_in_span, remove_trailing_white_spaces,
22+
CodeCharKind, CommentCodeSlices, FindUncommented};
2323
use comment::rewrite_comment;
2424
use config::{BraceStyle, Config};
2525
use items::{format_impl, format_struct, format_struct_struct, format_trait,
@@ -470,7 +470,7 @@ impl<'a> FmtVisitor<'a> {
470470
}
471471
ast::ItemKind::MacroDef(..) => {
472472
// FIXME(#1539): macros 2.0
473-
let mac_snippet = Some(self.snippet(item.span));
473+
let mac_snippet = Some(remove_trailing_white_spaces(&self.snippet(item.span)));
474474
self.push_rewrite(item.span, mac_snippet);
475475
}
476476
}

0 commit comments

Comments
 (0)