Skip to content

Commit bce26d5

Browse files
authored
Merge pull request #1134 from sinkuu/issue977
Fix #977
2 parents 35624c5 + 130c593 commit bce26d5

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

src/items.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,26 @@ impl<'a> FmtVisitor<'a> {
8585
let snippet = self.snippet(span);
8686
let brace_pos = snippet.find_uncommented("{").unwrap();
8787

88-
if fm.items.is_empty() && !contains_comment(&snippet[brace_pos..]) {
89-
self.buffer.push_str("{");
90-
} else {
88+
self.buffer.push_str("{");
89+
if !fm.items.is_empty() || contains_comment(&snippet[brace_pos..]) {
9190
// FIXME: this skips comments between the extern keyword and the opening
9291
// brace.
93-
self.last_pos = span.lo + BytePos(brace_pos as u32);
92+
self.last_pos = span.lo + BytePos(brace_pos as u32 + 1);
9493
self.block_indent = self.block_indent.block_indent(self.config);
9594

96-
for item in &fm.items {
97-
self.format_foreign_item(&*item);
98-
}
95+
if fm.items.is_empty() {
96+
self.format_missing_no_indent(span.hi - BytePos(1));
97+
self.block_indent = self.block_indent.block_unindent(self.config);
98+
99+
self.buffer.push_str(&self.block_indent.to_string(self.config));
100+
} else {
101+
for item in &fm.items {
102+
self.format_foreign_item(&*item);
103+
}
99104

100-
self.block_indent = self.block_indent.block_unindent(self.config);
101-
self.format_missing_with_indent(span.hi - BytePos(1));
105+
self.block_indent = self.block_indent.block_unindent(self.config);
106+
self.format_missing_with_indent(span.hi - BytePos(1));
107+
}
102108
}
103109

104110
self.buffer.push_str("}");
@@ -299,7 +305,8 @@ impl<'a> FmtVisitor<'a> {
299305
self.buffer.push_str(&format_header("enum ", ident, vis));
300306

301307
let enum_snippet = self.snippet(span);
302-
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
308+
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
309+
let body_start = span.lo + BytePos(brace_pos as u32 + 1);
303310
let generics_str = format_generics(&self.get_context(),
304311
generics,
305312
"{",
@@ -318,11 +325,17 @@ impl<'a> FmtVisitor<'a> {
318325
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
319326
match variant_list {
320327
Some(ref body_str) => self.buffer.push_str(&body_str),
321-
None => self.format_missing(span.hi - BytePos(1)),
328+
None => {
329+
if contains_comment(&enum_snippet[brace_pos..]) {
330+
self.format_missing_no_indent(span.hi - BytePos(1))
331+
} else {
332+
self.format_missing(span.hi - BytePos(1))
333+
}
334+
}
322335
}
323336
self.block_indent = self.block_indent.block_unindent(self.config);
324337

325-
if variant_list.is_some() {
338+
if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
326339
self.buffer.push_str(&self.block_indent.to_string(self.config));
327340
}
328341
self.buffer.push_str("}");

src/missed_spans.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ impl<'a> FmtVisitor<'a> {
3838
})
3939
}
4040

41+
pub fn format_missing_no_indent(&mut self, end: BytePos) {
42+
self.format_missing_inner(end, |this, last_snippet, _| {
43+
this.buffer.push_str(last_snippet.trim_right());
44+
})
45+
}
46+
4147
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
4248
end: BytePos,
4349
process_last_snippet: F) {

tests/source/issue-977.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// FIXME(#919)
2+
3+
trait NameC { /* comment */ }
4+
struct FooC { /* comment */ }
5+
enum MooC { /* comment */ }
6+
mod BarC { /* comment */ }
7+
extern { /* comment */ }

tests/target/issue-977.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// FIXME(#919)
2+
3+
trait NameC {
4+
// comment
5+
}
6+
struct FooC { /* comment */ }
7+
enum MooC {
8+
// comment
9+
}
10+
mod BarC {
11+
// comment
12+
}
13+
extern "C" {
14+
// comment
15+
}

0 commit comments

Comments
 (0)