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 columnlist.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的配置文件,否则高亮将失效。

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#代码:

ICSharpCode.TextEditor如何自定义代码折叠和高亮相关推荐

  1. code blocks 代码颜色_vuepress中实现代码折叠、高亮

    最近在vuepress中撰写UI框架文档时发现在组件中插入演示代码没高亮,虽然在文档markdown中写代码有高亮但就无法实现折叠了,而且vuepress没有提供折叠代码的配置,因此实现一个折叠组件外 ...

  2. 代码编辑器控件ICSharpCode.TextEditor的关键字高亮以及代码折叠功能

    SharpDevelop (#develop)有很多"副产品",其中最出名的应算SharpZipLib (#ziplib),纯C#的ZIP类库, 而在SharpDevelop (# ...

  3. 最完整的!WinForm使用ICSharpCode.TextEditor控件实现自定义高亮显示

    1.此案例以WinForm为例,首先下载个ICSharpCode.TextEditor.dll文件(官方版本不报错,下载地址:https://download.csdn.net/download/ki ...

  4. WPF 用 AvalonEdit 开发简单的代码编辑器 支持高亮自动提示

    用 WPF 开发一个代码编辑器的难度很低,因为行业里面有很多小伙伴开发过,这些小伙伴将自己的代码开源了,发布到 NuGet 上,所以让我开发一个代码编辑器的难度实在太低.在不要有奇特的要求情况下,通过 ...

  5. ACE editor 在线代码编辑极其高亮

    简介 ACE是一个开源的.独立的.基于浏览器的代码编辑器,可以嵌入到任何WEB页面或者JavaScript应用程序中,ACE支持超过60种语言语法高亮,并能够处理代码多达400万行的大型文档. 特性 ...

  6. WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置

    WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置 WebStorm是一个捷克公司开发的,功能虽然很强大,但UI貌似一直不是东欧人的强项.WebStorm默认的编辑器颜色搭配不算讲究,我看 ...

  7. AvalonEdit C#代码折叠策略类

    背景 AvalonEdit是一个基于WPF的开源.可扩展文本编辑器,用于代码的编辑显示,还可实现语法高亮.代码折叠.自动补全等功能,在需要显示或编辑代码的应用程序中确是一个不错的选择.然美中不足的是这 ...

  8. ICSharpCode.TextEditor 实现获取光标所在行,在某行或某字符 处插入文本

    大家好,我是赤道上的小熊,最近有个项目需要实现代码编辑器的功能,于是找到了ICSharpCode.TextEditor这个控件.该控件能对编写的代码实现高亮特殊字符,格式化代码,感觉还是比较好用的.在 ...

  9. 让Visual Studio 也支持JS代码折叠 [ Visual Studio | #region | #endregion ]

    前言 Visual Studio的代码折叠功能非常好用,#region #endregion 这个词连搜狗的词库里面都出现了(不含'#'号),可见使用频率很高,但是他不支持js的代码折叠 : ( 最近 ...

最新文章

  1. pip install scrpy 报错: command 'gcc' failed with exit status 1
  2. 公司裁员优先裁掉这类员工
  3. Set的实现类HashSet与TressSet代码实现
  4. 隐藏 console 窗口
  5. Atitit 提升效率 界面gui方面的前后端分离与cbb体系建设 规范推荐标准
  6. 单片机交通灯灯c语言程序,关于LED模拟交通灯单片机C语言程序设计
  7. vue 后台系统引入pdf安装包
  8. excel隐藏行的展现方法
  9. Fortran common
  10. 利息浅谈(五)——我的投资收益率怎么算?
  11. 安装单片机开发工具Keil5和仿真开发板proteus
  12. 技术岗的职业规划_技术人员职业发展规划
  13. 2021-11-03 投资学实务翻译9.1和9.2
  14. JCA-Java加密框架
  15. 管中窥豹,深入总部调研苏宁易购研发
  16. 工作流审批平台-审批流程-审批权限
  17. AT指令(中文详解版)
  18. 用深度学习识别海洋生物?飞桨轻松完成挑战!
  19. 免费的外文文献搜索下载工具推荐
  20. ”去他丫的北上广,老子要去成都定居了!“一名33岁老码农有话说

热门文章

  1. python的高级应用
  2. 远程调试为何要亲历现场,也许也可以这样解决
  3. 施迈赛IFL 50-385-11P-M20感应式接近开关
  4. iMobile中灵活运用图层,你知多少
  5. Java老鸟5年压箱底面试真题资料+自学手册,秋招拿下阿里45k offer
  6. 蓝桥杯python组—— 猴子分香蕉
  7. JS定时器setTimeout和setInterval介绍
  8. 夺命雷公狗ThinkPHP项目之----商城10商品属性管理
  9. 小白的cannot get interface MTU on 'br1':no such device处理方案
  10. Unreal5 第三人称射击游戏 射击功能实现1