Is there a way for Xcode to automatically align variable names, such as in the Apple code example?

Examples of Apple projects often have aligned variable names, in addition to their generic code type / indentation specification:

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

Unlike:

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

Is there a shortcut to make this happen? (I don't know what he will be called for, or I could look for documents for him.)

+6
source share
4 answers

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

My guess is that Apple runs its examples through uncrustify or some other beautiful printer before publishing, so they are all formatted using a common style. This may take some time if you want to accurately reproduce Apple's style, but at least it's pretty easy to get variable declarations to be consistent as you want. Some time has passed since I played with all the parameters, but I would start by considering the settings align_var_def_span , align_var_def_thresh and align_var_def_gap .

+1
source

Looks like you're looking for Uncrustify . I did not use it in anger myself, but the list of functions seems to include your requirement:

Functions

  • Identification code, alignment by parameters, purposes, etc.
  • Align to '=' and variable definitions
  • Align structure initializers
  • Align #define.
  • Combine backslash elements
  • Reform Comments (a bit)
  • Fix Intersymbol
  • Add or remove parsers in return statements
  • Add or remove curly braces in a single-statement if / do / while / for statement
  • Support for embedded SQL 'EXEC SQL'.
  • High configuration - 412 customizable parameters version 0.59
+3
source

I do not know if there is a shortcut, but you can create a program that takes a large string as input (variables), and the output will be an aligned version of this. Writing this program should not cause too many problems.

0
source

Xcode has no built-in function for this, but you can easily align variable names with tab . You should press tab after type several times and align to width.

0
source

Source: https://habr.com/ru/post/916846/


All Articles