Skip to content

Commit c240f10

Browse files
blesson3calebcartwright
authored andcommitted
BraceStyle AlwaysNewLine for extern blocks
* extern blocks can now get an opening brace on a new line * add test case for extern blocks
1 parent 22c4437 commit c240f10

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

Diff for: src/formatting/items.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,28 @@ impl<'a> FmtVisitor<'a> {
322322
let snippet = self.snippet(item.span);
323323
let brace_pos = snippet.find_uncommented("{").unwrap();
324324

325-
self.push_str("{");
326325
if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
326+
// configure opening brace style
327+
let brace_style = self.config.brace_style();
328+
match brace_style {
329+
BraceStyle::AlwaysNextLine => {
330+
// ensure we don't allow a space at the end so the line doesn't end with a space
331+
// (caused by &item.abi above => 'extern "C" ')
332+
if self.buffer.ends_with(' ') {
333+
self.buffer.pop();
334+
}
335+
336+
let nested_indent = self.shape().indent;
337+
let line = &format!(
338+
"{}{}",
339+
nested_indent.to_string_with_newline(self.config),
340+
"{"
341+
);
342+
self.push_str(line);
343+
}
344+
_ => self.push_str("{"),
345+
};
346+
327347
// FIXME: this skips comments between the extern keyword and the opening
328348
// brace.
329349
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
@@ -347,9 +367,12 @@ impl<'a> FmtVisitor<'a> {
347367
self.block_indent = self.block_indent.block_unindent(self.config);
348368
self.format_missing_with_indent(item.span.hi() - BytePos(1));
349369
}
370+
371+
self.push_str("}");
372+
} else {
373+
self.push_str("{}");
350374
}
351375

352-
self.push_str("}");
353376
self.last_pos = item.span.hi();
354377
}
355378

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for extern blocks
3+
4+
fn main()
5+
{
6+
extern "C"
7+
{
8+
fn i_am_good(x: i32);
9+
}
10+
11+
extern "Rust" {
12+
fn foo(x: i32);
13+
}
14+
15+
extern {
16+
#[link_name = "actual_symbol_name"]
17+
fn foobar();
18+
}
19+
20+
extern "cdecl" {}
21+
22+
extern "stdcall" { }
23+
24+
extern "win64" {
25+
fn bar(x: i32);
26+
}
27+
28+
extern "aapcs" {
29+
}
30+
31+
extern "fastcall"
32+
{/* f*/}
33+
34+
unsafe extern "C++" {
35+
fn lorem();
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// rustfmt-brace_style: AlwaysNextLine
2+
// AlwaysNextLine brace style for extern blocks
3+
4+
fn main()
5+
{
6+
extern "C"
7+
{
8+
fn i_am_good(x: i32);
9+
}
10+
11+
extern "Rust"
12+
{
13+
fn foo(x: i32);
14+
}
15+
16+
extern "C"
17+
{
18+
#[link_name = "actual_symbol_name"]
19+
fn foobar();
20+
}
21+
22+
extern "cdecl" {}
23+
24+
extern "stdcall" {}
25+
26+
extern "win64"
27+
{
28+
fn bar(x: i32);
29+
}
30+
31+
extern "aapcs" {}
32+
33+
extern "fastcall"
34+
{
35+
/* f*/
36+
}
37+
38+
unsafe extern "C++"
39+
{
40+
fn lorem();
41+
}
42+
}

0 commit comments

Comments
 (0)