📜 ⬆️ ⬇️

About Code Formatting and User Scripts in Xcode 4

When a person is tired of doing the routine work, he has a wild desire to get rid of this work. However, in real life it is not so easy to get rid of it, and all of it must be done. Depending on the type of work and its quantity, you can do differently. You can continue to perform already learned actions, you can withdraw into yourself and accumulate anger on everyone and everything, on this world, on the boss, on PM, on <enter your reason> And you can try to automate this routine.

And the post will be about a specific task that needs to be automated (Code Formatting) and how to expand Xcode with all sorts of goodies for further automation. Last time, it was the Copy Paste Detector . And now it's time for Code-Formattera. I will say right away that the question is not raised here whether an automatic Code-Formatter is needed. It is assumed that you need it. If, nevertheless, it is not needed - you can look under the cat to remember about the possibility to use external scripts in Xcode.

The initial task is to provide the ability to automatically format parts of the source code in Xcode.
Immediately immediately several solutions to this problem:
  1. Continue formatting manually
  2. Use the built-in Xcode Reindent
  3. Use custom scripts
  4. Use external formatters, copy text from Xcode into them, format, copy text back to Xcode
  5. Use Xcode in parallel with other programs that open files from the Xcode project.

')
Let's see how they suit us.

That I will correct with my hands! I'm still formatting with Xcode 1.0


There are such people. Maybe they really do not need text formatting. For those who need it, but they are too lazy:

I mean, if the task (in particular, formatting the code) is large enough, then automation wins over a long distance. It all comes down to time consuming. But even if the time to write the script + the time to solve the problem is longer, compared to if this task was done by hand, then nevertheless, it is worth trying to write such a script - the automation experience is all the same.

In Xcode, and so it is ... No? So it is not necessary.



Opinion of the author, regarding the Reindent command embedded in Xcode: not enough to fully format the code. However, many people developing for Xcode just use it. Sometimes there is still a code-style. Sometimes the Code-Style is described somewhere, and they try to follow it.
All the functionality of Re-Indent is to set spaces / tabs correctly, and its settings are placed on the same tab of the Xcode menu.
Although this option is not replete with a variety of settings, but it has just a huge advantage. It is embedded in Xcode. No need to install anything, go on the Internet and run scripts. Select the piece of code, press ^ I. Maybe this is the part that people like vehemently protecting Xcode like.

Custom scripts? So they did not become in Xcode 4 ...



Yes, in Xcode 4, a lot of things were gone, compared to Xcode 3. User scripts just go to the list of what was not in Xcode 4. People were very disappointed with it in their time.

However, good people thought, thought, and got out. Began to use Automator Services . Not exactly what people used to mean by User Scripts in Xcode 3, but, many tasks, they still solve.

Xcode Integration


Thanks to kind people and OpenSource, integration into Uncrustify in Xcode has become a very simple task.

The project to integrate Uncrustify in Xcode through Automator Services is on GitHub .

We put Uncrustify (Last time, I put uncrustify with the help of homebrew , in principle, as described in the project itself), we put our service scripts in the correct folder ( ~ / Library / Services / Automator / ). Profit If everything is done correctly, now additional commands will appear in the menu in Xcode, allowing you to format the code and which can be used without leaving Xcode.


And here is what you can see by selecting a piece of code and pressing the right button:


Where to go?



When performing all the above steps, you have the opportunity to automatically format the text in Xcode.

What to do next - depends on what you needed before this;)

If the task of automatic / semi-automatic formatting of the code was originally set, then it is worthwhile to set up the uncrustify.cfg configuration file . Universal Indent GUI , a cross-platform code-formatter editor, can help with this, including for uncrustify.

If there was more interest in the possibility of embedding external scripts in Xcode, then through trial and error, using the example of a ready-made connected service, you can think of many useful things.

Alternatives


Personally from myself I would recommend to look towards AppCode about which already wrote in due time on Habré . Even if it is paid with a 30-day free period. The issue of code formatting in Appcod is solved at a completely different level:


Total


There would be a task. You can always find a solution. From time to time this decision will be correct, to work quickly and to solve the set task.

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


All Articles