Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Clang-Format Method Brace Break

I'm working with clang-format for my Objective-C project, but I'm having difficulty setting it up properly.

Here's my desired result:

- (NSString *)someMethod
{
  return @"My String";
}

Here's the actual result after formatting:

- (NSString *)someMethod {
  return @"My String";
}

Here's my .clang-format file:

BasedOnStyle: WebKit
AlignTrailingComments: true
ColumnLimit: 120
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 2
UseTab: Never
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass: true
  AfterControlStatement: false
  AfterEnum: true
  AfterExternBlock: true
  AfterFunction: true
  AfterNamespace: true
  AfterObjCDeclaration: true
  AfterStruct: true
  AfterUnion: true
  BeforeCatch: false
  BeforeElse: false
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true

What setting do I need to change to get the formatter to put a line break before the opening brace of an Objective-C method?

like image 236
Cody Winton Avatar asked Nov 08 '22 01:11

Cody Winton


1 Answers

For any fine-tuning of your clang-format you are much better off never to use BasedOnStyle because that for me just creates random and hard to debug results.

Easiest way to do this would be probably to set: BreakBeforeBraces: Custom and then set everything up the way you want it in as it says in the documentation:

BraceWrapping:   
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   true
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
like image 59
Alper Avatar answered Nov 15 '22 04:11

Alper