Format inline attributes on out-of-line modules (#996)

Fixes #838
This commit is contained in:
Nick Cameron 2016-05-19 08:36:59 +12:00 committed by Marcus Klaas de Vries
parent 78f8b6c2e3
commit 9589cac62d
3 changed files with 26 additions and 4 deletions

View File

@ -185,20 +185,41 @@ impl<'a> FmtVisitor<'a> {
}
fn visit_item(&mut self, item: &ast::Item) {
// Only look at attributes for modules (except for rustfmt_skip) if the
// module is inline. We want to avoid looking at attributes in another
// file, which the AST doesn't distinguish.
// This is where we bail out if there is a skip attribute. This is only
// complex in the module case. It is complex because the module could be
// in a seperate file and there might be attributes in both files, but
// the AST lumps them all together.
match item.node {
ast::ItemKind::Mod(ref m) => {
let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
if outer_file.name == inner_file.name {
// Module is inline, in this case we treat modules like any
// other item.
if self.visit_attrs(&item.attrs) {
self.push_rewrite(item.span, None);
return;
}
} else if utils::contains_skip(&item.attrs) {
// Module is not inline, but should be skipped.
return;
} else {
// Module is not inline and should not be skipped. We want
// to process only the attributes in the current file.
let attrs = item.attrs
.iter()
.filter_map(|a| {
let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
if attr_file.name == outer_file.name {
Some(a.clone())
} else {
None
}
})
.collect::<Vec<_>>();
// Assert because if we should skip it should be caught by
// the above case.
assert!(!self.visit_attrs(&attrs));
}
}
_ => {

View File

@ -1,4 +1,4 @@
// Some nested mods
mod nestedmod ;
#[cfg(test)] mod nestedmod ;
pub mod no_new_line_beginning;

View File

@ -1,4 +1,5 @@
// Some nested mods
#[cfg(test)]
mod nestedmod;
pub mod no_new_line_beginning;