Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Clang Format for Allman style for iOS coding?

I have a lot of confusion regarding Clang Format API.

  1. I am not able to open .clangformat file so that I can take a look into that and configure according to me.
  2. I need to format my code in Allman style.
  3. I have seen lots of documentation on Google and Stack Overflow as well but I didn't get any help to achieve Allman style formatting.

I came across http://clangformat.com/ but there also I didn't get any help to achieve Allman style.

Here is the issue and the solution I want.

ISSUE #1:

[[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(captureSearchFiltersNotificationWithSelection:)
             name:kSearchFiltersNotificationWithSelection
           object:nil];

NEED #1:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSearchFiltersNotificationWithSelection:)name:kSearchFiltersNotificationWithSelection object:nil];

ISSUE #2:

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {
                //        listings = [NSMutableArray new];
                self.navTitle = @"Buy";
                searchFilters = [SearchFilter new];

                if ([LocationManager locationManager].location == nil) {
                        selectedSortOption = kSortBuyRefineOptionUndefined;
                }

                else {
                        selectedSortOption = kSortBuyRefineOptionNearMeAsc;
                }
        }
        return self;
}

NEED #2:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil 
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        listings = [NSMutableArray new];
        self.navTitle = @"Buy";
        searchFilters = [SearchFilter new];

        if ([LocationManager locationManager].location == nil) 
        {
            selectedSortOption = kSortBuyRefineOptionUndefined;
        }

        else 
        {
            selectedSortOption = kSortBuyRefineOptionNearMeAsc;
        }
    }

    return self;
}
like image 849
Developer Avatar asked Sep 25 '15 07:09

Developer


People also ask

How do you set up clang-format?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Where is clang-format Mac?

If you have the llvm toolchain already installed, you can find the clang-format.py file in /usr/local/opt/llvm/share/clang/clang-format.py without having to install a separate clang-format binary through Homebrew.

Where is clang-format file located?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Obj-C code.


1 Answers

You need to add a .clang-format file in your project root directory. As per the requirement you can edit this file in TextEditor. Below is the format for "Allman" style:

BasedOnStyle: None
AlignTrailingComments: true
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 8
UseTab: Never

Below are some links which can be helpful to you:

http://staxmanade.com/2015/01/how-to-install-clang-format-and-formatting-objective-c-files/

http://tonyarnold.com/2014/05/31/autoformatting-your-code.html

http://clang.llvm.org/docs/ClangFormatStyleOptions.html

http://blog.manbolo.com/2015/05/14/code-beautifier-in-xcode

Xcode project link to generate and install clang-format file: https://github.com/travisjeffery/ClangFormat-Xcode/blob/master/README.md

Fixing .clang-format issue: https://github.com/travisjeffery/ClangFormat-Xcode/issues/68

Apple's source code formatting options: https://developer.apple.com/legacy/library/documentation/DeveloperTools/Reference/XcodeUserDefaultRef/100-Xcode_User_Defaults/UserDefaultRef.html#//apple_ref/doc/uid/TP40005535-CH3-SW57

Hope this helps!

like image 74
Dharmesh Siddhpura Avatar answered Oct 03 '22 19:10

Dharmesh Siddhpura