前言

ICSharpCode.TextEditor 是一款非常不错的.NET代码编辑控件,内置了多种高亮语言支持,同时完美支持中文,非常赞!

先来看一下运行效果:

.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮

一、项目结构

.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮

这里需要注意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#" ,并添加高亮关键字,如下:

.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮

这样代码中出现的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#代码:

.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮

.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?