這篇文章主要介紹了.NET代碼編輯控件ICSharpCode.TextEditor怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供謝家集企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為謝家集眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
先來看一下運(yùn)行效果:
一、項(xiàng)目結(jié)構(gòu)
這里需要注意lib文件夾下導(dǎo)入的類庫,這個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("/// <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); //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就會高亮。下面的代碼片段將自定義高亮文件進(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#"); }
為了保持代碼適時進(jìn)行折疊,這里監(jiān)聽文本變化,如下所示:
private void TextEditor_TextChanged(object sender, EventArgs e) { //更新,以便進(jìn)行代碼折疊 textEditor.Document.FoldingManager.UpdateFoldings(null, null); }
最后說明的是,我們可以定義一個格式化代碼的類,來格式化C#代碼:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.NET代碼編輯控件ICSharpCode.TextEditor怎么用”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
新聞名稱:.NET代碼編輯控件ICSharpCode.TextEditor怎么用
網(wǎng)站地址:http://bm7419.com/article46/pscghg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站改版、網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、網(wǎng)站維護(hù)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)