Code Folding

The Wolfram Language Plugin supports three different kinds of code folding:

  • Inside code to collapse the body of Module, Block, etc. but also lists and other constructs
  • Sectioning comments to structure your package into logical regions
  • Named characters to collapse, e.g. \[Alpha] into α

Code folding helps to improve readability while you are working by hiding the contents of larger portions of code you are currently not working on. It needs to be mentioned that this doesn’t change any code; it merely affects the representation of your code inside the editor.

To collapse or expand folding regions, it is possible to use the menu entries und Code | Folding or keyboard shortcuts like Ctrl+.. More details can be found in the official IDEA documentation.

Folding code

Assume you need the first 100 digits of pi, and you stored these in a list inside your code.

New File

To hide the contents of this long list, you can collapse it into a compressed form.

New File

This kind of folding code is supported for lists and all Wolfram functions that work as scoping constructs with local variables like Module, With, Block, Table, Do, and many more.

Folding section comments

The Wolfram Language has a dedicated way to structure package-files into logical parts by inserting sectioning comments. Comment sections have a specific form and like in text documents, they can be used to start chapters, sections, subsections, etc. Such comments always have the section specifier in one line with nothing else inside the comment, and the descriptive text in the next line. A simple example looks like this:

New File

These sections act as folding region, and you can collapse everything between the beginning of a section and the start of the next section, including possible sub-sections, sub-sub-sections or text. We can collapse the first section by using Ctrl+., clicking on the - indicator on the left side of the editor, or using menu entries.

New File

Now, it only shows the title of the section hiding its entire content, and note how the indicator on the left side changed into a small +. All these special comments work perfectly within Mathematica and are displayed as appropriate text cells. Therefore, when opening the short example in the Mathematica front end, it looks like this

New File

It goes without saying that this gives you a powerful tool to create readable, structured, and maintainable packages.

Folding named characters

Named characters in Mathematica are special glyphs like λ that are represented using the form \[Lambda] in ASCII package code. The Wolfram Language Plugin supports the correct rendering of a subset of the named characters like \[Lambda] (λ) or \[Alpha] (α) that are available in Mathematica.

In general, package code should only consist of ASCII characters, but since it is sometimes unavoidable to have, e.g. Greek characters inside a Manipulate, the Wolfram Language Plugin can display \[Alpha] as if it where a unicode α. This is realized using mini folding regions for each character. Therefore, each \[XXX] can be collapsed into its unicode representation or later expanded back into its \[XXX] form.

Note that the Wolfram Language Plugin only supports named characters that can be part of symbols like \[Alpha] and are not operators like \[Rule]. Therefore, please use Rule[a,b] or a->b and not a\[Rule]b!

One of the reasons for the lack of support is that Mathematica still has no official syntax specification that lays out all available operators with their exact precedences and associativity. Another important reason is that many unicode operators in Mathematica can only be displayed with Wolfram’s fonts and have no public representation. The code points of these characters are in the private use area of unicode and the rendered \[Rule] arrow is one example: It has the unicode point U+F522 and while inside Mathematica a \:f522 b is instantly converted into a nicely rendered rule, the glyph is only available in Wolfram’s own fonts.