Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics: Clearer information style #17488

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/Compiler/Driver/CompilerDiagnostics.fs
psfinaki marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2039,14 +2039,15 @@ type FormattedDiagnosticDetailedInfo =
Location: FormattedDiagnosticLocation option
Canonical: FormattedDiagnosticCanonicalInformation
Message: string
Context: string option
}

[<RequireQualifiedAccess>]
type FormattedDiagnostic =
| Short of FSharpDiagnosticSeverity * string
| Long of FSharpDiagnosticSeverity * FormattedDiagnosticDetailedInfo

let FormatDiagnosticLocation (tcConfig: TcConfig) m : FormattedDiagnosticLocation =
let FormatDiagnosticLocation (tcConfig: TcConfig) (m: Range) : FormattedDiagnosticLocation =
if equals m rangeStartup || equals m rangeCmdArgs then
{
Range = m
Expand All @@ -2073,7 +2074,7 @@ let FormatDiagnosticLocation (tcConfig: TcConfig) m : FormattedDiagnosticLocatio
| DiagnosticStyle.Default ->
let file = file.Replace('/', Path.DirectorySeparatorChar)
let m = withStart (mkPos m.StartLine (m.StartColumn + 1)) m
(sprintf "%s(%d,%d): " file m.StartLine m.StartColumn), m, file
(sprintf "◦→ %s (%d,%d)" file m.StartLine m.StartColumn), m, file
vzarytovskii marked this conversation as resolved.
Show resolved Hide resolved

// We may also want to change Test to be 1-based
| DiagnosticStyle.Test ->
Expand Down Expand Up @@ -2150,7 +2151,7 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
match tcConfig.diagnosticStyle with
// Show the subcategory for --vserrors so that we can fish it out in Visual Studio and use it to determine error stickiness.
| DiagnosticStyle.VisualStudio -> sprintf "%s %s FS%04d: " subcategory message errorNumber
| _ -> sprintf "%s FS%04d: " message errorNumber
| _ -> sprintf "\n◦→ %s FS%04d: " message errorNumber

let canonical: FormattedDiagnosticCanonicalInformation =
{
Expand All @@ -2159,11 +2160,37 @@ let CollectFormattedDiagnostics (tcConfig: TcConfig, severity: FSharpDiagnosticS
TextRepresentation = text
}

let message = diagnostic.FormatCore(tcConfig.flatErrors, suggestNames)
let message =
diagnostic.FormatCore(tcConfig.flatErrors, suggestNames).Split([| '\n' |])
|> Array.map (fun msg -> "\n◦ " + msg)
|> String.Concat

let context =
match diagnostic.Range with
| Some m ->
let content =
m.FileName
|> FileSystem.GetFullFilePathInDirectoryShim tcConfig.implicitIncludeDir
|> System.IO.File.ReadAllLines

if m.StartLine = m.EndLine then
$"\n◦ {m.StartLine} |{content[m.StartLine - 1]}\n"
+ $"""◦ {String.init (m.StartColumn + 3) (fun _ -> " ")}^{String.init (m.EndColumn - m.StartColumn) (fun _ -> "~")}"""
|> Some
else
None
// Untested multi-line support:
// content
// |> fun lines -> Array.sub lines (m.StartLine - 1) (m.EndLine - m.StartLine - 1)
// |> Array.fold (fun (context, lineNumber) line -> (context + $"\n◦ {lineNumber} |{line}", lineNumber + 1)) ("", (m.StartLine))
// |> fst
// |> Some
| None -> None

let entry: FormattedDiagnosticDetailedInfo =
{
Location = where
Context = context
Canonical = canonical
Message = message
}
Expand Down Expand Up @@ -2194,7 +2221,10 @@ type PhasedDiagnostic with
| FormattedDiagnostic.Short(_, txt) -> buf.AppendString txt
| FormattedDiagnostic.Long(_, details) ->
match details.Location with
| Some l when not l.IsEmpty -> buf.AppendString l.TextRepresentation
| Some l when not l.IsEmpty ->
buf.AppendString l.TextRepresentation
// Because details.Context depends on the value of details.Location, if details.Location is not None, details.Context can be accessed directly.
buf.AppendString details.Context.Value
| _ -> ()

buf.AppendString details.Canonical.TextRepresentation
Expand Down
3 changes: 2 additions & 1 deletion src/Compiler/Driver/CompilerDiagnostics.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ type FormattedDiagnosticCanonicalInformation =
type FormattedDiagnosticDetailedInfo =
{ Location: FormattedDiagnosticLocation option
Canonical: FormattedDiagnosticCanonicalInformation
Message: string }
Message: string
Context: string option }

/// Used internally and in LegacyHostedCompilerForTesting
[<RequireQualifiedAccess>]
Expand Down
Loading