Skip to content

Commit c8f22cd

Browse files
authored
Change format + fix for --richerrors (#17614)
1 parent 6c96fc7 commit c8f22cd

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/Compiler/Driver/CompilerDiagnostics.fs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,7 @@ type FormattedDiagnosticDetailedInfo =
20452045
Canonical: FormattedDiagnosticCanonicalInformation
20462046
Message: string
20472047
Context: string option
2048+
DiagnosticStyle: DiagnosticStyle
20482049
}
20492050

20502051
[<RequireQualifiedAccess>]
@@ -2118,7 +2119,7 @@ let FormatDiagnosticLocation (tcConfig: TcConfig) (m: Range) : FormattedDiagnost
21182119
| DiagnosticStyle.Rich ->
21192120
let file = file.Replace('/', Path.DirectorySeparatorChar)
21202121
let m = withStart (mkPos m.StartLine (m.StartColumn + 1)) m
2121-
(sprintf "◦→ %s (%d,%d)" file m.StartLine m.StartColumn), m, file
2122+
(sprintf "\n --> %s (%d,%d)" file m.StartLine m.StartColumn), m, file
21222123

21232124
{
21242125
Range = m
@@ -2164,7 +2165,7 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
21642165
| DiagnosticStyle.Default
21652166
| DiagnosticStyle.Test -> sprintf "%s FS%04d: " message errorNumber
21662167
| DiagnosticStyle.VisualStudio -> sprintf "%s %s FS%04d: " subcategory message errorNumber
2167-
| DiagnosticStyle.Rich -> sprintf "\n◦→ %s FS%04d: " message errorNumber
2168+
| DiagnosticStyle.Rich -> sprintf "%s FS%04d: " message errorNumber
21682169

21692170
let canonical: FormattedDiagnosticCanonicalInformation =
21702171
{
@@ -2179,11 +2180,8 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
21792180
| DiagnosticStyle.Gcc
21802181
| DiagnosticStyle.Default
21812182
| DiagnosticStyle.Test
2183+
| DiagnosticStyle.Rich
21822184
| DiagnosticStyle.VisualStudio -> diagnostic.FormatCore(tcConfig.flatErrors, suggestNames)
2183-
| DiagnosticStyle.Rich ->
2184-
diagnostic.FormatCore(tcConfig.flatErrors, suggestNames).Split([| '\n' |])
2185-
|> Array.map (fun msg -> "\n" + msg)
2186-
|> String.Concat
21872185

21882186
let context =
21892187
match tcConfig.diagnosticStyle with
@@ -2201,14 +2199,14 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
22012199
|> System.IO.File.ReadAllLines
22022200

22032201
if m.StartLine = m.EndLine then
2204-
$"\n{m.StartLine} |{content[m.StartLine - 1]}\n"
2205-
+ $"""{String.init (m.StartColumn + 3) (fun _ -> " ")}^{String.init (m.EndColumn - m.StartColumn) (fun _ -> "~")}"""
2202+
$"\n {m.StartLine} | {content[m.StartLine - 1]}\n"
2203+
+ $"""{String.make (m.StartColumn + 6) ' '}{String.make (m.EndColumn - m.StartColumn) '^'}"""
22062204
|> Some
22072205
else
22082206
content
22092207
|> fun lines -> Array.sub lines (m.StartLine - 1) (m.EndLine - m.StartLine - 1)
22102208
|> Array.fold
2211-
(fun (context, lineNumber) line -> (context + $"\n{lineNumber} |{line}", lineNumber + 1))
2209+
(fun (context, lineNumber) line -> (context + $"\n{lineNumber} | {line}", lineNumber + 1))
22122210
("", (m.StartLine))
22132211
|> fst
22142212
|> Some
@@ -2220,6 +2218,7 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
22202218
Context = context
22212219
Canonical = canonical
22222220
Message = message
2221+
DiagnosticStyle = tcConfig.diagnosticStyle
22232222
}
22242223

22252224
errors.Add(FormattedDiagnostic.Long(severity, entry))
@@ -2247,16 +2246,33 @@ type PhasedDiagnostic with
22472246
match e with
22482247
| FormattedDiagnostic.Short(_, txt) -> buf.AppendString txt
22492248
| FormattedDiagnostic.Long(_, details) ->
2250-
match details.Location with
2251-
| Some l when not l.IsEmpty ->
2252-
buf.AppendString l.TextRepresentation
2253-
// Because details.Context depends on the value of details.Location, if details.Location is not None, details.Context can be accessed directly.
2254-
if details.Context.IsSome then
2255-
buf.AppendString details.Context.Value
2256-
| _ -> ()
2249+
match details.DiagnosticStyle with
2250+
| DiagnosticStyle.Emacs
2251+
| DiagnosticStyle.Gcc
2252+
| DiagnosticStyle.Test
2253+
| DiagnosticStyle.VisualStudio
2254+
| DiagnosticStyle.Default ->
2255+
match details.Location with
2256+
| Some l when not l.IsEmpty ->
2257+
buf.AppendString l.TextRepresentation
2258+
2259+
if details.Context.IsSome then
2260+
buf.AppendString details.Context.Value
2261+
| _ -> ()
2262+
2263+
buf.AppendString details.Canonical.TextRepresentation
2264+
buf.AppendString details.Message
2265+
| DiagnosticStyle.Rich ->
2266+
buf.AppendString details.Canonical.TextRepresentation
2267+
buf.AppendString details.Message
2268+
2269+
match details.Location with
2270+
| Some l when not l.IsEmpty ->
2271+
buf.AppendString l.TextRepresentation
22572272

2258-
buf.AppendString details.Canonical.TextRepresentation
2259-
buf.AppendString details.Message
2273+
if details.Context.IsSome then
2274+
buf.AppendString details.Context.Value
2275+
| _ -> ()
22602276

22612277
member diagnostic.OutputContext(buf, prefix, fileLineFunction) =
22622278
match diagnostic.Range with

src/Compiler/Driver/CompilerDiagnostics.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ type FormattedDiagnosticDetailedInfo =
114114
{ Location: FormattedDiagnosticLocation option
115115
Canonical: FormattedDiagnosticCanonicalInformation
116116
Message: string
117-
Context: string option }
117+
Context: string option
118+
DiagnosticStyle: DiagnosticStyle }
118119

119120
/// Used internally and in LegacyHostedCompilerForTesting
120121
[<RequireQualifiedAccess>]

src/Compiler/Driver/CompilerOptions.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ let testFlag tcConfigB =
13951395
let editorSpecificFlags (tcConfigB: TcConfigBuilder) =
13961396
[
13971397
CompilerOption("vserrors", tagNone, OptionUnit(fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.VisualStudio), None, None)
1398+
CompilerOption("richerrors", tagNone, OptionUnit(fun () -> tcConfigB.diagnosticStyle <- DiagnosticStyle.Rich), None, None)
13981399
CompilerOption("validate-type-providers", tagNone, OptionUnit id, None, None) // preserved for compatibility's sake, no longer has any effect
13991400
CompilerOption("LCID", tagInt, OptionInt ignore, None, None)
14001401
CompilerOption("flaterrors", tagNone, OptionUnit(fun () -> tcConfigB.flatErrors <- true), None, None)

0 commit comments

Comments
 (0)