Code folding

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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 amounts of text while viewing only those subsections of the text that are specifically relevant at any given time.

This feature is commonly used by developers to manage source code files.

Text folding is a similar feature used in folding editors and outliners. Text folding is generally distinguishable from code folding in that the latter tends to be used with the specific syntax of markup languages or programming languages, whereas the former can be used with ordinary text. Another variation, called "data folding", is implemented in some hex editors and is used to structure a binary file or hide inaccessible data sections in a RAM-editor.[1]

Consequently, the term "code folding" is only one among various other common terms for this feature. Related terms include "expand and collapse", "code hiding", "outlining" and others.

Conventions[edit]

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 overridden or augmented by the user.

Folding points are usually specified with one or more of the following conventions. Each of these conventions has its own distinct advantages and difficulties, and it is essentially up to the developers who create the text editor software to decide which conventions to follow. Text editors that provide support for multiple folding conventions typically allow the user to choose which is most appropriate for the file being edited.

Token-based[edit]

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. The most common delimiter tokens are {{{ to begin the folded section, and }}} to end it.

Indentation-based[edit]

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 such as Python and text files that require indentation as a rule by themselves.

Syntax-dependent[edit]

Syntax-dependent folding points are those that rely on the content of the file being edited 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.

User-specified[edit]

User-specified folding typically allows the user to fold sections of text using a generic selection method. Folded text might be anonymous or named.

Examples[edit]

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

 Heading 1
 {{{
 Body
 }}}

 Heading 2
 {{{
 Body
 }}}

 Heading 3
 {{{
 Body
 }}}

When loaded into a folding editor, the outline structure will be shown:

 Heading 1
 {{{ ...

 Heading 2
 {{{ ...

 Heading 3
 {{{ ...

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

Software with code folding capability[edit]

(Please insert in alphabetical order)

Token Indentation Syntax User
Ajax.org Cloud9 Editor Yes Yes Yes ?
AkelPad ? ? Yes ?
Anjuta IDE ? Yes Yes ?
BBEdit ? ? Yes ?
Code::Blocks IDE Yes Yes Yes Yes
Delphi IDE Yes ? Yes ?
Dreamweaver ? ? ? Yes
Eclipse ? ? Yes ?
EditPlus No Yes No No
Emacs Yes[2] ?[3] Yes[4] Yes[5]
EmEditor Professional ? Yes Yes ?
FlashDevelop IDE ? ? Yes ?
geany ? ? Yes ?
gedit Yes Yes Yes ?
ISPF ? Yes ? Yes
JED Yes Yes[6] ? No
jEdit Yes Yes Yes Yes
Kate Yes Yes Yes Yes
MATLAB No No Yes No
Ms Visual Studio Yes Yes Yes Yes
NetBeans IDE Yes Yes Yes Yes
Notepad++ ? Yes Yes Yes
NuSphere PHPEd ? ? Yes Yes
Qt Creator ? ? Yes ?
SciTE Yes Yes Yes ?
STET[7] ? ? ? ?
Sublime Text No Yes No Yes
TextMate Yes Yes Yes Yes
UltraEdit No No Yes Yes
Vim Yes Yes Yes Yes
Xcode Yes Yes Yes Yes
Zend Studio ? ? ? ?

Notes[edit]

  1. ^ "Data folding in HxD hex editor (listed as feature of RAM-Editor)". Retrieved 2007-04-30. 
  2. ^ Token-based folding is impemented by the folding minor mode. One can also use outline and allout minor modes for sectioning program sources.
  3. ^ One can use the set-selective-display function in Emacs to hide lines based on the indentation level, as suggested in the Universal code folding note.
  4. ^ Syntax-dependent folding is supported by the outline and allout modes for special dedicated outline-syntaxes; by the hideshow minor mode for some programming languages; also, by the semantic-tag-folding minor mode and the senator-fold-tag command for syntaxes supported by semantic (a component of CEDET), as well as by doc-mode for JavaDoc or Doxygen comments, by TeX-fold-mode, sgml-fold-element command, nxml-outln library in the corresponding language-specific modes, and possibly in other modes for particular syntaxes. Sometimes, the standard simple outline minor mode is used to simulate syntax-based folding, cf. the use of it in properly indented Emacs Lisp source code, the use of it (see near the end of the page) for properly indented HTML. Several folding mechanisms are unified by the fold-dwim interface. See also CategoryHideStuff.
  5. ^ Folding of user-selected regions in Emacs is implemented by the hide-region-hide command.
  6. ^ The set_selective_display function may be used to hide lines indented beyond a specified amount.
  7. ^ STET may have been the first text editor that supported folding[citation needed]

See also[edit]

External links[edit]