How does ICSharpCode. TextEditor customize code folding and highlighting and sublimetext3 syntax highlighting?

Source: Internet
Author: User

How does ICSharpCode. TextEditor customize code folding and highlighting and sublimetext3 syntax highlighting?

ICSharpCode. TextEditor is a very good. NET code editing control. It has built-in support for multiple highlight languages and supports Chinese characters perfectly! Let's take a look at the running effect:

1. Project Structure

Note the imported class libraries in the lib folder. These dll files are required for this Demo.

2 code folding

To implement the GenerateFoldMarkers method in IFoldingStrategy, the Code is as follows:

Using ICSharpCode. textEditor. document; 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 first-in-first-out 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 ,"... ");} // supports nesting {} 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 (); // obtain the comment text (including spaces) 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 ;}}}

3. Highlight Configuration

Copy the CSharp-Mode.xshd to the JackCSharp-Mode.xshd, change the name to: SyntaxDefinition name = "JackC #", and add the highlighted keyword, as shown below:

In this way, JackWang in the code will be highlighted. The following code snippet loads the custom highlight file and uses SetHighlighting for setting. Note that the xshd configuration file must exist in the directory. Otherwise, the highlighted file will become invalid.

1 textEditor. encoding = System. text. encoding. UTF8; 2 textEditor. font = new Font ("Hack", 12); 3 textEditor. document. foldingManager. foldingStrategy = new JackWangCUMT. winForm. mingFolding (); 4 textEditor. text = sampleCode; 5 6 // custom code highlight 7 string path = Application. startupPath + "\ HighLighting"; 8 FileSyntaxModeProvider fsmp; 9 if (Directory. exists (path) 10 {11 fsmp = new FileSyntaxModeProvider (path); 12 HighlightingManager. manager. addSyntaxModeFileProvider (fsmp); 13 textEditor. setHighlighting ("JackC #"); 14 15 16}

To keep the code folded at the right time, the text changes are monitored as follows:

1 private void TextEditor_TextChanged (object sender, EventArgs e) 2 {3 // update for code folding 4 textEditor. Document. FoldingManager. UpdateFoldings (null, null); 5}

Finally, we can define a class for 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.