RobotsAndPencils/objective-c-style-guide — pull request #2 by interstateone (2014-03-18 02:52:14+00:00)
Title: Add uncrustify.cfg for automated formatting

Adds an Uncrustify config file for automated formatting. This can be used with an [Xcode plugin](https://github.com/benoitsan/BBUncrustifyPlugin-Xcode) or even potentially with a PR review bot to show formatting diffs for reviewers.

This should be as close as possible to our style guide, with a few minor exceptions.

This configuration can be tested by running with the following snippets, yanked from AppCode's code style preview panes:

## Tabs and indents:
```objc
void globalFunc();
namespace foo {
class FooClass {
public:
FooClass();
~FooClass();
private:
int var;
};
}
@interface Foo {
@public
:
int i;
}

@property int a;
- (id)init;
@end

@protocol Foo

@optional
@property int a;
- (id)init;
@end

@implementation Foo

@synthesize a;

void innerFunc();
int innerVar;

- (id)init {
printf(
"");
return self;
}
@end
```

## Spaces
```objc
@interface Foo (Bar) : NSObject {
@public
int i;
@private
id bar;
}

- (id)from:(int)n with:(int)k andWith:(void *)p;
@property (readonly, getter=getParent) __weak Foo *parent;
@end

@implementation Foo

@synthesize parent, foo;

- (id)from:(int)n with:(int)k andWith:(void *)p {
void *s = (void *)@selector(foo:bar:);
[[[Foo create:s with:42] init:p andWith:i] perform];
[NSDictionary dictionaryWithObjectsAndKeys:@1, @"one", @222, @"twoTwoTwo", nil];
return ^int(int n, int k) {
return n + k;
};
}
@end

typedef void(fn)(int i, int j, int k);
typedef void(^block)(int i, int j, int k);
typedef int X;

int &refTest(X &&x) {
int **&p = (int **&)x;
int static &r = *&x;
return r && (r & x) ? r : x;
}

void doSomething();

void doSomething(int a, int b, void *(*)()) {
int i1 = 1 || 0 && 1;
int i2 = i1 == !1 && i1 != 0;
int i3 = 1 < 2 >= 3;
int i4 = ~1 | 2 & 3 ^ 4;
int i5 = ((1) + 2) - (4 * 5 / 6 % 7);
int i6 = -1 << 2 >> -3;
int i7 = 2 > 3 ? 7 + 8 + 9 : 11 + 12 + 13;
int i8 = 2 < 3 + 7 + 8 + 9 ?: 11 + 12 + 13;
void **p1 = &0;
void *p2 = **p;
int ii[3], jj[] = { 1, 2, 3 };
id array = @[ @1, @2 ];
id dictionary = @{ @1 : @"one", @222 : @"twoTwoTwo" };
iiiii = jjjjj = kkkkk = mmmmm = 22222;

doSomething(ii[1], jj[ii[2]], doSomething(123));

if (1)doSomething();
else if (2)doSomething();
if (1) {
doSomething();
} else if (2) {
doSomething();
} else doSomething();
for (int i = 1, j = 2; i <= j; i++, j--)doSomethingElse();
while (1)doSomethingElse();
do doSomethingElse(); while (1);
switch (1) {
case 0:
return;
case 1: {
return;
}
}

@try {
doSomethingElse();
} @catch (NSException *e) {
return;
} @finally {
return;
}
@synchronized (self) {
doSomethingElse();
}
@autoreleasepool {
doSomethingElse();
}
}

struct foo {
int i;
char j;
} foo_t;

enum foo {
i = 111, jjj = 222, kkkkk = 333
} foo_e;

namespace foo {
class FooClass : BarClass, virtual BazClass {
public:
FooClass();
virtual ~FooClass();
private:
int var;
};
}
```

## Wrapping and spaces
```objc
@interface Foo (Bar) : NSObject {
@public
int i;
@private
id bar;
}

- (id)from:(int)n with:(int)k andWith:(void *)p;
@property (readonly, getter=getParent) __weak Foo *parent;
@end

@implementation Foo

@synthesize parent, foo;

- (id)from:(int)n with:(int)k andWith:(void *)p {
void *s = (void *)@selector(foo:bar:);
[[[Foo create:s with:42] init:p andWith:i] perform];
[NSDictionary dictionaryWithObjectsAndKeys:@1, @"one", @222, @"twoTwoTwo", nil];
return ^int(int n, int k) {
return n + k;
};
}
@end

typedef void(fn)(int i, int j, int k);
typedef void(^block)(int i, int j, int k);
typedef int X;

int &refTest(X &&x) {
int **&p = (int **&)x;
int static &r = *&x;
return r && (r & x) ? r : x;
}

void doSomething();

void doSomething(int a, int b, void *(*)()) {
int i1 = 1 || 0 && 1;
int i2 = i1 == !1 && i1 != 0;
int i3 = 1 < 2 >= 3;
int i4 = ~1 | 2 & 3 ^ 4;
int i5 = ((1) + 2) - (4 * 5 / 6 % 7);
int i6 = -1 << 2 >> -3;
int i7 = 2 > 3 ? 7 + 8 + 9 : 11 + 12 + 13;
int i8 = 2 < 3 + 7 + 8 + 9 ?: 11 + 12 + 13;
void **p1 = &0;
void *p2 = **p;
int ii[3], jj[] = { 1, 2, 3 };
id array = @[ @1, @2 ];
id dictionary = @{ @1 : @"one", @222 : @"twoTwoTwo" };
iiiii = jjjjj = kkkkk = mmmmm = 22222;

doSomething(ii[1], jj[ii[2]], doSomething(123));

if (1)doSomething();
else if (2)doSomething();
if (1) {
doSomething();
} else if (2) {
doSomething();
} else doSomething();
for (int i = 1, j = 2; i <= j; i++, j--)doSomethingElse();
while (1)doSomethingElse();
do doSomethingElse(); while (1);
switch (1) {
case 0:
return;
case 1: {
return;
}
}

@try {
doSomethingElse();
} @catch (NSException *e) {
return;
} @finally {
return;
}
@synchronized (self) {
doSomethingElse();
}
@autoreleasepool {
doSomethingElse();
}
}

struct foo {
int i;
char j;
} foo_t;

enum foo {
i = 111, jjj = 222, kkkkk = 333
} foo_e;

namespace foo {
class FooClass : BarClass, virtual BazClass {
public:
FooClass();
virtual ~FooClass();
private:
int var;
};
}
```

## Blank lines
```objc
#import

void doSomething();
void doSomethingElse();
void global1;
void global2;

@interface Foo {
int i;
int j;
}

@property int a;
@property int b;
- (id)init;
- (void)doFoo;

@end

@implementation Foo

@synthesize x;
@dynamic y;
void innerGlobal1;
void innerGlobal2;

- (id)init {
printf("");
return self;
}

- (void)doFoo {
int k = i + j;
int m = k * 2;
}

@end

void doSomething() {
int i = 1;
}

void doSomethingElse() {
int i = 2;
}

namespace foo {
class FooClass {
public:
FooClass();
~FooClass();
private:
int var1;
int var2;
};

class BarClass {
};
}
namespace bar {
}
```