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
SynEdit1.UseCodeFolding := True;
SynEdit1.Highlighter := SynPasSyn1; // a highlighter that supports foldingThe 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:
| Property | Description |
|---|---|
GutterShapeSize | Size of the collapse/expand shape in the gutter (default 9, scaled with the DPI). |
CollapsedLineColor | Color of the line shown under a collapsed block. |
FolderBarLinesColor | Color of the lines of the folding bar in the gutter. |
ShowCollapsedLine | Shows a line under the collapsed blocks. |
ShowHintMark | Shows a hint mark on the collapsed lines (default True). |
IndentGuides | Shows the indent guides. |
IndentGuidesColor | Color 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:
| Command | Effect |
|---|---|
ecFoldAll, ecUnfoldAll | Collapses / expands all the blocks. |
ecFoldNearest, ecUnfoldNearest | Collapses / expands the block nearest to the caret. |
ecFoldLevel1, ecFoldLevel2, ecFoldLevel3 | Collapses the blocks of that nesting level. |
ecUnfoldLevel1, ecUnfoldLevel2, ecUnfoldLevel3 | Expands 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.
