.NET中用ICSharpCode.TextEditor自訂摺疊與高亮

來源:互聯網
上載者:User
前言

ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控制項,內建了多種高亮語言支援,同時完美支援中文,非常贊!

先來看一下運行效果:

一、項目結構

這裡需要注意lib檔案夾下匯入的類庫,這個Demo需要這些dll.

二、程式碼摺疊功能

需要實現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 先進先出   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#" ,並添加高亮關鍵字,如下:

這樣代碼中出現的JackWang就會高亮。下面的程式碼片段將自訂高亮檔案進行載入,並用SetHighlighting進行設定,這裡一定注意目錄下必須有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#");     }

為了保持代碼適時進行摺疊,這裡監聽文本變化,如下所示:

   private void TextEditor_TextChanged(object sender, EventArgs e)   {    //更新,以便進行程式碼摺疊功能    textEditor.Document.FoldingManager.UpdateFoldings(null, null);   }

最後說明的是,我們可以定義一個格式化代碼的類,來格式化C#代碼:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.