有没有办法折叠Xcode项目中的所有方法?

9
Xcode提供了当前文件的方法折叠选项。但是有没有办法将其应用于项目中的所有.m文件?

enter image description here

附注:我尝试过Xcode运行脚本和Xcode插件开发,但未能找到合适的解决方案。

我认为这是不可能的。而且我不明白为什么有人想要这样做。 - dasdom
为什么有人会想要这个?你在代码中遇到导航问题了吗?使用快速跳转栏。 - pronebird
3个回答

5

这里提供一个目的为此的AppleScript。需要注意的是:

  • 需要使用System Events。
  • 如果您同时打开多个工作区或您的工作区包含多个项目,则该脚本可能需要进行更改。
  • 您可能需要在项目导航器中选择一个与.m文件不同的文件。
    -- Collect all Objective-C file references from the given group.
    on handleGroup(theGroup, codeRefs)
        tell application "Xcode"
            set fileRefs to theGroup's file references
            repeat with x in fileRefs
                if x's file kind is equal to "sourcecode.c.objc" then
                    copy x to end of codeRefs
                end if
            end repeat

            set subgroups to theGroup's groups
            repeat with x in subgroups
                my handleGroup(x, codeRefs)
            end repeat
        end tell
    end handleGroup


    on moveFocusToProjectNavigator()
        tell application "System Events"
            tell process "Xcode"
                set frontmost to true
                tell menu bar item "Navigate" of menu bar 1
                    tell menu 1
                        click menu item "Reveal in Project navigator"
                    end tell
                end tell
            end tell
        end tell
    end moveFocusToProjectNavigator


    on moveFocusToNextArea()
        tell application "System Events"
            tell process "Xcode"
                set frontmost to true
                tell menu bar item "Navigate" of menu bar 1
                    tell menu 1
                        click menu item "Move Focus to Next Area"
                    end tell
                end tell
            end tell
        end tell
    end moveFocusToNextArea


    -- Simulate pressing the up arrow.
    on selectNextItem()
        tell application "System Events"
            tell process "Xcode"
                key code 126
            end tell
        end tell
    end selectNextItem


    on fold(shouldFold)
        set theCommand to "Fold Methods & Functions"
        if shouldFold is equal to 0 then
            set theCommand to "Unfold All"
        end if

        -- Fold the code by using the menu item.
        tell application "System Events"
            tell process "Xcode"
                set frontmost to true

                tell menu bar item "Editor" of menu bar 1
                    tell menu 1
                        tell menu item "Code Folding"
                            tell menu 1
                                click menu item theCommand
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end fold


    on run
        -- Set to one to fold, zero to unfold.
        set shouldFold to 1

        tell application "Xcode"
            set ws to every workspace document
            set theWorkspace to item 1 of ws
            tell theWorkspace
                set pr to item 1 of projects
                set codeRefs to {}
                my handleGroup(pr, codeRefs)

                -- Open each document and fold the code.
                set thePaths to {}
                repeat with x in codeRefs
                    copy x's full path to the end of thePaths
                end repeat
            end tell

            -- If we only have one path, a new workspace won't be opened.
            if 1 is equal to (count of thePaths) then
                open item 1 of thePaths
                my moveFocusToNextArea()
                my fold(shouldFold)
            else
                open thePaths

                set ws to every workspace document
                set theWorkspace to item 1 of ws
                my fold(shouldFold)
                repeat (count of theWorkspace's file references) - 1 times
                    my moveFocusToProjectNavigator()
                    my selectNextItem()
                    my moveFocusToNextArea()
                    my fold(shouldFold)
                end repeat

                -- If we don't wait before closing the window, the last document
                -- won't be folded.
                tell application "System Events" to delay 3

                tell theWorkspace to close
            end if
        end tell
    end run

为了完整起见,这里提供一种早期的尝试。由于需要逐个按URL打开文档,因此速度相对较慢。

    -- Collect all Objective-C file references from the given group.
    on handleGroup(theGroup, codeRefs)
        tell application "Xcode"
            set fileRefs to theGroup's file references
            repeat with x in fileRefs
                if x's file kind is equal to "sourcecode.c.objc" then
                    copy x to end of codeRefs
                end if
            end repeat

            set subgroups to theGroup's groups
            repeat with x in subgroups
                my handleGroup(x, codeRefs)
            end repeat
        end tell
    end handleGroup

    on run
        tell application "Xcode"
            set ws to every workspace document
            tell item 1 of ws
                set pr to item 1 of projects
                set codeRefs to {}
                my handleGroup(pr, codeRefs)

                -- Open each document and fold the code.
                repeat with x in codeRefs
                    set thePath to x's full path
                    set doc to open thePath

                    tell doc
                        activate
                    end tell

                    -- Fold the code by using the menu item.
                    tell application "System Events"
                        tell process "Xcode"
                            set frontmost to true

                            tell menu bar item "Editor" of menu bar 1
                                tell menu 1
                                    tell menu item "Code Folding"
                                        tell menu 1
                                            click menu item "Fold Methods & Functions"
                                        end tell
                                    end tell
                                end tell
                            end tell
                        end tell
                    end tell

                end repeat
            end tell
        end tell
    end run

谢谢你的回答。我会尝试这个方法并告诉你结果。 - Thilina Chamath Hewagama
我们能加快这个过程吗? - Thilina Chamath Hewagama
我有一个想法,我会尝试着去实现它。 - tsnorri
我接受你的答案,但如果你找到更好的方法,请发帖告诉我。 - Thilina Chamath Hewagama
谢谢!我有一点空闲时间,所以我写下了我想要的更改。几乎每次我尝试使用AppleScript做些什么时,一开始似乎几乎微不足道,但最终却变得非常复杂。 - tsnorri

2
也许更简单更好的解决方案是使用热键: 当您打开某个 .m 文件时,只需按下 Cmd + Alt + Shirt + <- 折叠 或者 Cmd + Alt + Shirt + -> 折叠

我经常使用这些快捷键,但现在我想要做的是一次性折叠所有的方法 :-) - Thilina Chamath Hewagama
在这种情况下,我不知道如何帮助你,很抱歉。也许Apple Script是一个选项,或者直接编辑UserInterfaceState.xcuserstate文件。 - Vitalii Gozhenko

1

@Geuis,xCode从xCode 8开始停止支持插件 :-( - Thilina Chamath Hewagama
@ Thilina Chamin Hewagama 噢,真遗憾。谢谢你提供的信息。 - Geuis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接