Skip to content

Commit 601cde3

Browse files
authored
Batch built-in highlighter added. (PavelTorgashov#271)
1 parent 624caca commit 601cde3

10 files changed

Lines changed: 4408 additions & 4249 deletions

Binary/FastColoredTextBox.dll

4.5 KB
Binary file not shown.

Binary/FastColoredTextBox.xml

Lines changed: 3322 additions & 3301 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Binary/Tester.exe

-1 KB
Binary file not shown.

FastColoredTextBox/SyntaxHighlighter.cs

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,39 @@ public class SyntaxHighlighter : IDisposable
2626
public readonly Style CrimsonStyle= new TextStyle(Brushes.Crimson, null, FontStyle.Regular);
2727
public readonly Style OrangeStyle= new TextStyle(Brushes.Orange, null, FontStyle.Regular);
2828
public readonly Style DodgerBlueStyle= new TextStyle(Brushes.DodgerBlue, null, FontStyle.Regular);
29-
29+
public readonly Style BlackItalicStyle = new TextStyle(Brushes.Black, null, FontStyle.Italic);
30+
public readonly Style RedBkgdYellowStyle = new TextStyle(Brushes.Red, Brushes.Yellow, FontStyle.Regular);
31+
public readonly Style RedBoldStyle = new TextStyle(Brushes.Red, null, FontStyle.Bold);
32+
public readonly TextStyle GreenStyleItalic = new TextStyle(Brushes.Green, null, FontStyle.Italic);
33+
public readonly TextStyle LightBlueStyle = new TextStyle(Brushes.RoyalBlue, null, FontStyle.Regular);
3034
//
3135
protected readonly Dictionary<string, SyntaxDescriptor> descByXMLfileNames =
3236
new Dictionary<string, SyntaxDescriptor>();
3337

3438
protected readonly List<Style> resilientStyles = new List<Style>(5);
3539

40+
protected Regex BatchFileStringRegex1;
41+
42+
protected Regex BatchFileVariableRegex1,
43+
BatchFileVariableRegex2;
44+
45+
protected Regex BatchFileAttrRegex;
46+
47+
protected Regex BatchFileClassNameRegex;
48+
49+
protected Regex BatchFileSymbolRegex1,
50+
BatchFileSymbolRegex2,
51+
BatchFileSymbolRegex3;
52+
53+
protected Regex BatchFileKeywordRegex1,
54+
BatchFileKeywordRegex2,
55+
BatchFileKeywordRegex3;
56+
57+
protected Regex BatchFileOutKeyRegex;
58+
59+
protected Regex BatchFileCommentRegex1,
60+
BatchFileCommentRegex2;
61+
3662
protected Regex CSharpAttributeRegex,
3763
CSharpClassNameRegex;
3864

@@ -195,6 +221,9 @@ public virtual void HighlightSyntax(Language language, Range range)
195221
case Language.Assembly:
196222
AssemblySyntaxHighlight(range);
197223
break;
224+
case Language.Batch:
225+
BatchFileSyntaxHighlight(range);
226+
break;
198227
default:
199228
break;
200229
}
@@ -730,6 +759,15 @@ public void InitStyleSchema(Language lang)
730759
KeywordStyle = DodgerBlueStyle;
731760
ClassNameStyle = BlueStyle;
732761
break;
762+
case Language.Batch:
763+
StringStyle = BlackItalicStyle;
764+
CommentStyle = GreenStyleItalic;
765+
KeywordStyle = BlueStyle;
766+
VariableStyle = OrangeStyle;
767+
BatchSymbolStyle1 = MagentaStyle;
768+
BatchSymbolStyle2 = RedStyle;
769+
BatchSymbolStyle3 = RedBoldStyle;
770+
break;
733771
}
734772
}
735773

@@ -1453,7 +1491,72 @@ public virtual void AssemblySyntaxHighlight(Range range)
14531491
*/
14541492
}
14551493

