Xcode 块 (NSMallocBlock) 缩进(大括号自动从新行开始)

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

我想用大括号从新行开始打开块。

让我们远离关于这是否是一个好决定的哲学讨论 -- regardless of 'where' and 'when', 'how' remains absolute.

所以,问题描述如下:

有办法实现吗?


我四处寻找了一段时间,但没有找到任何解决方案。

我已经知道这样的事情:

  1. Snippet Edit -- 这个小程序将允许编辑默认 Xcode 的代码片段。因此,您将能够在 ifforwhile 等中从新行打开大括号。但是,这不允许更改块缩进。

  2. Uncrustify -- 这可能会解决我的问题,但看起来并不容易设置。并且它只在之后格式化已经写好的代码,而不是格式化'on the go'。而且,即使我设置正确uncrustify,它仍然会很慢——大约需要4次鼠标点击来格式化块,这很耗时。

是否有人知道任何其他解决方案?

编辑:

更具体地说,我希望只要我在这一步按ENTER就缩进该块:

我认为没有办法在您按下 ENTER 后自动执行此操作。

可以 做的是点击 ENTER 让它填充代码段,然后在 { 之前点击 ENTER再次。 Xcode 之后按照您喜欢的方式进行格式化。

我找到的最简单的解决方案是:

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

您的 private.xml 可能如下所示:

<?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>

完成此操作后,您现在可以通过按 +ENTER 打开方块( 而不是 ENTER,当打开块,按 +ENTER)。这里是preview(放慢速度,让你看到细节)。

以上仅适用于单个块。如果你想一次打开两个块,你可以按ALT+ENTER。这里是preview(放慢速度,让你看到细节)。

这里是preview与实际速度:

只要手动把你的大括号换行,那么你的缩进应该是好的。