.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮
前言
ICSharpCode.TextEditor 是一款非常不錯(cuò)的.NET代碼編輯控件,內(nèi)置了多種高亮語(yǔ)言支持,同時(shí)完美支持中文,非常贊!
先來(lái)看一下運(yùn)行效果:

一、項(xiàng)目結(jié)構(gòu)

這里需要注意lib文件夾下導(dǎo)入的類庫(kù),這個(gè)Demo需要這些dll.
二、代碼折疊
需要實(shí)現(xiàn)IFoldingStrategy中的 GenerateFoldMarkers 方法,代碼如下:
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 先進(jìn)先出
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, "..."));
}
//支持嵌套 {}
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("http:/// <summary>")) // Look for method starts
{
startLines.Push(i);
}
if (text.Trim().StartsWith("http:/// <returns>")) // Look for method endings
{
int start = startLines.Pop();
//獲取注釋文本(包括空格)
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;
}
}
}
三、高亮配置
拷貝CSharp-Mode.xshd為 JackCSharp-Mode.xshd ,將其中的名字修改為: SyntaxDefinition name = "JackC#" ,并添加高亮關(guān)鍵字,如下:

這樣代碼中出現(xiàn)的JackWang就會(huì)高亮。下面的代碼片段將自定義高亮文件進(jìn)行加載,并用SetHighlighting進(jìn)行設(shè)置,這里一定注意目錄下必須有xshd的配置文件,否則高亮將失效。
textEditor.Encoding = System.Text.Encoding.UTF8;
textEditor.Font = new Font("Hack",12);
textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding();
textEditor.Text = sampleCode;
//自定義代碼高亮
string path = Application.StartupPath+ "\\HighLighting";
FileSyntaxModeProvider fsmp;
if (Directory.Exists(path))
{
fsmp = new FileSyntaxModeProvider(path);
HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
textEditor.SetHighlighting("JackC#");
}
為了保持代碼適時(shí)進(jìn)行折疊,這里監(jiān)聽(tīng)文本變化,如下所示:
private void TextEditor_TextChanged(object sender, EventArgs e)
{
//更新,以便進(jìn)行代碼折疊
textEditor.Document.FoldingManager.UpdateFoldings(null, null);
}
最后說(shuō)明的是,我們可以定義一個(gè)格式化代碼的類,來(lái)格式化C#代碼:


總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
.net下實(shí)現(xiàn)Word動(dòng)態(tài)填加數(shù)據(jù)打印
.net下實(shí)現(xiàn)Word動(dòng)態(tài)填加數(shù)據(jù)打印...2007-04-04
ASP.NET2.0緩存(Cache)技術(shù)深入理解
緩存技術(shù)是ASP.NET2.0非常重要的一個(gè)特性,它提供了一種非常好的本地?cái)?shù)據(jù)緩存機(jī)制,從而有效的提高數(shù)據(jù)訪問(wèn)的性能2012-11-11
詳解ASP.NET Core應(yīng)用中如何記錄和查看日志
本篇文章主要介紹了ASP.NET Core應(yīng)用中如何記錄和查看日志,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
Asp.Net Core輕量級(jí)Aop解決方案:AspectCore
這篇文章主要介紹了Asp.Net Core輕量級(jí)Aop解決方案:AspectCore,需要的朋友可以參考下2017-06-06

