如何在Xcode 6.1中更改花括号样式?

时间:2014-11-28 03:09:18

标签: coding-style xcode6 editor indentation

对于以前版本的Xcode,这个问题有答案,但没有一个能满足我的需求。

默认情况下,Xcode 6.1默认为缩进的K&R style,但我更喜欢Allman style。现在,我的所有代码片段都是Allman样式,但是Xcode的自动完成功能可以实现的功能仍然将大括号放在与函数声明相同的行上。例如,如果我创建一个类并编写init并让Xcode自动完成,它会将左大括号放在同一行上。手动修复它并不是一件大事,但它有点拖累。

我怎样才能实现这项功能,即使它很糟糕?

1 个答案:

答案 0 :(得分:0)

我有同样的挫败感,并开发了一种在某种程度上有所帮助的AppleScript。 Xcode中的文本编辑很难通过脚本编写支持,所以我的脚本有一些怪癖。当您在Xcode工作区中打开.swift文件时,它似乎效果最好,当您打开要编辑花括号的文件时,它似乎也更快乐。

它只会正确地缩进类和func级别打开花括号,尽管所有其他开口花括号将在它们自己的行上。我通常在添加了所有IBActions之后运行它来为我清理它。

我将脚本存储在〜/ Library / Scripts / Applications / Xcode文件夹中,以便我可以轻松访问它。

在这里,祝你好运:

set oneIndent to space & space & space & space -- Four spaces for indent (see Text Editing Preferences -> Indentation)
set isFirstBrace to true
global oneIndent, isFirstBrace

tell application "Xcode"
    set allDocs to every text document
    set theirNames to the name of every text document
    choose from list theirNames with prompt "Choose your active file:"
    if the result is not false then
        set aName to item 1 of the result
        set c to my FindItemNumber(theirNames, aName)
        set workingDocument to item c of allDocs
        set delegateText to text of workingDocument
        set textParagraphs to every paragraph of delegateText
        set newTextString to {}
        repeat with eachPara in textParagraphs
            if (eachPara as text) contains "{" then
                set end of newTextString to my solveIndent(eachPara as text)
                if isFirstBrace is true then set isFirstBrace to false
            else
                set end of newTextString to (eachPara as text) & return
            end if
        end repeat
        set text of item 1 of workingDocument to (newTextString as text)
    end if
end tell

----------------------
to solveIndent(p)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "{"
    if isFirstBrace is true then
        set newParagraph to (text item 1 of p) & return & "{" & (text item 2 of p) & return
    else
        set newParagraph to (text item 1 of p) & return & oneIndent & "{" & (text item 2 of p) & return
    end if
    set AppleScript's text item delimiters to astid
    return newParagraph
end solveIndent

--------------------------
to FindItemNumber(lst, choice)
    set counter to 1
    repeat with eachOption in lst
        if choice = (eachOption as text) then
            set cc to counter
            exit repeat
        end if
        set counter to counter + 1
    end repeat
    return cc
end FindItemNumber