Merge pull request #1134 from sinkuu/issue977

Fix #977
This commit is contained in:
Nick Cameron 2016-08-29 07:07:48 +12:00 committed by GitHub
commit bce26d51bd
4 changed files with 53 additions and 12 deletions

View File

@ -85,14 +85,19 @@ pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
let snippet = self.snippet(span);
let brace_pos = snippet.find_uncommented("{").unwrap();
if fm.items.is_empty() && !contains_comment(&snippet[brace_pos..]) {
self.buffer.push_str("{");
} else {
if !fm.items.is_empty() || contains_comment(&snippet[brace_pos..]) {
// FIXME: this skips comments between the extern keyword and the opening
// brace.
self.last_pos = span.lo + BytePos(brace_pos as u32);
self.last_pos = span.lo + BytePos(brace_pos as u32 + 1);
self.block_indent = self.block_indent.block_indent(self.config);
if fm.items.is_empty() {
self.format_missing_no_indent(span.hi - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
self.buffer.push_str(&self.block_indent.to_string(self.config));
} else {
for item in &fm.items {
self.format_foreign_item(&*item);
}
@ -100,6 +105,7 @@ pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
self.block_indent = self.block_indent.block_unindent(self.config);
self.format_missing_with_indent(span.hi - BytePos(1));
}
}
self.buffer.push_str("}");
self.last_pos = span.hi;
@ -299,7 +305,8 @@ pub fn visit_enum(&mut self,
self.buffer.push_str(&format_header("enum ", ident, vis));
let enum_snippet = self.snippet(span);
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
let body_start = span.lo + BytePos(brace_pos as u32 + 1);
let generics_str = format_generics(&self.get_context(),
generics,
"{",
@ -318,11 +325,17 @@ pub fn visit_enum(&mut self,
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
match variant_list {
Some(ref body_str) => self.buffer.push_str(&body_str),
None => self.format_missing(span.hi - BytePos(1)),
None => {
if contains_comment(&enum_snippet[brace_pos..]) {
self.format_missing_no_indent(span.hi - BytePos(1))
} else {
self.format_missing(span.hi - BytePos(1))
}
}
}
self.block_indent = self.block_indent.block_unindent(self.config);
if variant_list.is_some() {
if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
self.buffer.push_str(&self.block_indent.to_string(self.config));
}
self.buffer.push_str("}");

View File

@ -38,6 +38,12 @@ pub fn format_missing_with_indent(&mut self, end: BytePos) {
})
}
pub fn format_missing_no_indent(&mut self, end: BytePos) {
self.format_missing_inner(end, |this, last_snippet, _| {
this.buffer.push_str(last_snippet.trim_right());
})
}
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
end: BytePos,
process_last_snippet: F) {

View File

@ -0,0 +1,7 @@
// FIXME(#919)
trait NameC { /* comment */ }
struct FooC { /* comment */ }
enum MooC { /* comment */ }
mod BarC { /* comment */ }
extern { /* comment */ }

15
tests/target/issue-977.rs Normal file
View File

@ -0,0 +1,15 @@
// FIXME(#919)
trait NameC {
// comment
}
struct FooC { /* comment */ }
enum MooC {
// comment
}
mod BarC {
// comment
}
extern "C" {
// comment
}