1494+
protected void InitBatchFileRegex()
1495+
{
1496+
BatchFileStringRegex1 = new Regex("(\".+?\"|\'.+?\')", RegexOptions.Singleline);
1497+
1498+
BatchFileVariableRegex1 = new Regex(@"(?<!(^(?i)(rem|::).*))(?i)(%[a-zA-Z0-9]+?%|!.+?!)", RegexOptions.Multiline);
1499+
BatchFileVariableRegex2 = new Regex(@"(%%)(?:(?i:~[fdpnxsatz]*(?:\\$PATH:)?)?[a-zA-Z])");
1500+
1501+
BatchFileAttrRegex = new Regex(@"^\s*(?<range>\[.+?\])\s*$", RegexOptions.Multiline);
1502+
1503+
BatchFileClassNameRegex = new Regex(@"^:[a-zA-Z0-9!@#$%^&*()_]+", RegexOptions.Multiline);
1504+
1505+
BatchFileSymbolRegex1 = new Regex(@"^(@)(?=(?i)echo)", RegexOptions.Multiline);
1506+
BatchFileSymbolRegex2 = new Regex(@"(\*)", RegexOptions.Singleline);
1507+
BatchFileSymbolRegex3 = new Regex(@"(?<!(^(?i)(rem|::).*))(?i)(>|<|&)", RegexOptions.Multiline);
14561508

1509+
// Command keywords
1510+
BatchFileKeywordRegex1 = new Regex(@"(?<!(^(?i)(rem|::|echo).*))(?i)(goto|do|cd|start)", RegexOptions.Multiline);
1511+
// Standard keywords
1512+
BatchFileKeywordRegex2 = new Regex(@"^([ ]{0,}|@)?\b(?i)(arp|assoc|at|attrib|aux|bcdedit|break|cacls|call|cd|chcp|chdir|chkdsk|chkntfs|choice|cipher|clip|cls|cmd|cmdextversion|color|com|com1|com2|com3|com4|comp|compact|con|convert|copy|ctty|date|defined|del|dir|diskcomp|diskpart|do|doskey|dpath|driverquery|echo|else|endlocal|equ|erase|errorlevel|exist|exit|expand|fc|find|findstr|for|forfiles|format|fsutil|ftype|geq|goto|gpresult|graftabl|gtr|help|icacls|if|in|ipconfig|label|leq|lpt|lpt1|lpt2|lpt3|lpt4|lss|makecab|md|mkdir|mklink|mode|more|move|neq|net|netsh|not|nul|openfiles|path|pause|ping|popd|print|prompt|pushd|rd|recover|reg|rem|ren|rename|replace|rmdir|robocopy|rundll32|sc|schtasks|set|setlocal|setx|shift|shutdown|sort|start|subst|systeminfo|taskkill|tasklist|time|timeout|title|tree|type|ver|verify|vol|wmic|xcopy)(?![a-zA-Z]|[0-9])", RegexOptions.Multiline);
1513+
// Special keywords
1514+
BatchFileKeywordRegex3 = new Regex(@"(?<!(^(?i)(rem|::).*))(?i)NUL", RegexOptions.Multiline);
1515+
1516+
BatchFileOutKeyRegex = new Regex(@"^([ ]{1,}|@)?\b(?i)(git)(?![a-zA-Z]|[0-9])", RegexOptions.Multiline);
1517+
1518+
BatchFileCommentRegex1 = new Regex(@"(?(REM).*)", RegexOptions.Multiline);
1519+
BatchFileCommentRegex2 = new Regex(@"(?(:{2}).*)", RegexOptions.Multiline);
1520+
}
1521+
1522+
/// <summary>
1523+
/// Highlights Batch file code.
1524+
/// </summary>
1525+
/// <param name="range"></param>
1526+
public virtual void BatchFileSyntaxHighlight(Range range)
1527+
{
1528+
range.tb.LeftBracket = '(';
1529+
range.tb.RightBracket = ')';
1530+
// Clear all styles
1531+
range.ClearStyle(StyleIndex.All);
1532+
1533+
if (BatchFileAttrRegex == null)
1534+
InitBatchFileRegex();
1535+
//string highlighting
1536+
range.SetStyle(StringStyle, BatchFileStringRegex1);
1537+
//variable highlighting
1538+
range.SetStyle(VariableStyle, BatchFileVariableRegex1);
1539+
range.SetStyle(VariableStyle, BatchFileVariableRegex2);
1540+
//attribute highlighting
1541+
range.SetStyle(GrayStyle, BatchFileAttrRegex);
1542+
//class name highlighting
1543+
range.SetStyle(RedBkgdYellowStyle, BatchFileClassNameRegex);
1544+
//symbol highlighting
1545+
range.SetStyle(BatchSymbolStyle1, BatchFileSymbolRegex1);
1546+
range.SetStyle(BatchSymbolStyle2, BatchFileSymbolRegex2);
1547+
range.SetStyle(BatchSymbolStyle3, BatchFileSymbolRegex3);
1548+
//keyword highlighting
1549+
range.SetStyle(KeywordStyle, BatchFileKeywordRegex1);
1550+
range.SetStyle(KeywordStyle, BatchFileKeywordRegex2);
1551+
range.SetStyle(KeywordStyle, BatchFileKeywordRegex3);
1552+
//outside keyword highlighting
1553+
range.SetStyle(LightBlueStyle, BatchFileOutKeyRegex);
1554+
//comment highlighting
1555+
range.SetStyle(CommentStyle, BatchFileCommentRegex1);
1556+
range.SetStyle(CommentStyle, BatchFileCommentRegex2);
1557+
//clear folding markers
1558+
range.ClearFoldingMarkers();
1559+
}
14571560

14581561
#region Styles
14591562

@@ -1577,6 +1680,21 @@ public virtual void AssemblySyntaxHighlight(Range range)
15771680
/// </summary>
15781681
public Style AssemblyRegisterStyle { get; set; }
15791682

1683+
/// <summary>Add commentMore actions
1684+
/// Specific Batch file symbol style
1685+
/// </summary>
1686+
public Style BatchSymbolStyle1 { get; set; }
1687+
1688+
/// <summary>
1689+
/// Specific Batch file symbol style
1690+
/// </summary>
1691+
public Style BatchSymbolStyle2 { get; set; }
1692+
1693+
/// <summary>Add commentMore actions
1694+
/// Specific Batch file symbol style
1695+
/// </summary>
1696+
public Style BatchSymbolStyle3 { get; set; }
1697+
15801698
#endregion
15811699
}
15821700

@@ -1595,6 +1713,7 @@ public enum Language
15951713
JS,
15961714
Lua,
15971715
JSON,
1598-
Assembly
1716+
Assembly,
1717+
Batch
15991718
}
16001719
}

0 commit comments

Comments
 (0)