0
votes

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.

1
For the % issue, is that the MagentaStyle? Also, just to clarify, you want the percentages to be highlighted if there is something between them and both percentages have to be on the same line?Doug F
@DougF Yes that is correct.Zach Pedigo
OK, if that's the case, I did a regex at regex101.com/r/tdgSAZ/1 if you want to check it out to see if it works for you. If it doesn't, let me know what's wrong and I'll give it another shot.Doug F
@DougF Tried it, it just highlighted almost the entire file because one of the lines at the top contains just one '%'.Zach Pedigo
@ZachPedigo OK, I think I see where I went wrong. This time I'm doing this dotnetfiddle at dotnetfiddle.net/AAHh0U and I think this will work for you. I had to use RegexOptions.Multiline. Give it a go. Sorry for all the back and forth.Doug F

1 Answers

0
votes

Big thanks to @DougF for helping me find this solution. The answer is:

@"^:[a-zA-Z]+"