Code folding

From Wikipedia, the free encyclopedia

Code folding is a feature of some text editors, source code editors and IDEs that allows the user to selectively hide and display sections of a currently-edited file as a part of routine edit operations. This allows the user to manage large regions of potentially complicated text within one window, while still viewing only those subsections of the text that are specifically relevant during a particular editing session.

This type of feature is customarily popular among developers who routinely manage a high volume and variety of source code files. Nonetheless, text editors that include this feature often provide enough flexibility to allow uses for purposes other than source code management. Consequently, the term 'Code Folding' is only one among various other common terms for this feature. Terms such as 'Outlining' 'Code Hiding' 'Expand and Collapse' (among others) are also common.

Contents

[edit] Conventions

In order to support code folding, the text editor must provide a mechanism for identifying 'folding points' within a text file. Some text editors provide this mechanism automatically, while others provide defaults that can either be overidden or augmented by the user.

Generally, folding points are specified with one or more of the following conventions:

  • token-based
  • indentation-based
  • syntax-dependent

Token-based folding points are specified using special delimiters that serve no other purpose in the text than to identify the boundaries of folding points. This convention can be compared to indentation-based folding points, where printable characters are used instead of whitespace.

Indentation-based folding points are generally specified by the position and sequence of non-printing whitespace (such as tabs and spaces) within the text. This convention is particularly suitable to syntaxes and text files that require indentation as a rule by themselves.

Syntax-dependent folding points are those that rely on the specific source code or programming language of the currently-edited file in order to specify where specific folding regions should begin and end. Syntax-based folding points are typically defined around any or all of the standard sub-features of the markup language or programming language in use.

Each of these conventions has its own distinct advantages and difficulties, and it is essentially up to the developer(s) who create the text editor software to decide which conventions to follow (if any).

[edit] Example: token-based code folding

The following document contains folding marks ({{{ ... }}}):

 Heading 1
 {{{
 Body
 }}}

 Heading 2
 {{{
 Body
 }}}

 Heading 3
 {{{
 Body
 }}}

When loaded into a folding editor, the outline structure will be shown (just the headings):

 Heading 1
 {{{ ...

 Heading 2
 {{{ ...

 Heading 3
 {{{ ...

Usually clicking on the {{{ marks makes the appropriate body text appear.

[edit] Software with this feature

Recent versions of the open-source text editor Vim, Emacs, and the Java IDE Eclipse offer highly-configurable support for code folding. EditPad Pro supports code folding through fully editable file navigation schemes. Sun's NetBeans IDE, Microsoft's Visual Studio.NET, the Code::Blocks IDE, the UltraEdit text editor, the Zeus IDE , the Mac OS X editors TextMate and BBEdit, the KDE text editor Kate, Macromedia Dreamweaver, The cross-platform SciTE editor based on the Scintilla editing component, the GNOME IDE Anjuta and the general source code editor Notepad++ offer code folding as well.

[edit] See also

In other languages