Tuesday, March 15, 2016

Xcode block (NSMallocBlock) indentation (braces automatically starting from new line)

Leave a Comment

I want to open blocks with braces starting at new line.

Let's move away from philosophical discussion of whether this is a good decision or not -- regardless of 'where' and 'when', 'how' remains absolute.

So, the problem is described below:

enter image description here

Is there a way to achieve this?


I was looking around for quite a while, but I didn't find any solutions.

I am already aware of such things as:

  1. Snippet Edit -- this little program will allow to edit default Xcode's code snippets. So, you will be able to open braces from new line in your if, for, while, etc. However, this doesn't allow to change the block indentation.

  2. Uncrustify -- this might solve my problem, but it doesn't look like being easy to set up. And it only formats the code after it is already written, instead of formatting 'on the go'. Moreover, even if I set uncrustify correctly, it will still be quite slow -- about 4 mouse clicks are required to format the block, which is time consuming.

Is anybody aware of any other solutions?

EDIT:

To be more specific, I want the block to be indented as soon as I press ENTER on this step:

enter image description here

2 Answers

Answers 1

The easiest solution that i've found is this:

Download Karabiner and change your private.xml (you can also view documentation here)

Your private.xml may look like this:

<?xml version="1.0"?> <root>     <item>         <name>Command+Enter to complete single block</name>         <identifier>private.swap_cmnd+Enter_to_complete__single_block</identifier>         <autogen>             __KeyToKey__             KeyCode::RETURN, ModifierFlag::COMMAND_L | ModifierFlag::NONE,             KeyCode::RETURN,              KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,             KeyCode::DELETE, ModifierFlag::COMMAND_L,             KeyCode::BRACKET_RIGHT, ModifierFlag::SHIFT_L,             KeyCode::BRACKET_RIGHT,             KeyCode::SEMICOLON,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,             KeyCode::CURSOR_LEFT,             KeyCode::RETURN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,              KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,              KeyCode::I, ModifierFlag::CONTROL_L,             KeyCode::CURSOR_RIGHT,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_LEFT, ModifierFlag::SHIFT_L,             KeyCode::TAB,         </autogen>     </item>      <item>         <name>Option+Enter to complete double block</name>         <identifier>private.swap_Option+Enter_to_complete_double_block</identifier>         <autogen>             __KeyToKey__             KeyCode::RETURN, ModifierFlag::OPTION_L | ModifierFlag::NONE,             KeyCode::CURSOR_RIGHT,             KeyCode::RETURN,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_L,             KeyCode::TAB,              KeyCode::RETURN,              KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_L,             KeyCode::TAB,             KeyCode::RETURN,             KeyCode::DELETE,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,             KeyCode::DELETE, ModifierFlag::COMMAND_L,             KeyCode::BRACKET_RIGHT, ModifierFlag::SHIFT_L,             KeyCode::BRACKET_RIGHT,             KeyCode::SEMICOLON,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,             KeyCode::CURSOR_LEFT,             KeyCode::RETURN,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,             KeyCode::CURSOR_LEFT,             KeyCode::RETURN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_DOWN,             KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L,              KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,             KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L,              KeyCode::I, ModifierFlag::CONTROL_L,             KeyCode::CURSOR_RIGHT,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_UP,             KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_L,             KeyCode::TAB,         </autogen>     </item> </root> 

After this is done, you can now open the block by pressing +ENTER (instead of pressing ENTER, when opening the block, press +ENTER). Here is the preview (slowed down, so that you see details).

The above works with single blocks only. If you want to open two blocks at once, you can press ALT+ENTER. Here is the preview (slowed down, so that you see details).

Here is the preview with the actual speed:

Demo CountPages alpha

Answers 2

I don't think there is a way to do that automatically after you press ENTER.

What you can do, is hit ENTER to let it fill the snippet, and then go before { and hit ENTER again. Xcode formats it the way you like afterwards.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment