@@ -18,13 +18,15 @@ import {
18
18
19
19
import {
20
20
isLineBreak ,
21
+ isWhiteSpace ,
21
22
COLOR_CYAN ,
22
23
COLOR_YELLOW ,
23
24
COLOR_RED ,
24
25
COLOR_MAGENTA ,
25
26
COLOR_RESET ,
26
27
isColorsEnabled ,
27
- setColorsEnabled
28
+ setColorsEnabled ,
29
+ CharCode
28
30
} from "./util" ;
29
31
30
32
export {
@@ -185,10 +187,10 @@ export function formatDiagnosticMessage(
185
187
if ( showContext ) {
186
188
sb . push ( "\n" ) ;
187
189
sb . push ( formatDiagnosticContext ( range ) ) ;
190
+ } else {
191
+ sb . push ( "\n in " ) ;
192
+ sb . push ( source . normalizedPath ) ;
188
193
}
189
- sb . push ( "\n" ) ;
190
- sb . push ( " in " ) ;
191
- sb . push ( source . normalizedPath ) ;
192
194
sb . push ( "(" ) ;
193
195
sb . push ( source . lineAt ( range . start ) . toString ( ) ) ;
194
196
sb . push ( "," ) ;
@@ -201,10 +203,10 @@ export function formatDiagnosticMessage(
201
203
if ( showContext ) {
202
204
sb . push ( "\n" ) ;
203
205
sb . push ( formatDiagnosticContext ( relatedRange ) ) ;
206
+ } else {
207
+ sb . push ( "\n in " ) ;
208
+ sb . push ( relatedSource . normalizedPath ) ;
204
209
}
205
- sb . push ( "\n" ) ;
206
- sb . push ( " in " ) ;
207
- sb . push ( relatedSource . normalizedPath ) ;
208
210
sb . push ( "(" ) ;
209
211
sb . push ( relatedSource . lineAt ( relatedRange . start ) . toString ( ) ) ;
210
212
sb . push ( "," ) ;
@@ -218,34 +220,59 @@ export function formatDiagnosticMessage(
218
220
219
221
/** Formats the diagnostic context for the specified range, optionally with terminal colors. */
220
222
function formatDiagnosticContext ( range : Range ) : string {
221
- var text = range . source . text ;
223
+ var source = range . source ;
224
+ var text = source . text ;
222
225
var len = text . length ;
223
226
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
225
231
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
226
235
while ( end < len && ! isLineBreak ( text . charCodeAt ( end ) ) ) end ++ ;
227
236
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
+ " │ "
231
245
] ;
232
246
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
+ }
235
254
}
236
255
if ( isColorsEnabled ( ) ) sb . push ( COLOR_RED ) ;
237
256
if ( range . start == range . end ) {
238
257
sb . push ( "^" ) ;
239
258
} else {
240
259
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 ) ) {
242
264
sb . push ( start == range . start + 1 ? "^" : "~" ) ;
243
265
break ;
266
+ } else {
267
+ sb . push ( "~" ) ;
244
268
}
245
- sb . push ( "~" ) ;
246
269
}
247
270
}
248
271
if ( isColorsEnabled ( ) ) sb . push ( COLOR_RESET ) ;
272
+ sb . push ( "\n " ) ;
273
+ sb . push ( lineSpace ) ;
274
+ sb . push ( " └─ in " ) ;
275
+ sb . push ( source . normalizedPath ) ;
249
276
return sb . join ( "" ) ;
250
277
}
251
278
0 commit comments