Skip to content

Commit

Permalink
Implement initial option for brace style for non-fn items.
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Nov 15, 2015
1 parent cdf56f7 commit 8658774
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Expand Up @@ -302,4 +302,5 @@ create_config! {
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
wrap_comments: bool, false, "Break comments to fit on the line";
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
}
20 changes: 18 additions & 2 deletions src/items.rs
Expand Up @@ -634,10 +634,17 @@ impl<'a> FmtVisitor<'a> {
let header_str = self.format_header("enum ", ident, vis);
self.buffer.push_str(&header_str);

let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
!enum_def.variants.is_empty() {
format!("\n{}", self.block_indent.to_string(self.config))
} else {
" ".to_owned()
};
let enum_snippet = self.snippet(span);
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
let generics_str = self.format_generics(generics,
"{",
&separator,
"{",
self.block_indent,
self.block_indent.block_indent(self.config),
Expand Down Expand Up @@ -813,16 +820,24 @@ impl<'a> FmtVisitor<'a> {

let body_lo = span_after(span, "{", self.codemap);

let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
!fields.is_empty() {
format!("\n{}", self.block_indent.to_string(self.config))
} else {
" ".to_owned()
};

let generics_str = match generics {
Some(g) => {
try_opt!(self.format_generics(g,
"{",
&separator,
"{",
offset,
offset + header_str.len(),
mk_sp(span.lo, body_lo)))
}
None => " {".to_owned(),
None => format!("{}{{", separator),
};
result.push_str(&generics_str);

Expand Down Expand Up @@ -954,6 +969,7 @@ impl<'a> FmtVisitor<'a> {
fn format_generics(&self,
generics: &ast::Generics,
opener: &str,
separator: &str,
terminator: &str,
offset: Indent,
generics_offset: Indent,
Expand All @@ -973,7 +989,7 @@ impl<'a> FmtVisitor<'a> {
result.push_str(&self.block_indent.to_string(self.config));
result.push_str(opener);
} else {
result.push(' ');
result.push_str(separator);
result.push_str(opener);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/source/item-brace-style.rs
@@ -0,0 +1,16 @@
// rustfmt-item_brace_style: AlwaysNextLine

mod M {
enum A {
A,
}

struct B {
b: i32,
}

// For empty enums and structs, the brace remains on the same line.
enum C {}

struct D {}
}
18 changes: 18 additions & 0 deletions tests/target/item-brace-style.rs
@@ -0,0 +1,18 @@
// rustfmt-item_brace_style: AlwaysNextLine

mod M {
enum A
{
A,
}

struct B
{
b: i32,
}

// For empty enums and structs, the brace remains on the same line.
enum C {}

struct D {}
}

0 comments on commit 8658774

Please sign in to comment.