By selectively hiding and showing sections of code, code folding allows you to focus on what’s relevant, while ignoring irrelevant details. Code folding is also a useful way for quickly getting a high-level overview of a large section of code. RubyMine adds custom folds to the standard list of editor code folding features. Custom folds can be used to effectively outline a section of code. In this post, we’ll look at code folding in RubyMine on OS X.
Folding Individual Code Blocks
A code folding toggle icon appears in the left gutter next to blocks of code that can be folded. Press
command + - and
command + + to fold and unfold foldable blocks.
Folding All Code Blocks
Fold and unfold an entire file’s code blocks with
command + shift + - and
command + shift + +.
After folding all the code blocks in a file, successively unfolding with
command + + will unfold to your cursor.
If a section of code does not have a folding toggle icon in the gutter, select it, then fold it with
command + ..
A folded selection appears as an ellipsis. Unfold it with
command + . or
command + +.
Custom Folding Regions
A custom folding region can be used to describe or outline a section of code. RubyMine uses a special Ruby comment for custom folding regions.
To create a custom folding region, first select the section of code, then press
command + option + T and choose “<editor-fold…> Comments” or “region…endregion Comments” from the “Surround With” dialog.
Finally, provide a description for the custom folding region.
You’ll have to manually create this comment if RubyMine doesn’t give you either comment option in the “Surround With” dialog.
Navigating By Custom Folding Regions
Navigate to a custom folding region with
command + option + ..
RubyMine can be configured to automatically fold certain code constructs, e.g., methods. To set these preferences, search for “code folding” in the settings dialog,
command + ,.
Don’t Forget About the Details
Not every developer is a fan of code folding. Folds are often used to hide long, ugly, or complicated sections of code. Once hidden, the code rots; out of sight, out of mind. On the other hand, hiding code behind folds keeps you focused, preventing unnecessary refactoring tangents. Like any technique, code folding has its pros and cons, try it out and see if it works for you.
About the Author