This article is mainly introduced to you. NET in the use of Icsharpcode.texteditor custom code folding and highlighting the relevant data, the text through the sample code and pictures introduced in very detailed, the need for friends can refer to, let's look at the following together.
Objective
ICSharpCode.TextEditoris a very good. NET code editing control, built in a variety of highlighting language support, while the perfect support for Chinese, very good!
Let's take a look at the running effect:
I. Structure of the project
It is important to note that the libraries under the Lib folder are imported, and this demo requires these DLLs.
Second, code folding
You need to implement the Generatefoldmarkers method in Ifoldingstrategy, with the following code:
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> ();
// stack FIFO
var startLines = new Stack <int> ();
// Create foldmarkers for the whole document, enumerate through every line.
for (int i = 0; i <document.TotalNumberOfLines; i ++)
{
// Get the text of current line.
string text = document.GetText (document.GetLineSegment (i));
if (text.Trim (). StartsWith ("# region")) // Look for method starts
{
startLines.Push (i);
}
if (text.Trim (). StartsWith ("# endregion")) // Look for method endings
{
int start = startLines.Pop ();
// Add a new FoldMarker to the list.
// document = the current document
// start = the start line for the FoldMarker
// document.GetLineSegment (start) .Length = the ending of the current line = the start column of our foldmarker.
// i = The current line = end line of the FoldMarker.
// 7 = The end column
list.Add (new FoldMarker (document, start, document.GetLineSegment (start) .Length, i, 57, FoldType.Region, "..."));
}
// Support nesting ()
if (text.Trim (). StartsWith ("{")) // Look for method starts
{
startLines.Push (i);
}
if (text.Trim (). StartsWith ("}")) // Look for method endings
{
if (startLines.Count> 0)
{
int start = startLines.Pop ();
list.Add (new FoldMarker (document, start, document.GetLineSegment (start) .Length, i, 57, FoldType.TypeBody, "...}"));
}
}
// /// <summary>
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 ();
// Get comment text (including spaces)
string display = document.GetText (document.GetLineSegment (start + 1) .Offset, document.GetLineSegment (start + 1) .Length);
// remove ///
display = display.Trim (). TrimStart ('/');
list.Add (new FoldMarker (document, start, document.GetLineSegment (start) .Length, i, 57, FoldType.TypeBody, display));
}
}
return list;
}
}
}
Three, highlight the configuration
Copy csharp-mode.xshd to jackcsharp-mode.xshd, change the name to:SyntaxDefinition name = "JackC#", and add the highlighted keyword, as follows:
The Jackwang that appear in the code will be highlighted. The following code snippet loads the custom highlighting file and sets it up with sethighlighting, so note that there must be a xshd configuration file in the directory, otherwise the highlighting will fail.
textEditor.Encoding = System.Text.Encoding.UTF8;
textEditor.Font = new Font ("Hack", 12);
textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding ();
textEditor.Text = sampleCode;
// Custom code highlighting
string path = Application.StartupPath + "\\ HighLighting";
FileSyntaxModeProvider fsmp;
if (Directory.Exists (path))
{
fsmp = new FileSyntaxModeProvider (path);
HighlightingManager.Manager.AddSyntaxModeFileProvider (fsmp);
textEditor.SetHighlighting ("JackC #");
}
To keep the code from collapsing in time, listen for text changes as follows:
private void TextEditor_TextChanged (object sender, EventArgs e)
{
// Update for code folding
textEditor.Document.FoldingManager.UpdateFoldings (null, null);
}
Finally, we can define a class of formatting code to format C # code:
Summarize
"Recommended"
1. ASP. NET free Video Tutorials
2. ASP. NET Tutorials
3. Geek Academy ASP. NET Video Tutorial