Code Folding

Code folding allows to hide a group of lines in a text file and to show only a descriptive headline instead. This allows to hide the details and to show the structure of a text file, like the summary of a book.

Folding Methods

There are several methods to specify which lines are to be folded.

1. Syntax-based folding

The editor analyses the source code and selects the lines that can be folded, usually it allows to fold classes, functions, if-then-else statements, ...

Syntax-based folding tends to create too many folds without added value: folding every 'if' and 'while' blocks does not help navigation or readability, except may be, huge 'while' blocks, but anyway, creating such blocks is a very bad programming practice.

This method works only with languages supported by the text editor.

2. Indentation-based folding

The indented blocks of code can be folded.

3. Marker-based folding

The editor inserts markers in the text file to delimit the beginning and the end of the section of text to hide. These markers are usually embedded into comments to prevent a compiler to interpret them.

This method is the most flexible, you can fold any group of lines and specify any descriptive headline instead of just showing the first line. It also works with any kind of source file allowing comments, and fortunately, it is the case with almost all programming languages and markup languages. In addition it works with any kind of text file, not only source code, which is very useful to organize notes.

But this method is also the most painful to use because you must select manually the regions of text to be folded and enter a headline for each one. However the extra effort is worth it.

Navigation Methods

When some text is folded in a folding editor, there is two way to get it.

1. Expanding

The collapsed section is expanded, so you can edit text and collapse the section again when finished. Collapsing and Expanding is usually performed with shortcuts or a button in the left margin.

This is the most common method used by almost 99% of text editors having folding capability.

2. Changing context

Instead of collapsing and expanding sections, this method keeps always folded section collapsed and if you want to view such a section, the current text is replaced by the collapsed text.

This approach transforms a long flat text file into a hierarchy of smaller text blocks.

Folding in Code Browser

Code Browser supports only the marker-based folding and never expands folded sections.

This is a small example in C, the fact() function is folded with just a headline displayed. The implementation detail of the function is hidden an there is only a descriptive headline visible.

#include <stdio.h>

fact(n) - Factorial

int main()
  int n = 5;
  printf("fact(%d) = %d\n", n, fact(n));
  return 0;

There are markers in comments at the beginning and at the end of the function, for the compiler, it is a perfectly valid source code.

#include <stdio.h>

//[of]:fact(n) - Factorial
int fact(int n)
  if( n < 2 )
    return 1;
    return n * fact(n-1);

int main()
  int n = 5;
  printf("fact(%d) = %d\n", n, fact(n));
  return 0;

As fold markers can be nested, it is possible to transform a long flat text file like this one...

into a tree with multiple levels like this...

Once a text file is no longer a flat list of lines but a hierarchy of blocks, it is possible to navigate through the document in different ways.

Code Browser implements two different methods of navigation.

1. Tree View

The window is spiltted in two parts, the left part shows the tree of folded sections and the right part show the content of the selected section:

2. Browser View

This view tries to emulate a Smalltalk Class Browser: each level is displayed in a small pane and the last folded section is displayed underneath:

Advantages of Folding

The code structure is easier to view

A block of code, a function or a class for instance can be sumarized to a single headline.

Higher level document manipulation

As a line can represent a function, a class or any block of text, the editor can manipulate these elements as easily as a single line of text.

Faster navigation

No need to scroll page after page to find something, you navigate through a tree instead of a long flat file. The access to a line tends to be logarithmic instead of linear.

Better programming practices

It encourages better structuration of code and better commenting.