Folding Taggers

The folding taggers are used when you want to create collapsible (foldable) sections of code in a document. The base class responsible for this functionality is the FoldingTaggerBase class with concrete implementations provided out of the box being the BracketFoldingTagger, CSharpFoldingTagger and VisualBasicFoldingTagger.

Figure 1: RadSyntaxEditor with a registered CSharpFoldingTagger

RadSyntaxEditor with a registered CSharpFoldingTagger

FoldingTaggerBase

FoldingTaggerBase is the class responsible for generating the collapsible code regions. You can inherit this class and add FoldingRegionDefinitions to define the start and end matching rules of a collapsible section.

Example 1: Adding FoldingRegionDefinitions

public CustomFoldingTagger(ITextDocumentEditor editor) : base(editor) 
{ 
    this.FoldingRegionDefinitions.Add(new FoldingRegionDefinition("FUNCTION", "END")); 
    this.FoldingRegionDefinitions.Add(new FoldingRegionDefinition("IF", "END IF")); 
} 
In addition, you may also want to override the BuildStartRegionBlockPattern and BuildEndRegionBlockPattern and return a custom regular expression to match the start and end.

Example 2: Default implementation of the BuildStartRegionBlockPattern and BuildEndRegionBlockPattern methods

protected override string BuildStartRegionBlockPattern(Dictionary<string, string> startToEndMap) 
{ 
    // returns \bFUNCTION\b|\bIF\b 
    return string.Join("|", startToEndMap.Keys.Select(x => string.Format("\b{0}\b", x))); 
} 
 
protected override string BuildEndRegionBlockPattern(Dictionary<string, string> startToEndMap) 
{ 
    // returns \bEND\b|\bEND IF\b 
    return string.Join("|", startToEndMap.Values.Select(x => string.Format("\b{0}\b", x))); 
} 
Here's a full list of the overridable methods which you can use in order to create your own custom folding taggers. You can check their default implementations in the various out of the box folding taggers to get a general understanding of how to use them by downloading the source code of the UI for WPF suite.
  • string BuildStartRegionBlockPattern(Dictionary<string, string> startToEndMap): Prepares patters for matching start of collapsible folding section. Default implementation is to join all start strings of provided collapsible sections.
  • string BuildEndRegionBlockPattern(Dictionary<string, string> startToEndMap): Prepares patters for matching end of collapsible folding section. Default implementation is to join all end strings of provided collapsible sections.
  • string GetFoldingRegionTitle(Span foldingSpan): Gets the text displayed in the collapsed folding section. (... by default)
  • string GetFoldingRegionToolTipContent(Span foldingSpan): Gets the folding region's tooltip content.
  • string Span CoerceFoldingSpan(Span defaultFoldingSpan): Coerces the default provided folding span - span starting from the start of the region's start text and ending in the end of the region's end text.
  • string void RebuildFoldingRegions: Rebuilds the list of all collapsible (folding) regions.
  • void InvalidateFoldingRegions: Clears all collapsible (folding) regions and rebuilds them.

See Also

In this article
Not finding the help you need? Improve this article