有什么办法可以折叠Xcode项目中的所有方法吗?

时间:2021-02-19 15:02:08

Xcode provides method folding option for the current file. But is there any way to apply this to all the .m files in the project ?

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

有什么办法可以折叠Xcode项目中的所有方法吗?

p.s: I tried Xcode run scripts & Xcode plugin development, but failed to comeup with a proper solution

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

3 个解决方案

#1


5  

Here's an AppleScript for the purpose. Caveat:

这是一个AppleScript用于此目的。警告:

  • System Events is required.
  • 系统事件是必需的。

  • The script probably needs changes if you have multiple workspaces open or your workspace contains multiple projects.
  • 如果打开多个工作空间或工作空间包含多个项目,则脚本可能需要更改。

  • You may need to have something else than an .m file selected in the project navigator.
  • 您可能需要在项目导航器中选择.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

For the sake of completeness, here's an earlier attempt. It is quite slow since it needs to open one document by URL at a time.

为了完整起见,这是一个较早的尝试。它非常慢,因为它需要一次按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

#2


2  

Maybe much easier and better solution will be use hotkey: When you open some .m file, just press Cmd + Alt + Shirt + <- to fold or Cmd + Alt + Shirt + -> to fold

也许更容易和更好的解决方案将使用热键:当你打开一些.m文件,只需按Cmd + Alt + Shirt + < - 折叠或Cmd + Alt + Shirt + - >折叠

#3


1  

Done and dusted.

I wrote a xCode plugin which easily does the work.
plugin: https://github.com/ThilinaHewagama/HCTAutoFolding

我写了一个xCode插件,可以轻松完成工作。插件:https://github.com/ThilinaHewagama/HCTAutoFolding

#1


5  

Here's an AppleScript for the purpose. Caveat:

这是一个AppleScript用于此目的。警告:

  • System Events is required.
  • 系统事件是必需的。

  • The script probably needs changes if you have multiple workspaces open or your workspace contains multiple projects.
  • 如果打开多个工作空间或工作空间包含多个项目,则脚本可能需要更改。

  • You may need to have something else than an .m file selected in the project navigator.
  • 您可能需要在项目导航器中选择.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

For the sake of completeness, here's an earlier attempt. It is quite slow since it needs to open one document by URL at a time.

为了完整起见,这是一个较早的尝试。它非常慢,因为它需要一次按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

#2


2  

Maybe much easier and better solution will be use hotkey: When you open some .m file, just press Cmd + Alt + Shirt + <- to fold or Cmd + Alt + Shirt + -> to fold

也许更容易和更好的解决方案将使用热键:当你打开一些.m文件,只需按Cmd + Alt + Shirt + < - 折叠或Cmd + Alt + Shirt + - >折叠

#3


1  

Done and dusted.

I wrote a xCode plugin which easily does the work.
plugin: https://github.com/ThilinaHewagama/HCTAutoFolding

我写了一个xCode插件,可以轻松完成工作。插件:https://github.com/ThilinaHewagama/HCTAutoFolding