程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> ICSharpCode.TextEditor如何自定義代碼折疊和高亮,sublimetext3語法高亮

ICSharpCode.TextEditor如何自定義代碼折疊和高亮,sublimetext3語法高亮

編輯:C#入門知識

ICSharpCode.TextEditor如何自定義代碼折疊和高亮,sublimetext3語法高亮


  ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊!先來看一下運行效果:

1 項目結構

 這裡需要注意lib文件夾下導入的類庫,這個Demo需要這些dll.

2 代碼折疊

  需要實現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;
        }
    }
}

3 高亮配置

  拷貝CSharp-Mode.xshd為 JackCSharp-Mode.xshd ,將其中的名字修改為: SyntaxDefinition name = "JackC#",並添加高亮關鍵字,如下:

 這樣代碼中出現的JackWang就會高亮。下面的代碼片段將自定義高亮文件進行加載,並用SetHighlighting進行設置,這裡一定注意目錄下必須有xshd的配置文件,否則高亮將失效。

 1  textEditor.Encoding = System.Text.Encoding.UTF8;
 2  textEditor.Font = new Font("Hack",12);
 3  textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding();
 4  textEditor.Text = sampleCode;
 5 
 6  //自定義代碼高亮
 7  string path = Application.StartupPath+ "\\HighLighting";
 8  FileSyntaxModeProvider fsmp;
 9  if (Directory.Exists(path))
10  {
11      fsmp = new FileSyntaxModeProvider(path);
12      HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
13      textEditor.SetHighlighting("JackC#");
14                 
15 
16  }

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

1         private void TextEditor_TextChanged(object sender, EventArgs e)
2         {
3             //更新,以便進行代碼折疊
4             textEditor.Document.FoldingManager.UpdateFoldings(null, null);
5         }

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved