5

I've inherited a huge iOS project which is all using two-space indenting. I need that to be four-space indenting. I know that I can hit Ctrl+I per file, but I'm wondering whether Xcode (or AppCode, for that matter) has a means of re-indenting every source file in the entire project in one whack.

Failing that, I’m wondering how I might go about writing some sort of script (using Automator, or perhaps an Xcode plugin, or even something on the command line) to achieve this without going insane.

Luke
  • 9,512
  • 15
  • 82
  • 146
  • This is possibly outdated by now, but see if the answers in this thread can be of help; http://stackoverflow.com/questions/3216426/is-it-possible-to-set-indent-settings-in-xcode-per-project-or-per-file-even. – dfrib Dec 09 '15 at 16:29
  • 1
    No good I’m afraid – the options there don’t actual re-factor anything, they just set project preferences and won’t modify code. – Luke Dec 09 '15 at 16:32
  • That's too bad... I guess you could download a trial version of JetBrains AppCode and let their code formatter do the job, then return to Xcode. Not the solution you were hoping for though, I'm sure x) https://www.jetbrains.com/objc/features/ – dfrib Dec 09 '15 at 16:40
  • Formatting/indentation/codestyle support in Xcode has always been (and continues to be) significantly lacking. I don't know how Apple does it. – Craig Otis Dec 09 '15 at 16:40
  • 2
    Take a look at clang-format. It may be able to do what you're looking for. It's available in brew. – Rob Napier Dec 09 '15 at 16:43
  • AppCode doesn't seem to do the job either, I have that on my machine and I can't find options that will ONLY indent. – Luke Dec 09 '15 at 16:45

4 Answers4

10

You can use SwiftFormat to make indentation on all of your project .swift files

$ brew install swiftformat
$ cd <path to your project>
$ swiftformat .
Ivan Besarab
  • 990
  • 7
  • 12
2

Refactoring and Re-Indenting are quite different operations. I'm making this distinction so that folks newer to coding / coding IDEs aren't confused.

Refactoring: taking an existing variable / block of code and propagating changes through the project where it is used. For example, refactoring (renaming) a variable foobar to foobar2 would automatically find all cases where foobar is used and change it to foobar2

Re-indenting: changing the structure / flow / alignment of the code to match the desired coding style for clarity / correctness, particularly ensuring that nested items like conditionals and loops are neatly aligned to improve code readability.

Justin Ngan
  • 1,050
  • 12
  • 20
1

You can use SourceKit, but it is a bit complicated to use, so instead you can use SourceKitten which is a wrapper on top of SourceKit. You have to search for all .swift files and run sourcekitten format --file path/to/file

find . -type f -name "*.swift" | while read line; do
  sourcekitten format --file "$line";
done
Mihai Georgescu
  • 674
  • 8
  • 20
0

So, I did manage this with AppCode in the end. We ended up doing a wider-scale refactor using AppCode’s various tools. I’m not sure if I would’ve been able to do JUST the spacing, their Twitter support suggested that I could, but didn’t detail exactly how.

Luke
  • 9,512
  • 15
  • 82
  • 146