RIGHT_THEN, this isn't for AvalonEdit but was for the prior version of the control: ICSharpCode.TextEditor, from the sample TextEditor project. I will switch my editor from the Alsing SyntaxBox to this, and work on fleshing it out over the next few weeks (and will keep any implementation of this interface posted)
- Code: Select all
/// <summary>
/// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy
/// </summary>
public class RegionFoldingStrategy : IFoldingStrategy
{
/// <summary>
/// Generates the foldings for our document.
/// </summary>
/// <param name="document">The current document.</param>
/// <param name="fileName">The filename of the document.</param>
/// <param name="parseInformation">Extra parse information, not used in this sample.</param>
/// <returns>A list of FoldMarkers.</returns>
public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
{
List<FoldMarker> list = new List<FoldMarker>();
Stack<int> startLines = new Stack<int>();
// Create foldmarkers for the whole document, enumerate through every line.
for (int i = 0; i < document.TotalNumberOfLines; i++)
{
var seg = document.GetLineSegment(i);
int offs, end = document.TextLength;
char c;
for (offs = seg.Offset; offs < end && ((c = document.GetCharAt(offs)) == ' ' || c == '\t'); offs++)
{}
if (offs == end)
break;
int spaceCount = offs - seg.Offset;
// now offs points to the first non-whitespace char on the line
if (document.GetCharAt(offs) == '#') {
string text = document.GetText(offs, seg.Length - spaceCount);
if (text.StartsWith("#region"))
startLines.Push(i);
if (text.StartsWith("#endregion") && startLines.Count > 0) {
// Add a new FoldMarker to the list.
int start = startLines.Pop();
list.Add(new FoldMarker(document, start,
document.GetLineSegment(start).Length,
i, spaceCount + "#endregion".Length));
}
}
}
return list;
}
}