@@ -21,7 +21,7 @@ type FormattingOptions struct {
21
21
}
22
22
23
23
const (
24
- foregroundColorEscapeGrey = "\u001b [90m"
24
+ ForegroundColorEscapeGrey = "\u001b [90m"
25
25
foregroundColorEscapeRed = "\u001b [91m"
26
26
foregroundColorEscapeYellow = "\u001b [93m"
27
27
foregroundColorEscapeBlue = "\u001b [94m"
@@ -31,51 +31,53 @@ const (
31
31
const (
32
32
gutterStyleSequence = "\u001b [7m"
33
33
gutterSeparator = " "
34
- resetEscapeSequence = "\u001b [0m"
34
+ ResetEscapeSequence = "\u001b [0m"
35
35
ellipsis = "..."
36
36
)
37
37
38
38
func FormatDiagnosticsWithColorAndContext (output io.Writer , diags []* ast.Diagnostic , formatOpts * FormattingOptions ) {
39
39
if len (diags ) == 0 {
40
40
return
41
41
}
42
-
43
42
for i , diagnostic := range diags {
44
43
if i > 0 {
45
44
fmt .Fprint (output , formatOpts .NewLine )
46
45
}
46
+ FormatDiagnosticWithColorAndContext (output , diagnostic , formatOpts )
47
+ }
48
+ }
47
49
48
- if diagnostic .File () != nil {
49
- file := diagnostic .File ()
50
- pos := diagnostic .Loc ().Pos ()
51
- WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
52
- fmt .Fprint (output , " - " )
53
- }
50
+ func FormatDiagnosticWithColorAndContext (output io.Writer , diagnostic * ast.Diagnostic , formatOpts * FormattingOptions ) {
51
+ if diagnostic .File () != nil {
52
+ file := diagnostic .File ()
53
+ pos := diagnostic .Loc ().Pos ()
54
+ WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
55
+ fmt .Fprint (output , " - " )
56
+ }
54
57
55
- writeWithStyleAndReset (output , diagnostic .Category ().Name (), getCategoryFormat (diagnostic .Category ()))
56
- fmt .Fprintf (output , "%s TS%d: %s" , foregroundColorEscapeGrey , diagnostic .Code (), resetEscapeSequence )
57
- WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
58
+ writeWithStyleAndReset (output , diagnostic .Category ().Name (), getCategoryFormat (diagnostic .Category ()))
59
+ fmt .Fprintf (output , "%s TS%d: %s" , ForegroundColorEscapeGrey , diagnostic .Code (), ResetEscapeSequence )
60
+ WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
58
61
59
- if diagnostic .File () != nil && diagnostic .Code () != diagnostics .File_appears_to_be_binary .Code () {
60
- fmt .Fprint (output , formatOpts .NewLine )
61
- writeCodeSnippet (output , diagnostic .File (), diagnostic .Pos (), diagnostic .Len (), getCategoryFormat (diagnostic .Category ()), "" , formatOpts )
62
- fmt .Fprint (output , formatOpts .NewLine )
63
- }
62
+ if diagnostic .File () != nil && diagnostic .Code () != diagnostics .File_appears_to_be_binary .Code () {
63
+ fmt .Fprint (output , formatOpts .NewLine )
64
+ writeCodeSnippet (output , diagnostic .File (), diagnostic .Pos (), diagnostic .Len (), getCategoryFormat (diagnostic .Category ()), "" , formatOpts )
65
+ fmt .Fprint (output , formatOpts .NewLine )
66
+ }
64
67
65
- if (diagnostic .RelatedInformation () != nil ) && (len (diagnostic .RelatedInformation ()) > 0 ) {
66
- for _ , relatedInformation := range diagnostic .RelatedInformation () {
67
- file := relatedInformation .File ()
68
- if file != nil {
69
- fmt .Fprint (output , formatOpts .NewLine )
70
- fmt .Fprint (output , " " )
71
- pos := relatedInformation .Pos ()
72
- WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
73
- fmt .Fprint (output , " - " )
74
- WriteFlattenedDiagnosticMessage (output , relatedInformation , formatOpts .NewLine )
75
- writeCodeSnippet (output , file , pos , relatedInformation .Len (), foregroundColorEscapeCyan , " " , formatOpts )
76
- }
68
+ if (diagnostic .RelatedInformation () != nil ) && (len (diagnostic .RelatedInformation ()) > 0 ) {
69
+ for _ , relatedInformation := range diagnostic .RelatedInformation () {
70
+ file := relatedInformation .File ()
71
+ if file != nil {
77
72
fmt .Fprint (output , formatOpts .NewLine )
73
+ fmt .Fprint (output , " " )
74
+ pos := relatedInformation .Pos ()
75
+ WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
76
+ fmt .Fprint (output , " - " )
77
+ WriteFlattenedDiagnosticMessage (output , relatedInformation , formatOpts .NewLine )
78
+ writeCodeSnippet (output , file , pos , relatedInformation .Len (), foregroundColorEscapeCyan , " " , formatOpts )
78
79
}
80
+ fmt .Fprint (output , formatOpts .NewLine )
79
81
}
80
82
}
81
83
}
@@ -104,7 +106,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
104
106
fmt .Fprint (writer , indent )
105
107
fmt .Fprint (writer , gutterStyleSequence )
106
108
fmt .Fprintf (writer , "%*s" , gutterWidth , ellipsis )
107
- fmt .Fprint (writer , resetEscapeSequence )
109
+ fmt .Fprint (writer , ResetEscapeSequence )
108
110
fmt .Fprint (writer , gutterSeparator )
109
111
fmt .Fprint (writer , formatOpts .NewLine )
110
112
i = lastLine - 1
@@ -125,7 +127,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
125
127
fmt .Fprint (writer , indent )
126
128
fmt .Fprint (writer , gutterStyleSequence )
127
129
fmt .Fprintf (writer , "%*d" , gutterWidth , i + 1 )
128
- fmt .Fprint (writer , resetEscapeSequence )
130
+ fmt .Fprint (writer , ResetEscapeSequence )
129
131
fmt .Fprint (writer , gutterSeparator )
130
132
fmt .Fprint (writer , lineContent )
131
133
fmt .Fprint (writer , formatOpts .NewLine )
@@ -134,7 +136,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
134
136
fmt .Fprint (writer , indent )
135
137
fmt .Fprint (writer , gutterStyleSequence )
136
138
fmt .Fprintf (writer , "%*s" , gutterWidth , "" )
137
- fmt .Fprint (writer , resetEscapeSequence )
139
+ fmt .Fprint (writer , ResetEscapeSequence )
138
140
fmt .Fprint (writer , gutterSeparator )
139
141
fmt .Fprint (writer , squiggleColor )
140
142
if i == firstLine {
@@ -159,7 +161,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
159
161
fmt .Fprint (writer , strings .Repeat ("~" , len (lineContent )))
160
162
}
161
163
162
- fmt .Fprint (writer , resetEscapeSequence )
164
+ fmt .Fprint (writer , ResetEscapeSequence )
163
165
}
164
166
}
165
167
@@ -196,7 +198,7 @@ func getCategoryFormat(category diagnostics.Category) string {
196
198
case diagnostics .CategoryWarning :
197
199
return foregroundColorEscapeYellow
198
200
case diagnostics .CategorySuggestion :
199
- return foregroundColorEscapeGrey
201
+ return ForegroundColorEscapeGrey
200
202
case diagnostics .CategoryMessage :
201
203
return foregroundColorEscapeBlue
202
204
}
@@ -208,7 +210,7 @@ type FormattedWriter func(output io.Writer, text string, formatStyle string)
208
210
func writeWithStyleAndReset (output io.Writer , text string , formatStyle string ) {
209
211
fmt .Fprint (output , formatStyle )
210
212
fmt .Fprint (output , text )
211
- fmt .Fprint (output , resetEscapeSequence )
213
+ fmt .Fprint (output , ResetEscapeSequence )
212
214
}
213
215
214
216
func WriteLocation (output io.Writer , file * ast.SourceFile , pos int , formatOpts * FormattingOptions , writeWithStyleAndReset FormattedWriter ) {
@@ -359,9 +361,9 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
359
361
}
360
362
return fmt .Sprintf ("%s%s:%d%s" ,
361
363
fileName ,
362
- foregroundColorEscapeGrey ,
364
+ ForegroundColorEscapeGrey ,
363
365
line + 1 ,
364
- resetEscapeSequence ,
366
+ ResetEscapeSequence ,
365
367
)
366
368
}
367
369
@@ -383,3 +385,15 @@ func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatO
383
385
WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
384
386
fmt .Fprint (output , formatOpts .NewLine )
385
387
}
388
+
389
+ func FormatDiagnosticsStatusWithColorAndTime (output io.Writer , time string , diag * ast.Diagnostic , formatOpts * FormattingOptions ) {
390
+ fmt .Fprint (output , "[" )
391
+ writeWithStyleAndReset (output , time , ForegroundColorEscapeGrey )
392
+ fmt .Fprint (output , "] " )
393
+ WriteFlattenedDiagnosticMessage (output , diag , formatOpts .NewLine )
394
+ }
395
+
396
+ func FormatDiagnosticsStatusAndTime (output io.Writer , time string , diag * ast.Diagnostic , formatOpts * FormattingOptions ) {
397
+ fmt .Fprint (output , time , " - " )
398
+ WriteFlattenedDiagnosticMessage (output , diag , formatOpts .NewLine )
399
+ }
0 commit comments