Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Xcode to provide code folding?

I'm testing on OS X. I've got a source file that's heavy with preprocessor macros and tests. I'm trying to collapse the code so I can determine why a particular symbol is not being defined.

According to Apple's Folding and Unfolding Source Code, I should be able to do it through the focus ribbon. That's not working - there is no ribbon, and the editor does not respond to requests from the menu.

According to Stack Overflow's Enable code-folding by default (and others like How to collapse all methods in XCode?), I can use hot keys to do it. That's not working either - the editor appears to ignore them too.

enter image description here

I don't have #pragma marks or #regions, so Can you set code regions in Xcode? does not apply.

How do I force Xcode to provide code folding?

like image 278
jww Avatar asked Sep 22 '15 05:09

jww


2 Answers

Within Xcode 7, Folding & Unfolding code blocks is set to OFF.

To change this:

  1. Select Xcode (top left)
  2. Choose Preferences
  3. Navigate to Text Editing (center)
  4. Tick the option Code folding ribbon

.

(see below)

.

enter image description here

Once completed, the shortcuts for folding and unfolding within Xcode should work.

like image 85
ChrisHaze Avatar answered Nov 03 '22 02:11

ChrisHaze


Updates in Xcode 10

Xcode 10 has increased support for code folding, including:

  1. A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
  2. A new style for folded code in the editor that allows you to edit lines with folded code
  3. Support for folding any block of code enclosed in curly braces
  4. Support for folding blocks of code from the folding ribbon, from structured selection, or from the

Menubar ► Editor ► Code Folding ► Fold menu item

enter image description here

Look at this snapshot:

enter image description here


Code folding was disabled in Xcode 9 beta 1, which is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE

Here is how:

  1. Press and hold (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
  2. Keep (hold) (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
  3. Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
  4. Now, to again unfold your code block, release (command) button and click on 3 dots folding a block.

For easy understanding, look at this snapshot:

enter image description here


It's all keyboard short cuts are also working.

Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Unfold All                    ⌥ U        option + U
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + left


Code folding options from Xcode Menu:

Menubar ▶ Editor ▶ Code Folding ▶ "Here is list of code folding options"

Here is ref snapshot:

enter image description here

like image 44
Krunal Avatar answered Nov 03 '22 02:11

Krunal