mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Fix #977
This commit is contained in:
parent
d022f05f34
commit
130c593544
37
src/items.rs
37
src/items.rs
@ -85,20 +85,26 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
let snippet = self.snippet(span);
|
let snippet = self.snippet(span);
|
||||||
let brace_pos = snippet.find_uncommented("{").unwrap();
|
let brace_pos = snippet.find_uncommented("{").unwrap();
|
||||||
|
|
||||||
if fm.items.is_empty() && !contains_comment(&snippet[brace_pos..]) {
|
self.buffer.push_str("{");
|
||||||
self.buffer.push_str("{");
|
if !fm.items.is_empty() || contains_comment(&snippet[brace_pos..]) {
|
||||||
} else {
|
|
||||||
// FIXME: this skips comments between the extern keyword and the opening
|
// FIXME: this skips comments between the extern keyword and the opening
|
||||||
// brace.
|
// 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);
|
self.block_indent = self.block_indent.block_indent(self.config);
|
||||||
|
|
||||||
for item in &fm.items {
|
if fm.items.is_empty() {
|
||||||
self.format_foreign_item(&*item);
|
self.format_missing_no_indent(span.hi - BytePos(1));
|
||||||
}
|
self.block_indent = self.block_indent.block_unindent(self.config);
|
||||||
|
|
||||||
self.block_indent = self.block_indent.block_unindent(self.config);
|
self.buffer.push_str(&self.block_indent.to_string(self.config));
|
||||||
self.format_missing_with_indent(span.hi - BytePos(1));
|
} 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.buffer.push_str("}");
|
self.buffer.push_str("}");
|
||||||
@ -299,7 +305,8 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
self.buffer.push_str(&format_header("enum ", ident, vis));
|
self.buffer.push_str(&format_header("enum ", ident, vis));
|
||||||
|
|
||||||
let enum_snippet = self.snippet(span);
|
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(),
|
let generics_str = format_generics(&self.get_context(),
|
||||||
generics,
|
generics,
|
||||||
"{",
|
"{",
|
||||||
@ -318,11 +325,17 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
|
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
|
||||||
match variant_list {
|
match variant_list {
|
||||||
Some(ref body_str) => self.buffer.push_str(&body_str),
|
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);
|
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(&self.block_indent.to_string(self.config));
|
||||||
}
|
}
|
||||||
self.buffer.push_str("}");
|
self.buffer.push_str("}");
|
||||||
|
@ -34,6 +34,12 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
fn format_missing_inner<F: Fn(&mut FmtVisitor, &str, &str)>(&mut self,
|
||||||
end: BytePos,
|
end: BytePos,
|
||||||
process_last_snippet: F) {
|
process_last_snippet: F) {
|
||||||
|
7
tests/source/issue-977.rs
Normal file
7
tests/source/issue-977.rs
Normal 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
15
tests/target/issue-977.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// FIXME(#919)
|
||||||
|
|
||||||
|
trait NameC {
|
||||||
|
// comment
|
||||||
|
}
|
||||||
|
struct FooC { /* comment */ }
|
||||||
|
enum MooC {
|
||||||
|
// comment
|
||||||
|
}
|
||||||
|
mod BarC {
|
||||||
|
// comment
|
||||||
|
}
|
||||||
|
extern "C" {
|
||||||
|
// comment
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user