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

fix formatted diagnostics to make them required #3539

Merged
merged 2 commits into from
May 1, 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
4 changes: 2 additions & 2 deletions src/Microsoft.DotNet.Interactive.CSharp/CSharpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ await RunAsync(
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToImmutableArray();

context.Publish(new DiagnosticsProduced(kernelDiagnostics, submitCode, formattedDiagnostics));
context.Publish(new DiagnosticsProduced(kernelDiagnostics, formattedDiagnostics, submitCode));

// Report the compilation failure or exception
if (exception is not null)
Expand Down Expand Up @@ -457,7 +457,7 @@ internal DiagnosticsProduced GetDiagnosticsProduced(
.Select(text => new FormattedValue(PlainTextFormatter.MimeType, text))
.ToImmutableArray();

return new DiagnosticsProduced(kernelDiagnostics, command, formattedDiagnostics);
return new DiagnosticsProduced(kernelDiagnostics, formattedDiagnostics, command);
}

async Task IKernelCommandHandler<RequestDiagnostics>.HandleAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Microsoft.DotNet.Interactive.CSharpProject.Build;
internal class ProjectBuildInfo
{
public Guid ProjectGuid { get; set; } = Guid.Empty;
public IReadOnlyList<string> ProjectReferences { get; set; } = new List<string>();
public IReadOnlyList<string> ProjectReferences { get; set; } = [];
public string ProjectFilePath { get; set; } = string.Empty;
public string LanguageName { get; set; } = string.Empty;
public string TargetPath { get; set; } = string.Empty;
public string[] SourceFiles { get; set; } = Array.Empty<string>();
public string[] References { get; set; } = Array.Empty<string>();
public string[] AnalyzerReferences { get; set; } = Array.Empty<string>();
public string[] PreprocessorSymbols { get; set; } = Array.Empty<string>();
public string[] SourceFiles { get; set; } = [];
public string[] References { get; set; } = [];
public string[] AnalyzerReferences { get; set; } = [];
public string[] PreprocessorSymbols { get; set; } = [];
public string LangVersion { get; set; } = string.Empty;

public string OutputType { get; set; } = string.Empty;

// TODO : Modify the target to get this value
public string DefineConstants { get; set; } = string.Empty;
public string[] CompileInputs { get; set; } = Array.Empty<string>();
public string[] CompileInputs { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.DotNet.Interactive.CSharpProject.Build;
using Microsoft.DotNet.Interactive.CSharpProject.Servers.Roslyn;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;

namespace Microsoft.DotNet.Interactive.CSharpProject;

Expand Down Expand Up @@ -146,7 +147,10 @@ async Task IKernelCommandHandler<CompileProject>.HandleAsync(CompileProject comm

var diagnostics = GetDiagnostics(_buffer.Content, result).ToArray();

context.Publish(new DiagnosticsProduced(diagnostics, command));
context.Publish(new DiagnosticsProduced(
diagnostics,
diagnostics.Select(d => new FormattedValue(PlainTextFormatter.MimeType, d.ToString())).ToArray(),
command));

if (diagnostics.Any(d => d.Severity == CodeAnalysis.DiagnosticSeverity.Error))
{
Expand Down Expand Up @@ -188,7 +192,10 @@ async Task IKernelCommandHandler<RequestDiagnostics>.HandleAsync(RequestDiagnost

var diagnostics = GetDiagnostics(command.Code, result).ToArray();

context.Publish(new DiagnosticsProduced(diagnostics, command));
context.Publish(new DiagnosticsProduced(
diagnostics,
diagnostics.Select(d => new FormattedValue(PlainTextFormatter.MimeType, d.ToString())).ToArray(),
command));
}

async Task IKernelCommandHandler<RequestSignatureHelp>.HandleAsync(RequestSignatureHelp command, KernelInvocationContext context)
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.DotNet.Interactive.FSharp/FSharpKernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ type FSharpKernel () as this =
|> Array.map (fun d -> d.ToString())
|> Array.map (fun text -> new FormattedValue(PlainTextFormatter.MimeType, text))

context.Publish(DiagnosticsProduced(diagnostics, codeSubmission, formattedDiagnostics))
context.Publish(DiagnosticsProduced(diagnostics, formattedDiagnostics, codeSubmission))

match result with
| Ok(result) when not isError ->
Expand Down Expand Up @@ -370,7 +370,8 @@ type FSharpKernel () as this =
let errors = checkFileResults.Diagnostics

let diagnostics = errors |> Array.map getDiagnostic |> fun x -> x.ToImmutableArray()
context.Publish(DiagnosticsProduced(diagnostics, requestDiagnostics))
let formattedDiagnostics = errors |> Array.map (fun x -> new FormattedValue(PlainTextFormatter.MimeType, x.ToString()))
context.Publish(DiagnosticsProduced(diagnostics, formattedDiagnostics, requestDiagnostics))
}

let handleRequestValueValueInfos (requestValueInfos: RequestValueInfos) (context: KernelInvocationContext) =
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ private void PublishDiagnostics(KernelInvocationContext context, KernelCommand c

context.Publish(
new DiagnosticsProduced(
diagnostics.Select(ToSerializedDiagnostic),
command,
formattedDiagnostics));
diagnostics.Select(ToSerializedDiagnostic).ToArray(),
formattedDiagnostics,
command));

static Interactive.Diagnostic ToSerializedDiagnostic(Diagnostic d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ async Task IKernelCommandHandler<SubmitCode>.HandleAsync(
.ToImmutableArray();

var diagnostics = parseErrors.Select(ToDiagnostic).ToImmutableArray();
context.Publish(new DiagnosticsProduced(diagnostics, submitCode, formattedDiagnostics));
context.Publish(new DiagnosticsProduced(diagnostics, formattedDiagnostics, submitCode));

var parseException = new ParseException(parseErrors);
ReportError(parseException.ErrorRecord);
Expand Down Expand Up @@ -336,8 +336,11 @@ Task IKernelCommandHandler<RequestDiagnostics>.HandleAsync(

IsCompleteSubmission(code, out var parseErrors);

var diagnostics = parseErrors.Select(ToDiagnostic);
context.Publish(new DiagnosticsProduced(diagnostics, requestDiagnostics));
var diagnostics = parseErrors.Select(ToDiagnostic).ToArray();
context.Publish(new DiagnosticsProduced(
diagnostics,
diagnostics.Select(d => new FormattedValue(PlainTextFormatter.MimeType, d.ToString())).ToArray(),
requestDiagnostics));

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"message": "message"
}
],
"formattedDiagnostics": []
"formattedDiagnostics": [
{
"mimeType": "text/plain",
"value": "code: [(1, 2)-(3, 4)) message",
"suppressDisplay": false
}
]
},
"eventType": "DiagnosticsProduced",
"command": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ IEnumerable<KernelEvent> events()
},
requestCompletion);

var diagnostic = new Diagnostic(
new LinePositionSpan(
new LinePosition(1, 2),
new LinePosition(3, 4)),
DiagnosticSeverity.Error,
"code",
"message");

yield return new DiagnosticsProduced(
new[]
{
new Diagnostic(
new LinePositionSpan(
new LinePosition(1, 2),
new LinePosition(3, 4)),
DiagnosticSeverity.Error,
"code",
"message")
},
[diagnostic],
[new FormattedValue(PlainTextFormatter.MimeType, diagnostic.ToString())],
new SubmitCode("123"));

yield return new DisplayedValueProduced(
Expand Down
25 changes: 15 additions & 10 deletions src/Microsoft.DotNet.Interactive/Events/DiagnosticsProduced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.DotNet.Interactive.Commands;

namespace Microsoft.DotNet.Interactive.Events;

public class DiagnosticsProduced : KernelEvent
{
private readonly IReadOnlyCollection<Diagnostic> _diagnostics;

public DiagnosticsProduced(IEnumerable<Diagnostic> diagnostics,
KernelCommand command,
IReadOnlyCollection<FormattedValue> formattedDiagnostics = null) : base(command)
public DiagnosticsProduced(
IReadOnlyCollection<Diagnostic> diagnostics,
IReadOnlyCollection<FormattedValue> formattedDiagnostics,
KernelCommand command) : base(command)
{
if (diagnostics is null)
{
throw new ArgumentNullException(nameof(diagnostics));
}

_diagnostics = diagnostics.ToImmutableList();
FormattedDiagnostics = formattedDiagnostics ?? Array.Empty<FormattedValue>();
if (formattedDiagnostics is null)
{
throw new ArgumentNullException(nameof(formattedDiagnostics));
}

Diagnostics = this.RemapDiagnosticsFromRequestingCommand(diagnostics);

FormattedDiagnostics = formattedDiagnostics;
}

public IReadOnlyCollection<Diagnostic> Diagnostics => this.RemapDiagnosticsFromRequestingCommand(_diagnostics);
public IReadOnlyCollection<Diagnostic> Diagnostics { get; }

public IReadOnlyCollection<FormattedValue> FormattedDiagnostics { get; }

public override string ToString() => $"{GetType().Name}";
public override string ToString() =>
$"{nameof(DiagnosticsProduced)}: {string.Join(Environment.NewLine, Diagnostics).TruncateForDisplay()}";
}
2 changes: 2 additions & 0 deletions src/Microsoft.DotNet.Interactive/Events/ErrorProduced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public ErrorProduced(
}

public string Message { get; }

public override string ToString() => $"{nameof(ErrorProduced)}: {Message?.TruncateForDisplay()}";
}
2 changes: 2 additions & 0 deletions src/Microsoft.DotNet.Interactive/FormattedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public static IReadOnlyList<FormattedValue> CreateManyFromObject(object value, p

return formattedValues;
}

public override string ToString() => $"{MimeType}: {Value.TruncateForDisplay()}";
}
20 changes: 11 additions & 9 deletions src/Microsoft.DotNet.Interactive/KernelEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Text;
using Microsoft.DotNet.Interactive.Commands;
Expand Down Expand Up @@ -35,32 +34,35 @@ public static LinePositionSpan CalculateLineOffsetFromParentCommand(this KernelE
return initialRange;
}

