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 = ; 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, , FoldType.Region, "..."));
}
//支持嵌套 {}
if (text.Trim().StartsWith("{")) // Look for method starts
{
startLines.Push(i);
}
if (text.Trim().StartsWith("}")) // Look for method endings
{
if (startLines.Count > )
{
int start = startLines.Pop();
list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, , 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 + ).Offset, document.GetLineSegment(start + ).Length);
//remove ///
display = display.Trim().TrimStart('/');
list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, , 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",);
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. NetBeans自定义代码折叠块,类似vs中的#region

    //<editor-fold defaultstate="collapsed" desc="测试代码折叠"> echo '<script ty ...

  2. intelliJ idea代码折叠

    在intelliJ idea中不仅可以对类.方法等结构的代码进行折叠(ctrl+-)还可以自定义折叠代码.intelliJ支持两种风格的自定义代码折叠,如下: visual studio style ...

  3. intelliJ idea #region 代码折叠

    在intelliJ idea中不仅可以对类.方法等结构的代码进行折叠(ctrl+-)还可以自定义折叠代码.intelliJ支持两种风格的自定义代码折叠,如下: visual studio style ...

  4. eclipse自定义代码块折叠

    1.下载插件 com.cb.eclipse.folding_1.0.6.jar 下载地址:http://files.cnblogs.com/haiq/代码折叠插件_com.cb.eclipse.fol ...

  5. 使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器

    使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器 品高工作流 的流程模拟器中使用了一个具有语法高亮和折叠功能的XML编辑器,其核心就是用了SharpDevelop中的 ...

  6. SharpDevelop浅析_4_TextEditor_自动完成、代码折叠……

    SharpDevelop浅析_4_TextEditor_自动完成.代码折叠…… SharpDevelop浅析_4_TextEditor_自动完成.代码折叠…… Parser及其应用: Code Com ...

  7. VIM 代码折叠

    VIM 代码折叠 VIM代码折叠方式可以用"foldmethod"选项来设置,如: set foldmethod=indent 有6种方式来折叠代码 1. manual //手工定 ...

  8. Linux下面对于VIM编辑器的代码折叠使用与screen

    VIM设置代码折叠 1. 折叠方式 可用选项 'foldmethod' 来设定折叠方式:set fdm=*****.有 6 种方法来选定折叠:          manual           手工 ...

  9. vim代码折叠功能

    问题:怎样在vim中实现代码折叠功能? 解决方法:直接使用vim自带的快捷键和命令,便可以实现功能强大的折叠 小试折叠: 1  :set fdm=marker  在vim中执行该命令 2  5G  将 ...

随机推荐

  1. 【POJ3037】Skiing 最短路

    题意: 有个n*m的滑雪场,bessie要从(1,1)滑到(n,m),问最小时间. 起始有一个速度v,然后每从一个点A到一个点B(仅仅能上下左右走,每次一格),速度就会乘上2^(权值A-权值B). 然 ...

  2. Ubuntu Ruby On Rails安装和配置

    在这篇文章中ubuntu通过rvm安装ruby和rails.步借鉴了官方网站和网上信息,这里给大家分享. 1. 安装mapapis公钥: gpg --keyserver hkp://keys.gnup ...

  3. win7兼容oracle

    操作系统:win7,数据库版本:Oracle 10.0. 问题:安装Oracle10.0时,安装程序意外退出,可按照如下操作解决win7与oracle兼容性问题. 1.打开“\Oracle 10G \ ...

  4. leetcode第24题--Reverse Nodes in k-Group

    problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...

  5. 在Ubuntu 12.04 - 64bit中安装CodeSourcery时提示错误

    安装时提示错误,Your 64-bit Linux host is missing the 32-bit libraries requied to install and use Sourcery C ...

  6. javascript 学习总结(二)Array数组

    1.数组常用方法 var colors = ["red", "blue", "green"]; //creates an array wit ...

  7. NYOJ 58 步数最少 【BFS】

    意甲冠军:不解释. 策略:如果: 这个问题也可以用深宽搜索搜索中使用.我曾经写过,使用深层搜索.最近的学校范围内的搜索,拿这个问题来试试你的手. 代码: #include<stdio.h> ...

  8. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  9. Ibatis.Net执行Sql超时commandTimeout的一个坑

    项目中使用了Ibatis.Net,数据库是Mysql,在做一个批量Update的操作时,需要执行40几秒,在执行到30秒的时候,会抛出异常:Timeout expired , The timeout ...

  10. SpringMVC源码

    SpringMVC源码分析系列 说到java的mvc框架,struts2和springmvc想必大家都知道,struts2的设计基本上完全脱离了Servlet容器,而springmvc是依托着Serv ...