Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have Xcode auto-align variable names like in Apple's example code?

Tags:

xcode

Apple's example projects often have the variable names aligned, in addition to their type specification/general code indentation:

  NSUInteger        level;
  double            fadeSpeed;
  CGPoint           ballPosition;
  int               keyOffset;

As opposed to:

  NSUInteger level;
  double fadeSpeed;
  CGPoint ballPosition;
  int keyOffset;

Is there a shortcut to make this happen? (I don't know what it would be called or else I could search the docs for it.)

like image 975
buildsucceeded Avatar asked May 28 '12 13:05

buildsucceeded


2 Answers

It sounds like you're looking for Uncrustify. I've not used it in anger myself, but the feature list seems to include your requirement:

Features

  • Ident code, aligning on parens, assignments, etc
  • Align on '=' and variable definitions
  • Align structure initializers
  • Align #define stuff
  • Align backslash-newline stuff
  • Reformat comments (a little bit)
  • Fix inter-character spacing
  • Add or remove parens on return statements
  • Add or remove braces on single-statement if/do/while/for statements
  • Supports embedded SQL 'EXEC SQL' stuff
  • Highly configurable - 412 configurable options as of version 0.59
like image 54
Ashley Mills Avatar answered Sep 30 '22 20:09

Ashley Mills


The popular pretty-printer uncrustify has at least a dozen different variable alignment parameters that you can specify in its config file. Tony Arnold's project shows you how to use uncrustify as an automator service (which means that you can access it from Xcode or any other editor). Robert Payne suggests using it from a shell script that you can incorporate into your build process.

My guess is that Apple runs its examples through uncrustify or some other pretty-printer before publishing so that they're all formatted using a common style. It may take some fiddling if you want to exactly reproduce Apple's style, but at a minimum it should be pretty easy to get your variable declarations to align the way you want. It's been a while since I played with all the options, but I'd start by looking at the align_var_def_span, align_var_def_thresh, and align_var_def_gap settings.

like image 29
Caleb Avatar answered Sep 30 '22 20:09

Caleb