媒介
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#代码:
总结
【相干引荐】
1. ASP.NET免费视频教程
2. ASP.NET教程
3. 极客学院ASP.NET视频教程
以上就是细致引见一款.NET代码编辑控件(ICSharpCode.TextEditor)的细致内容,更多请关注ki4网别的相干文章!