ICSharpCode.TextEditor如何自定义代码折叠和高亮
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如何自定义代码折叠和高亮的更多相关文章
- NetBeans自定义代码折叠块,类似vs中的#region
//<editor-fold defaultstate="collapsed" desc="测试代码折叠"> echo '<script ty ...
- intelliJ idea代码折叠
在intelliJ idea中不仅可以对类.方法等结构的代码进行折叠(ctrl+-)还可以自定义折叠代码.intelliJ支持两种风格的自定义代码折叠,如下: visual studio style ...
- intelliJ idea #region 代码折叠
在intelliJ idea中不仅可以对类.方法等结构的代码进行折叠(ctrl+-)还可以自定义折叠代码.intelliJ支持两种风格的自定义代码折叠,如下: visual studio style ...
- eclipse自定义代码块折叠
1.下载插件 com.cb.eclipse.folding_1.0.6.jar 下载地址:http://files.cnblogs.com/haiq/代码折叠插件_com.cb.eclipse.fol ...
- 使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器
使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器 品高工作流 的流程模拟器中使用了一个具有语法高亮和折叠功能的XML编辑器,其核心就是用了SharpDevelop中的 ...
- SharpDevelop浅析_4_TextEditor_自动完成、代码折叠……
SharpDevelop浅析_4_TextEditor_自动完成.代码折叠…… SharpDevelop浅析_4_TextEditor_自动完成.代码折叠…… Parser及其应用: Code Com ...
- VIM 代码折叠
VIM 代码折叠 VIM代码折叠方式可以用"foldmethod"选项来设置,如: set foldmethod=indent 有6种方式来折叠代码 1. manual //手工定 ...
- Linux下面对于VIM编辑器的代码折叠使用与screen
VIM设置代码折叠 1. 折叠方式 可用选项 'foldmethod' 来设定折叠方式:set fdm=*****.有 6 种方法来选定折叠: manual 手工 ...
- vim代码折叠功能
问题:怎样在vim中实现代码折叠功能? 解决方法:直接使用vim自带的快捷键和命令,便可以实现功能强大的折叠 小试折叠: 1 :set fdm=marker 在vim中执行该命令 2 5G 将 ...
随机推荐
- ASP.NET 5 Hello World
ASP.NET 5系列教程 (二):Hello World 本篇文章内容比较基础,主要是向大家展示如何创建一个 ASP.NET 5 工程,主要包含内容如下: 创建ASP.NET 5 工程 添加 T ...
- 【COCOS2DX-游戏开发之三四】cocos2dx 3.0 TableView特殊使用方法:滚动时不能选择等等
cocos2dx 3.0版本号TableView拍生自ScrollView,经常使用来做滚动列表,有几种特殊使用方法,不知道大家用到过没 要求:1.滚动时不能选中TableCell,非滚动状态才干选中 ...
- LibVLC audio controls
原文 http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__audio.html LibVLC audio co ...
- SQLServer通过链接服务器远程删除数据性能问题解决
原文:SQLServer通过链接服务器远程删除数据性能问题解决 在上一遍文章中介绍了SQLServer通过链接服务器访问Oracle性能问题的解决方法,本文介绍链接服务器下远程删除SQLServer数 ...
- javascript 学习总结(三)Boolean对象
Boolean对象 /* 创建 Boolean 对象的语法: new Boolean(value); //构造函数 Boolean(value); //转换函数 参数 value 由布尔对象存放的值或 ...
- JavaScipt中对DOM的理解
一.理解DOM 浏览器通过文档对象模型DOM使JavaScript程序可以访问页面上的元素,而DOM是页面上XHTML中文档正文标题.段落.列表.样式ID.class以及所有其他出现的数据的一个内部表 ...
- Mysql高级之游标
原文:Mysql高级之游标 什么是游标?select 语句也许一次性会取出来n条语句,那么游标便可以一次取出来select中的一条记录.每取出来一条,便向下移动一次!可以实现很复杂逻辑! 上面还有有一 ...
- android 实现分享功能两种方法
当我想做一个智能的记事本的时候,我就在尝试自己写一组分享功能.后来才知道,原来每个社交软件中都有自己的分享接口. 这就大大减少了我们的代码量了. 第一种方法:特点--简单 package com.ex ...
- 如何给TableLayout加边框
呵呵,其实很简单 就是将TableLayout定义一种颜色,在给TableRow定义一种颜色.通过TableRow的layout_margin挤出一个像素的宽度就变成了TableLayout的宽度.这 ...
- VS 文件自动定位功能
在Visual Studio 中,当你在所有打开的文件中进行切换时,在Solution Explorer中也会自定定位到这个文件的目录下面,这个功能用来查找当前文件是非常有用.在Tools->O ...