From 96a2f25788ebda721bdd52de9e6c178ed364a5aa Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 14 Mar 2016 18:55:55 +1300 Subject: [PATCH] Keep empty extern blocks on one line Closes #462 --- src/items.rs | 23 ++++++++++++++--------- tests/source/extern.rs | 4 ++++ tests/target/extern.rs | 2 ++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/items.rs b/src/items.rs index 1c5eaa2a631..d70f780df5a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -84,19 +84,24 @@ impl<'a> FmtVisitor<'a> { self.buffer.push_str(&::utils::format_abi(fm.abi)); let snippet = self.snippet(span); - let brace_pos = snippet.find_uncommented("{").unwrap() as u32; + let brace_pos = snippet.find_uncommented("{").unwrap(); - // FIXME: this skips comments between the extern keyword and the opening - // brace. - self.last_pos = span.lo + BytePos(brace_pos); - self.block_indent = self.block_indent.block_indent(self.config); + if fm.items.is_empty() && !contains_comment(&snippet[brace_pos..]) { + self.buffer.push_str("{"); + } else { + // FIXME: this skips comments between the extern keyword and the opening + // brace. + self.last_pos = span.lo + BytePos(brace_pos as u32); + self.block_indent = self.block_indent.block_indent(self.config); - for item in &fm.items { - self.format_foreign_item(&*item); + 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("}"); self.last_pos = span.hi; } diff --git a/tests/source/extern.rs b/tests/source/extern.rs index e33b646b726..347612dd8f1 100644 --- a/tests/source/extern.rs +++ b/tests/source/extern.rs @@ -34,3 +34,7 @@ libc::c_long; file: *mut FILE) -> *mut FILE; } + +extern { + +} diff --git a/tests/target/extern.rs b/tests/target/extern.rs index ca8c43d8b2c..e25e8b01cd4 100644 --- a/tests/target/extern.rs +++ b/tests/target/extern.rs @@ -45,3 +45,5 @@ extern "C" { file: *mut FILE) -> *mut FILE; } + +extern "C" {}