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?

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • The question is not off-topic as it's about development tools which are explicitly mentioned in the [About page](http://stackoverflow.com/about) (see also on [meta.stackoverflow.com](http://meta.stackoverflow.com/questions/254570/choosing-between-stack-overflow-and-programmers-stack-exchange/254571#254571)). See also the [on-topic page in the FAQs](http://stackoverflow.com/help/on-topic): 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. – DarkDust May 14 '14 at 13:15
  • I went back and re-read the guidelines, and can not imagine how this is off-topic either. – Jody Hagins May 14 '14 at 13:19
  • 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. – Sulthan May 26 '14 at 08:17
  • @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. – DarkDust May 26 '14 at 12:10

2 Answers2

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.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • But does it support the format described? If so, how? – DarkDust May 14 '14 at 13:12
  • 3
    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. – Jody Hagins May 14 '14 at 13:17
  • 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. – DarkDust May 22 '14 at 13:05
  • 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. – Jody Hagins May 23 '14 at 16:14
  • 2
    @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". – Sulthan May 26 '14 at 08:20
  • 1
    @Sulthan 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. – Jody Hagins May 26 '14 at 20:49
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.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50