@@ -37,14 +37,12 @@ export class HumanFriendlyWriter implements Writer {
37
37
) ;
38
38
39
39
// Show some context around diagnostic range
40
- const startOffset = offsetAt ( diagnostic . range . start , text ) ;
41
- const endOffset = offsetAt ( diagnostic . range . end , text ) ;
42
- const codePrev = chalk . cyan ( text . substring ( Math . max ( startOffset - 10 , 0 ) , startOffset ) ) ;
43
- const codeHighlight = chalk . magenta ( text . substring ( startOffset , endOffset ) ) ;
44
- const codePost = chalk . cyan ( text . substring ( endOffset , endOffset + 10 ) ) ;
45
- const code = codePrev + codeHighlight + codePost ;
46
- let msg ;
40
+ const codePrevLine = this . getLine ( diagnostic . range . start . line - 1 , text ) ;
41
+ const codeLine = this . getCodeLine ( diagnostic , text ) ;
42
+ const codeNextLine = this . getLine ( diagnostic . range . end . line + 1 , text ) ;
43
+ const code = codePrevLine + codeLine + codeNextLine ;
47
44
45
+ let msg ;
48
46
if ( this . isVerbose ) {
49
47
msg = `${ diagnostic . message } ${ source } \n${ chalk . cyan ( code ) } ` ;
50
48
} else {
@@ -63,6 +61,28 @@ export class HumanFriendlyWriter implements Writer {
63
61
} ) ;
64
62
}
65
63
64
+ private getCodeLine ( diagnostic : Diagnostic , text : string ) {
65
+ const startOffset = offsetAt ( diagnostic . range . start , text ) ;
66
+ const endOffset = offsetAt ( diagnostic . range . end , text ) ;
67
+ const codePrev = text . substring (
68
+ offsetAt ( { line : diagnostic . range . start . line , character : 0 } , text ) ,
69
+ startOffset ,
70
+ ) ;
71
+ const codeHighlight = chalk . magenta ( text . substring ( startOffset , endOffset ) ) ;
72
+ const codePost = text . substring (
73
+ endOffset ,
74
+ offsetAt ( { line : diagnostic . range . end . line , character : Number . MAX_SAFE_INTEGER } , text ) ,
75
+ ) ;
76
+ return codePrev + codeHighlight + codePost ;
77
+ }
78
+
79
+ private getLine ( line : number , text : string ) : string {
80
+ return text . substring (
81
+ offsetAt ( { line, character : 0 } , text ) ,
82
+ offsetAt ( { line, character : Number . MAX_SAFE_INTEGER } , text ) ,
83
+ ) ;
84
+ }
85
+
66
86
completion ( _f : number , errorCount : number , warningCount : number ) {
67
87
this . stream . write ( '====================================\n' ) ;
68
88
0 commit comments