@@ -18,13 +18,15 @@ import {
1818
1919import {
2020 isLineBreak ,
21+ isWhiteSpace ,
2122 COLOR_CYAN ,
2223 COLOR_YELLOW ,
2324 COLOR_RED ,
2425 COLOR_MAGENTA ,
2526 COLOR_RESET ,
2627 isColorsEnabled ,
27- setColorsEnabled
28+ setColorsEnabled ,
29+ CharCode
2830} from "./util" ;
2931
3032export {
@@ -185,10 +187,10 @@ export function formatDiagnosticMessage(
185187 if ( showContext ) {
186188 sb . push ( "\n" ) ;
187189 sb . push ( formatDiagnosticContext ( range ) ) ;
190+ } else {
191+ sb . push ( "\n in " ) ;
192+ sb . push ( source . normalizedPath ) ;
188193 }
189- sb . push ( "\n" ) ;
190- sb . push ( " in " ) ;
191- sb . push ( source . normalizedPath ) ;
192194 sb . push ( "(" ) ;
193195 sb . push ( source . lineAt ( range . start ) . toString ( ) ) ;
194196 sb . push ( "," ) ;
@@ -201,10 +203,10 @@ export function formatDiagnosticMessage(
201203 if ( showContext ) {
202204 sb . push ( "\n" ) ;
203205 sb . push ( formatDiagnosticContext ( relatedRange ) ) ;
206+ } else {
207+ sb . push ( "\n in " ) ;
208+ sb . push ( relatedSource . normalizedPath ) ;
204209 }
205- sb . push ( "\n" ) ;
206- sb . push ( " in " ) ;
207- sb . push ( relatedSource . normalizedPath ) ;
208210 sb . push ( "(" ) ;
209211 sb . push ( relatedSource . lineAt ( relatedRange . start ) . toString ( ) ) ;
210212 sb . push ( "," ) ;
@@ -218,34 +220,59 @@ export function formatDiagnosticMessage(
218220
219221/** Formats the diagnostic context for the specified range, optionally with terminal colors. */
220222function formatDiagnosticContext ( range : Range ) : string {
221- var text = range . source . text ;
223+ var source = range . source ;
224+ var text = source . text ;
222225 var len = text . length ;
223226 var start = range . start ;
224- var end = range . end ;
227+ var end = start ;
228+ var lineNumber = source . lineAt ( start ) . toString ( ) ;
229+ var lineSpace = " " . repeat ( lineNumber . length ) ;
230+ // Find preceeding line break
225231 while ( start > 0 && ! isLineBreak ( text . charCodeAt ( start - 1 ) ) ) start -- ;
232+ // Skip leading whitespace
233+ while ( start < len && isWhiteSpace ( text . charCodeAt ( start ) ) ) start ++ ;
234+ // Find next line break
226235 while ( end < len && ! isLineBreak ( text . charCodeAt ( end ) ) ) end ++ ;
227236 var sb : string [ ] = [
228- "\n " ,
229- text . substring ( start , end ) ,
230- "\n "
237+ lineSpace ,
238+ " :\n " ,
239+ lineNumber ,
240+ " │ " ,
241+ text . substring ( start , end ) . replaceAll ( "\t" , " " ) ,
242+ "\n " ,
243+ lineSpace ,
244+ " │ "
231245 ] ;
232246 while ( start < range . start ) {
233- sb . push ( " " ) ;
234- start ++ ;
247+ if ( text . charCodeAt ( start ) == CharCode . TAB ) {
248+ sb . push ( " " ) ;
249+ start += 2 ;
250+ } else {
251+ sb . push ( " " ) ;
252+ start ++ ;
253+ }
235254 }
236255 if ( isColorsEnabled ( ) ) sb . push ( COLOR_RED ) ;
237256 if ( range . start == range . end ) {
238257 sb . push ( "^" ) ;
239258 } else {
240259 while ( start ++ < range . end ) {
241- if ( isLineBreak ( text . charCodeAt ( start ) ) ) {
260+ let cc = text . charCodeAt ( start ) ;
261+ if ( cc == CharCode . TAB ) {
262+ sb . push ( "~~" ) ;
263+ } else if ( isLineBreak ( cc ) ) {
242264 sb . push ( start == range . start + 1 ? "^" : "~" ) ;
243265 break ;
266+ } else {
267+ sb . push ( "~" ) ;
244268 }
245- sb . push ( "~" ) ;
246269 }
247270 }
248271 if ( isColorsEnabled ( ) ) sb . push ( COLOR_RESET ) ;
272+ sb . push ( "\n " ) ;
273+ sb . push ( lineSpace ) ;
274+ sb . push ( " └─ in " ) ;
275+ sb . push ( source . normalizedPath ) ;
249276 return sb . join ( "" ) ;
250277}
251278
0 commit comments