11using System ;
2+ using System . Collections . Generic ;
23using System . Globalization ;
34using System . IO ;
45
@@ -53,45 +54,48 @@ static void Main(string[] args)
5354
5455 IStateStack ? ruleStack = null ;
5556
56- using ( StreamReader sr = new StreamReader ( fileToParse ) )
57+ string fileContent = File . ReadAllText ( fileToParse ) ;
58+ ReadOnlyMemory < char > contentMemory = fileContent . AsMemory ( ) ;
59+
60+ foreach ( var lineRange in GetLineRanges ( fileContent ) )
5761 {
58- string ? line = sr . ReadLine ( ) ;
62+ bool needsLineBreak = true ;
63+
64+ ReadOnlyMemory < char > lineMemory = contentMemory . Slice ( lineRange . Start , lineRange . Length ) ;
65+ ITokenizeLineResult result = grammar . TokenizeLine ( lineMemory , ruleStack , TimeSpan . MaxValue ) ;
66+
67+ ruleStack = result . RuleStack ;
5968
60- while ( line != null )
69+ foreach ( IToken token in result . Tokens )
6170 {
62- ITokenizeLineResult result = grammar . TokenizeLine ( line , ruleStack , TimeSpan . MaxValue ) ;
71+ int startIndex = Math . Min ( token . StartIndex , lineRange . Length ) ;
72+ int endIndex = Math . Min ( token . EndIndex , lineRange . Length ) ;
6373
64- ruleStack = result . RuleStack ;
74+ int foreground = - 1 ;
75+ int background = - 1 ;
76+ FontStyle fontStyle = FontStyle . NotSet ;
6577
66- foreach ( IToken token in result . Tokens )
78+ foreach ( var themeRule in theme . Match ( token . Scopes ) )
6779 {
68- int startIndex = ( token . StartIndex > line . Length ) ?
69- line . Length : token . StartIndex ;
70- int endIndex = ( token . EndIndex > line . Length ) ?
71- line . Length : token . EndIndex ;
80+ if ( foreground == - 1 && themeRule . foreground > 0 )
81+ foreground = themeRule . foreground ;
7282
73- int foreground = - 1 ;
74- int background = - 1 ;
75- FontStyle fontStyle = FontStyle . NotSet ;
83+ if ( background == - 1 && themeRule . background > 0 )
84+ background = themeRule . background ;
7685
77- foreach ( var themeRule in theme . Match ( token . Scopes ) )
78- {
79- if ( foreground == - 1 && themeRule . foreground > 0 )
80- foreground = themeRule . foreground ;
81-
82- if ( background == - 1 && themeRule . background > 0 )
83- background = themeRule . background ;
86+ if ( fontStyle == FontStyle . NotSet && themeRule . fontStyle > 0 )
87+ fontStyle = themeRule . fontStyle ;
88+ }
8489
85- if ( fontStyle == FontStyle . NotSet && themeRule . fontStyle > 0 )
86- fontStyle = themeRule . fontStyle ;
87- }
90+ ReadOnlySpan < char > tokenSpan = lineMemory . Span . Slice ( startIndex , endIndex - startIndex ) ;
91+ WriteToken ( tokenSpan , foreground , background , fontStyle , theme ) ;
8892
89- WriteToken ( line . SubstringAtIndexes ( startIndex , endIndex ) , foreground , background , fontStyle , theme ) ;
90- }
93+ if ( tokenSpan . IndexOf ( '\n ' ) != - 1 )
94+ needsLineBreak = false ;
95+ }
9196
97+ if ( needsLineBreak )
9298 Console . WriteLine ( ) ;
93- line = sr . ReadLine ( ) ;
94- }
9599 }
96100
97101 var colorDictionary = theme . GetGuiColorDictionary ( ) ;
@@ -113,11 +117,12 @@ static void Main(string[] args)
113117 Console . WriteLine ( "ERROR: " + ex . Message ) ;
114118 }
115119 }
116- static void WriteToken ( string text , int foreground , int background , FontStyle fontStyle , Theme theme )
120+
121+ static void WriteToken ( ReadOnlySpan < char > text , int foreground , int background , FontStyle fontStyle , Theme theme )
117122 {
118123 if ( foreground == - 1 )
119124 {
120- Console . Write ( text ) ;
125+ Console . Out . Write ( text ) ;
121126 return ;
122127 }
123128
@@ -127,7 +132,8 @@ static void WriteToken(string text, int foreground, int background, FontStyle fo
127132 Color foregroundColor = GetColor ( foreground , theme ) ;
128133
129134 Style style = new Style ( foregroundColor , backgroundColor , decoration ) ;
130- Markup markup = new Markup ( text . Replace ( "[" , "[[" ) . Replace ( "]" , "]]" ) , style ) ;
135+ string textStr = text . ToString ( ) ;
136+ Markup markup = new Markup ( textStr . Replace ( "[" , "[[" ) . Replace ( "]" , "]]" ) , style ) ;
131137
132138 AnsiConsole . Write ( markup ) ;
133139 }
@@ -173,13 +179,26 @@ static Color HexToColor(string hexString)
173179
174180 return new Color ( r , g , b ) ;
175181 }
176- }
177182
178- internal static class StringExtensions
179- {
180- internal static string SubstringAtIndexes ( this string str , int startIndex , int endIndex )
183+ static IEnumerable < ( int Start , int Length ) > GetLineRanges ( string content )
181184 {
182- return str . Substring ( startIndex , endIndex - startIndex ) ;
185+ int lineStart = 0 ;
186+
187+ for ( int i = 0 ; i < content . Length ; i ++ )
188+ {
189+ if ( content [ i ] == '\n ' )
190+ {
191+ int lineLength = i - lineStart + 1 ; // Include the \n
192+ yield return ( lineStart , lineLength ) ;
193+ lineStart = i + 1 ;
194+ }
195+ }
196+
197+ // Handle last line without terminator
198+ if ( lineStart < content . Length )
199+ {
200+ yield return ( lineStart , content . Length - lineStart ) ;
201+ }
183202 }
184203 }
185204}
0 commit comments