From 130c5935443dab5ff7cc0ba0fb2bf196b640f447 Mon Sep 17 00:00:00 2001 From: sinkuu Date: Tue, 23 Aug 2016 21:00:43 +0900 Subject: [PATCH] Fix #977 --- src/items.rs | 37 +++++++++++++++++++++++++------------ src/missed_spans.rs | 6 ++++++ tests/source/issue-977.rs | 7 +++++++ tests/target/issue-977.rs | 15 +++++++++++++++ 4 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 tests/source/issue-977.rs create mode 100644 tests/target/issue-977.rs diff --git a/src/items.rs b/src/items.rs index 565abae217c..55b4f8b9dcd 100644 --- a/src/items.rs +++ b/src/items.rs @@ -85,20 +85,26 @@ 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 { + self.buffer.push_str("{"); + 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); - for item in &fm.items { - self.format_foreign_item(&*item); - } + 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); + } - self.block_indent = self.block_indent.block_unindent(self.config); - self.format_missing_with_indent(span.hi - BytePos(1)); + self.block_indent = self.block_indent.block_unindent(self.config); + self.format_missing_with_indent(span.hi - BytePos(1)); + } } self.buffer.push_str("}"); @@ -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("}"); diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 7e6d1516636..5a737ebdee8 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -34,6 +34,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(&mut self, end: BytePos, process_last_snippet: F) { diff --git a/tests/source/issue-977.rs b/tests/source/issue-977.rs new file mode 100644 index 00000000000..dc2663d581b --- /dev/null +++ b/tests/source/issue-977.rs @@ -0,0 +1,7 @@ +// FIXME(#919) + +trait NameC { /* comment */ } +struct FooC { /* comment */ } +enum MooC { /* comment */ } +mod BarC { /* comment */ } +extern { /* comment */ } diff --git a/tests/target/issue-977.rs b/tests/target/issue-977.rs new file mode 100644 index 00000000000..ad7c2bd4e5c --- /dev/null +++ b/tests/target/issue-977.rs @@ -0,0 +1,15 @@ +// FIXME(#919) + +trait NameC { + // comment +} +struct FooC { /* comment */ } +enum MooC { + // comment +} +mod BarC { + // comment +} +extern "C" { + // comment +} -- 2.44.0