public static IReadOnlyCollection<Diagnostic> RemapDiagnosticsFromRequestingCommand(this KernelEvent @event, IReadOnlyCollection<Diagnostic> diagnostics)
public static IReadOnlyCollection<Diagnostic> RemapDiagnosticsFromRequestingCommand(
this KernelEvent @event,
IReadOnlyCollection<Diagnostic> diagnostics)
{
return @event.Command switch
{
SubmitCode submitCode
when submitCode.LanguageNode is { } => submitCode.LanguageNode.RemapDiagnosticsFromLanguageNode(diagnostics),
when submitCode.LanguageNode is not null => submitCode.LanguageNode.RemapDiagnosticsFromLanguageNode(diagnostics),

RequestDiagnostics requestDiagnostics
when requestDiagnostics.LanguageNode is { } => requestDiagnostics.LanguageNode.RemapDiagnosticsFromLanguageNode(diagnostics),
when requestDiagnostics.LanguageNode is not null => requestDiagnostics.LanguageNode.RemapDiagnosticsFromLanguageNode(diagnostics),

_ => diagnostics // no meaningful remapping can occur
};
}

private static IReadOnlyCollection<Diagnostic> RemapDiagnosticsFromLanguageNode(this LanguageNode languageNode, IReadOnlyCollection<Diagnostic> diagnostics)
private static IReadOnlyCollection<Diagnostic> RemapDiagnosticsFromLanguageNode(
this LanguageNode languageNode,
IReadOnlyCollection<Diagnostic> diagnostics)
{
var root = languageNode.SyntaxTree.GetRoot();
var initialSpan = languageNode.Span;
var sourceText = SourceText.From(root.Text);
var codePosition = sourceText.Lines.GetLinePositionSpan(initialSpan);
return diagnostics.Select(d => d.WithLinePositionSpan(
new LinePositionSpan(
new LinePosition(d.LinePositionSpan.Start.Line + codePosition.Start.Line, d.LinePositionSpan.Start.Character),
new LinePosition(d.LinePositionSpan.End.Line + codePosition.Start.Line, d.LinePositionSpan.End.Character))
)
).ToImmutableList();
d.LinePositionSpan.Start with { Line = d.LinePositionSpan.Start.Line + codePosition.Start.Line },
d.LinePositionSpan.End with { Line = d.LinePositionSpan.End.Line + codePosition.Start.Line }))
).ToArray();
}

internal static void StampRoutingSlipAndLog(this KernelEvent @event, Uri uri)
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.DotNet.Interactive/Parsing/SubmissionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;

namespace Microsoft.DotNet.Interactive.Parsing;

Expand Down Expand Up @@ -124,7 +125,10 @@ private IReadOnlyList<KernelCommand> SplitSubmission(
CodeAnalysis.DiagnosticSeverity.Error,
"NI0001", // QUESTION: (SplitSubmission) what code should this be?
"Unrecognized magic command");
var diagnosticsProduced = new DiagnosticsProduced(new[] { diagnostic }, originalCommand);
var diagnosticsProduced = new DiagnosticsProduced(
[diagnostic],
[new FormattedValue(PlainTextFormatter.MimeType, diagnostic.ToString())] ,
originalCommand);
context.Publish(diagnosticsProduced);
return Task.CompletedTask;
});
Expand Down
Loading