Skip to content

Commit 9ec8880

Browse files
topecongirowada314
authored andcommitted
Remove trailing whitespaces in macro def
1 parent ee0045f commit 9ec8880

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
@@ -19,8 +19,8 @@ use syntax::parse::ParseSess;
1919
use expr::rewrite_literal;
2020
use spanned::Spanned;
2121
use codemap::{LineRangeUtils, SpanUtils};
22-
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
23-
FindUncommented};
22+
use comment::{contains_comment, recover_missing_comment_in_span, remove_trailing_white_spaces,
23+
CodeCharKind, CommentCodeSlices, FindUncommented};
2424
use comment::rewrite_comment;
2525
use config::{BraceStyle, Config};
2626
use items::{format_impl, format_struct, format_struct_struct, format_trait,
@@ -459,7 +459,7 @@ impl<'a> FmtVisitor<'a> {
459459
}
460460
ast::ItemKind::MacroDef(..) => {
461461
// FIXME(#1539): macros 2.0
462-
let mac_snippet = Some(self.snippet(item.span));
462+
let mac_snippet = Some(remove_trailing_white_spaces(&self.snippet(item.span)));
463463
self.push_rewrite(item.span, mac_snippet);
464464
}
465465
}

0 commit comments

Comments
 (0)