using
ICSharpCode.TextEditor.Document;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
JackWangCUMT.WinForm
{
/// <summary>
/// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy
/// </summary>
public
class
MingFolding : 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>();
var
startLines =
new
Stack<
int
>();
for
(
int
i = 0; i < document.TotalNumberOfLines; i++)
{
string
text = document.GetText(document.GetLineSegment(i));
if
(text.Trim().StartsWith(
"#region"
))
{
startLines.Push(i);
}
if
(text.Trim().StartsWith(
"#endregion"
))
{
int
start = startLines.Pop();
list.Add(
new
FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.Region,
"..."
));
}
if
(text.Trim().StartsWith(
"{"
))
{
startLines.Push(i);
}
if
(text.Trim().StartsWith(
"}"
))
{
if
(startLines.Count > 0)
{
int
start = startLines.Pop();
list.Add(
new
FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody,
"...}"
));
}
}
if
(text.Trim().StartsWith(
"/// <summary>"
)) // Look
for
method starts
{
startLines.Push(i);
}
if
(text.Trim().StartsWith(
"/// <returns>"
)) // Look
for
method endings
{
int
start = startLines.Pop();
string
display = document.GetText(document.GetLineSegment(start + 1).Offset, document.GetLineSegment(start + 1).Length);
display = display.Trim().TrimStart(
'/'
);
list.Add(
new
FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, display));
}
}
return
list;
}
}
}