How to configure Clang Format for Allman style for iOS coding?
Asked Answered
S

1

10

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;
}
Slaver answered 25/9, 2015 at 7:3 Comment(4)
@halfer Any idea about answer?Slaver
Re: "I am not able to open .clangformat file so that I can take a look into that and configure according to me.", could you clarify what you mean, as it's unclear? The filename should normally be .clang-format or _clang-format and placed in the project directory.Gyron
@l'L'l In which editor, I can see the content of ".clang-format" file?Slaver
@Slaver any editor, of courseEzzell
S
6

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!

Sharpen answered 28/9, 2015 at 5:45 Comment(6)
You should read my question again. And also about Clang Format. I think you didn't my question.Slaver
I have .clang-format class at the root directory but I need to know how can I configure that for Allman Style. If you want I can send you that file or you send the formatted file.Slaver
I have already read all the documentaion and also configured the file as you have mentioned but still it is not working. Please create one sample code with my issues and test your file. And kindly let me know if that worked for you. You can share the screen recording also.Slaver
To achieve your style, I need to spend extra time and I have to do some research for .clang-format file configuration. But yes this is the way to get the desired formatting. You can even go to Xcode -> Preferences -> Text Editing -> Indentation for Xcode default formatting settings. Let me know if you achieve this as this will be a good research and can be helpful to others.Sharpen
Did you find anything?Slaver
@Slaver it is highly unrealistic for you to ask someone to share a screen recording. This is a very helpful answer.Lavernelaverock

© 2022 - 2024 — McMap. All rights reserved.