Use ICSharpCode. TextEditor in. NET to customize code folding and highlighting, and use MATLAB editor to highlight
Preface
ICSharpCode.TextEditor
It is a very good. NET code editing control with built-in support for multiple highlight languages and perfect support for Chinese characters!
Let's take a look at the running effect:
I. project structure
Note the imported class libraries in the lib folder. These dll files are required for this Demo.
Ii. 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 a JackCSharp-Mode.xshd and change its name:SyntaxDefinition name = "JackC#"
And add the highlighted keywords, 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.
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 highlight 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 folded at the right time, the text changes are monitored as follows:
Private void TextEditor_TextChanged (object sender, EventArgs e) {// update for code folding textEditor. Document. FoldingManager. UpdateFoldings (null, null );}
Finally, we can define a class for formatting code to format C # code:
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.