Code-formatter for Objective-C
Asked Answered
S

2

13

Similar questions have been asked before, but they didn't help me with what I'd like to do:

I want to re-format existing Objective-C code (several hundred files). For pure Apple-style formatting, uncrustify seems to do what I want. But for a few projects, I need a different style that I haven't found out how to configure uncrustify for. In this style, long method calls look like this (please refrain from discussing whether you like that style or not; do not suggest to use a different style):

[self
    longMethod:arg1
    withLots:arg2
    ofArguments:arg3
    aBlock:^{
       [self doSomething];
    }
    andAnotherBlock:^{
       [self doSomethingElse];
    }
];

This wrapping is done when the method call would exceed a line length of 80 or 100 characters. Each line is indented by one level and contains exactly one argument and the selector part up to the corresponding :. The lines thus are not colon-aligned.

No wrapping is done if the line length is below 80 or 100 characters:

[self shortMethod:withAnArgument];

Is there a code formatter that can be tweaked to support this style? If so, which and more importantly, how?

Spitzer answered 14/5, 2014 at 12:51 Comment(4)
The question is not off-topic as it's about development tools which are explicitly mentioned in the About page (see also on meta.stackoverflow.com). See also the on-topic page in the FAQs: this is not about opinions but facts: either a tool can do it or it can't. If it can, I'd like to know how.Spitzer
I went back and re-read the guidelines, and can not imagine how this is off-topic either.Legionnaire
It always hurts my eyes to see 80 chars limit for any language than pure C. Character limit enforced because of IBM punched cards and working only because the function names in C couldn't be longer than 8 chars... For Obj-C the wrapping results in a really strange format and I don't think you will find a formatter supporting it. The stranger the format, the less likely you will find a formatter.Anyone
@Sulthan: See my question: please refrain from discussing whether you like that style or not; do not suggest to use a different style. This is as useless as discussing K&R vs. Allman style in C.Spitzer
L
2

Clang format can be used to format code in any number of styles. You can even specify the exact options you desire, or use one of several "standard" styles.

There is an Xcode plugin as well.

Legionnaire answered 14/5, 2014 at 13:11 Comment(6)
But does it support the format described? If so, how?Spitzer
Well, that format is ugly to my eyes, so I've never tried that exact format :-). However, I do know it has options for specifying wrap styles and line lengths. I also think it has options to align or not align based on the colons. It has the most formatting options for Obj-C that I know of, and appears to match. However, I am afraid that I am not going to spend my time trying to tweak the options to match that exact format.Legionnaire
So I did have a look at Clang format and it doesn't help me. In fact, it's even less useful than uncrustify since there's not much you can tune about Objective-C method calls.Spitzer
It's hard to get automatic code formatters to do exactly what everyone wants. Usually, you have to make compromises in what you want if you want it done automatically. I have found a somewhat happy place with clang-format... except for the way it indents inline blocks... have not yet found anything I like.Legionnaire
@JodyHagins One of the best things you can do for code readibility is to stop using inline blocks :) It took me years to understand that creating a variable for something makes code much more readable simply because of the fact that you will add a "name".Anyone
@Anyone Done it both ways, in several different languages (including Obj-C and C++). I find that, in general, inline blocks increase code readability. Like anything though, it can be abused. All tools are useful.Legionnaire
A
2

No, I do not think that there is code formatter for this style. You can use clang's Lib Tooling to do the job. Yes, you have to build you own tool using it. But developing your own tool is the usual way, if you want to do something very unusual.

Amourpropre answered 26/5, 2014 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.