Skip to content

Code Folding ​

TSynEdit can collapse and expand blocks of text (procedures, classes, comments, regions...) with the markers in the gutter, like the IDEs do.

Unit: SynEdit.CodeFolding.

Enabling the folding ​

pascal
SynEdit1.UseCodeFolding := True;
SynEdit1.Highlighter := SynPasSyn1;   // a highlighter that supports folding

The folding ranges are computed by the highlighter. The highlighters that support it derive from TSynCustomCodeFoldingHighlighter: Object Pascal, C/C++, C#, Java, JavaScript, PHP, Perl, Python, Visual Basic, VBScript, COBOL, DWScript, FoxPro, INI, JSON, YAML and Omni. For the other ones, or for special rules, write the ranges with the event OnScanForFoldRanges of the editor.

The CodeFolding property ​

CodeFolding (TSynCodeFolding) controls the appearance:

PropertyDescription
GutterShapeSizeSize of the collapse/expand shape in the gutter (default 9, scaled with the DPI).
CollapsedLineColorColor of the line shown under a collapsed block.
FolderBarLinesColorColor of the lines of the folding bar in the gutter.
ShowCollapsedLineShows a line under the collapsed blocks.
ShowHintMarkShows a hint mark on the collapsed lines (default True).
IndentGuidesShows the indent guides.
IndentGuidesColorColor of the indent guides.

Commands ​

SynEdit1.CollapseAll collapses all the blocks. The other operations are editor commands that can be bound to keys with Keystrokes, or executed with ExecuteCommand:

CommandEffect
ecFoldAll, ecUnfoldAllCollapses / expands all the blocks.
ecFoldNearest, ecUnfoldNearestCollapses / expands the block nearest to the caret.
ecFoldLevel1, ecFoldLevel2, ecFoldLevel3Collapses the blocks of that nesting level.
ecUnfoldLevel1, ecUnfoldLevel2, ecUnfoldLevel3Expands the blocks of that nesting level.

Demo ​

See the Code Folding demo: it opens a C++, a JavaScript and a Python file with the folding enabled.

Released under the MPL 1.1 / GPL 2 dual license.