. NET using Icsharpcode.texteditor to customize folding and highlighting

Source: Internet
Author: User
Preface

ICSharpCode.TextEditoris a very good. NET code editing control, built in a variety of highlighting language support, while the perfect support for Chinese, very good!

Let's take a look at the running effect:

I. Structure of the project

It is important to note that the libraries under the Lib folder are imported, and this demo requires these DLLs.

Second, code folding

You need to implement the Generatefoldmarkers method in Ifoldingstrategy, with the following code:

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 t He 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 use D in this sample.</param>//<returns>a List of foldmarkers.</returns> public list<foldmarker> Generatefoldmarkers (idocument document, String FileName, Object parseinformation) {list<foldmarker> List = new L   Ist<foldmarker> (); Stack FIFO 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 the 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 ' 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, "...")); }//supports nested {} 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, 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 (); Gets the comment text (including spaces) of 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, foldtype.typebody, display));  }} return list; } }}

Three, highlight the configuration

Copy csharp-mode.xshd to jackcsharp-mode.xshd, change the name to: SyntaxDefinition name = "JackC#" , and add the highlighted keyword, as follows:

The Jackwang that appear in the code will be highlighted. The following code snippet loads the custom highlighting file and sets it up with sethighlighting, so note that there must be a xshd configuration file in the directory, otherwise the highlighting will fail.

texteditor.encoding = System.Text.Encoding.UTF8; Texteditor.font = new Font ("Hack", 12); TextEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding (); Texteditor.text = SampleCode; Custom code Highlighting string path = application.startuppath+ "\\HighLighting"; Filesyntaxmodeprovider FSMP; if (directory.exists (path)) {  FSMP = new Filesyntaxmodeprovider (path);  HighlightingManager.Manager.AddSyntaxModeFileProvider (FSMP);  Texteditor.sethighlighting ("jackc#");     }

To keep the code from collapsing in time, listen for text changes as follows:

   private void Texteditor_textchanged (object sender, EventArgs e)   {    //updated for code folding    TextEditor.Document.FoldingManager.UpdateFoldings (null, NULL);   }

Finally, we can define a class of formatting code to format C # code:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.