Remove empty lines at the beginning of the file

This commit is contained in:
topecongiro 2017-11-10 17:09:31 +09:00
parent 6ce42823cb
commit 05798572bb
2 changed files with 16 additions and 0 deletions

View File

@ -321,6 +321,7 @@ where
let filemap = visitor.codemap.lookup_char_pos(module.inner.lo()).file;
// Format inner attributes if available.
if !krate.attrs.is_empty() && path == main_file {
visitor.skip_empty_lines(filemap.end_pos);
if visitor.visit_attrs(&krate.attrs, ast::AttrStyle::Inner) {
visitor.push_rewrite(module.inner, None);
} else {
@ -328,6 +329,7 @@ where
}
} else {
visitor.last_pos = filemap.start_pos;
visitor.skip_empty_lines(filemap.end_pos);
visitor.format_separate_mod(module, &*filemap);
};

View File

@ -699,6 +699,20 @@ impl<'a> FmtVisitor<'a> {
self.format_missing_with_indent(filemap.end_pos);
}
pub fn skip_empty_lines(&mut self, end_pos: BytePos) {
while let Some(pos) = self.codemap
.opt_span_after(mk_sp(self.last_pos, end_pos), "\n")
{
if let Some(snippet) = self.opt_snippet(mk_sp(self.last_pos, pos)) {
if snippet.trim().is_empty() {
self.last_pos = pos;
} else {
return;
}
}
}
}
pub fn get_context(&self) -> RewriteContext {
RewriteContext {
parse_session: self.parse_session,