.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮
程序员文章站
2022-07-18 16:42:31
前言
icsharpcode.texteditor 是一款非常不错的.net代码编辑控件,内置了多种高亮语言支持,同时完美支持中文,非常赞!
先来看一下运行效果:...
前言
icsharpcode.texteditor
是一款非常不错的.net代码编辑控件,内置了多种高亮语言支持,同时完美支持中文,非常赞!
先来看一下运行效果:
一、项目结构
这里需要注意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#"
,并添加高亮关键字,如下:
这样代码中出现的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#代码:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。