Monthly Archives: May 2012

Folding code in Xcode

A colleague of mine has the practice of collapsing all the methods in an Objective C file when he loads it to simplify finding the right method to view. When I asked him how he did it, he couldn’t remember since it was an automatic action. I finally got around to figuring out how to do it.

The feature is called Code Folding and can be done using the pointer, the menu bar, and the keyboard.

Code Folding using the Pointer

You may have noticed the different colored gray bars on the left of your code in Xcode and may have even accidentally hovered over one of them and been treated to a drastic change in the UI. Those gray bars allow you to fold – or collapse – your code. Using those bars, you can fold any size block, such as an if block, a for block, an @synchronized block, or even a whole method block.

Hovering over the gray bar shows you the block that will be folded. Clicking on the gray bar folds the block and shows a yellow ellipsis between the curly braces where the block used to be. It also changes the gray bar to an arrow. Clicking the arrow or double-clicking the ellipsis will unfold – or expand – the folded block.

Code Folding using the Menu Bar

The code folding functionality is exposed in the menus accessible from the menu bar as well.  In Xcode 4, check out Editor -> Code Folding.

Code Folding using the Keyboard

Apple has defined default keystrokes for code folding, but I found that some of them conflicted with keystrokes defined for other functions. In most cases the conflicts were caused by changes I had made – either in Xcode or in Mac OS X Lion – to accommodate writing code.

The following code folding functionality can be controlled using the keyboard:

  1. Fold all methods and functions – Option + Cmd + Shift + Left Arrow
  2. Unfold all methods and functions – Option + Cmd + Shift + Right Arrow
  3. Fold the current block – Option + Cmd + Left Arrow
  4. Unfold the current block – Option + Cmd + Right Arrow

I had to change the fold/unfold block key bindings because they conflicted with the key bindings I had selected for switching between Spaces in Lion. I changed them to Ctrl + Cmd + Shift + Left Arrow/Right Arrow respectively. This, of course, required that I change another key binding in Xcode – the key bindings for fold/unfold comment blocks.

Comment blocks are blocks of comments using /* and */ as delimiters. Since I almost never use this style of commenting, removing the key binding for folding/unfolding comment blocks in favor of code folding/unfolding was a good trade-off for me.

Mac Utility: Save

I’ve been enjoying my switch from Windows to Mac.  However just like Windows, Mac OS X has its quirks and irritations.  There are some very nice changes in 10.7 (Lion) to window management, but one thing missing is to restore window positions and sizes when switching between a single monitor and multiple monitors.

The Stay utility from Cordless Dog solves this problem.  Here is the summary of the app from the website:

What is Stay?

If you’re fastidious about keeping your windows tidy, Stay is for you. Stay ensures that your windows are always where you want them to be, even as you connect and disconnect displays.

The app is available both from the App Store as well as from their website and costs $15 ($14.99 from the App Store).  The difference between the two versions is that the version from the website supports Spaces.  The reason for this is that Spaces support requires private APIs, and Apple doesn’t allow apps submitted to the App Store to call private APIs.  Cordless Dog did a cool thing, though, for those that want to purchase from the App Store but also want Spaces support.  Simply make your purchase from the App Store and then install from the website.

One thing I’m not crazy about is that they app is delivered as a .zip file from the website rather than as a .pkg file.

Functionality Overview

Stay provides the following functionality:

  • Store windows for all applications.
  • Store windows for the current application.
  • Store the active window.
  • Restore all windows.
  • Edit stored windows.

When you choose to store windows for all applications, the app flips through all the windows, which takes more time than you’d think.  If you’re running the version that supports Spaces, it takes even longer, depending on how many windows and spaces you have.

Restoring windows isn’t quite as reliable as I was hoping.  The app uses window titles to identify windows, but some applications, such as Google Chrome, use a relatively transient string – the webpage title – for the window title.  This means as you switch tabs, Stay may not be able to identify the window to restore its position.

Overall, Stay is a big help in window management when switching between a single monitor and multiple monitors.