I have the following code to create syntax highlighting for a text editor that I am working on. It uses the FastColoredTextBox component. I can't quite get the regex pattern for highlighting batch file variables correct.
private void batchSyntaxHighlight(FastColoredTextBox fctb)
{
fctb.LeftBracket = '(';
fctb.RightBracket = ')';
fctb.LeftBracket2 = '\x0';
fctb.RightBracket2 = '\x0';
Range e = fctb.Range;
e.ClearStyle(StyleIndex.All);
//clear style of changed range
e.ClearStyle(BlueStyle, BoldStyle, GrayStyle, MagentaStyle, GreenStyleItalic, BrownStyleItalic, YellowStyle);
//variable highlighting
e.SetStyle(YellowStyle, "(\".+?\"|\'.+?\')", RegexOptions.Singleline);
//comment highlighting
e.SetStyle(GreenStyleItalic, @"(REM.*)");
//attribute highlighting
e.SetStyle(GrayStyle, @"^\s*(?<range>\[.+?\])\s*$", RegexOptions.Multiline);
//class name highlighting
e.SetStyle(BoldStyle, @"(:.*)");
//symbol highlighting
e.SetStyle(MagentaStyle, @"(@|%)", RegexOptions.Singleline);
e.SetStyle(RedStyle, @"(\*)", RegexOptions.Singleline);
//keyword highlighting
e.SetStyle(BlueStyle, @"\b(set|SET|echo|Echo|ECHO|FOR|for|PUSHD|pushd|POPD|popd|pause|PAUSE|exit|Exit|EXIT|cd|CD|If|IF|if|ELSE|Else|else|GOTO|goto|DEL|del)");
//clear folding markers
e.ClearFoldingMarkers();
BATCH_HIGHLIGHTING = true;
}
Using this code I can't seem to highlight strings between two '%' symbols without highlighting almost the entire file because many lines will only contain one '%' symbol or two right next to each other.
I am also having trouble with '::' comments. In order to highlight the labels I have created the regex pattern to match any line that has a ':' in it followed by all characters that proceed it.
I want to get the highlighting correct so that labels will be highlighting BoldStyle and '::' comments will be highlighted GreenItalicStyle without any conflicts. I would also like to be able to highlight strings that lay between two '%' symbols without conflicts (such as a line that contains only one '%')
All this should only be highlighted if not in a comment.
EDIT: Currently the code only highlights '%' symbols by themselves as I was unable to get the code to work for highlighting between them without causing major syntax issues.