Skip to content

Commit

Permalink
Use CompilationDiagnostic type for diagnostics instead of `Diagnost…
Browse files Browse the repository at this point in the history
…ic` (#7)

* Use `CompilationDiagnostic` type for diagnostics instead of `Diagnostic`

* Inline CompilationDiagnostic variables
  • Loading branch information
KubaZ2 authored Dec 3, 2024
1 parent 7e09b77 commit 2bb012b
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 74 deletions.
10 changes: 10 additions & 0 deletions Bot/Sharp/Compilation/CompilationDiagnostic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace Sharp.Compilation;

public record CompilationDiagnostic(
DiagnosticSeverity Severity,
string Id,
LinePosition Location,
string Message);
5 changes: 1 addition & 4 deletions Bot/Sharp/Compilation/CompilationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Microsoft.CodeAnalysis;


namespace Sharp.Compilation;

public class CompilationProvider(ICompilerProvider compilerProvider) : ICompilationProvider
Expand All @@ -11,7 +8,7 @@ public async Task<CompilationResult> CompileAsync(ulong operationId, Language la
if (compiler is null)
return new CompilationResult.CompilerNotFound(language);

List<Diagnostic> diagnostics = [];
List<CompilationDiagnostic> diagnostics = [];
MemoryStream assembly = new();
var success = await compiler.CompileAsync(operationId, code, diagnostics, assembly, output);

Expand Down
7 changes: 2 additions & 5 deletions Bot/Sharp/Compilation/CompilationResult.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Microsoft.CodeAnalysis;


namespace Sharp.Compilation;

public abstract record CompilationResult
{
public record Success(Stream Assembly, List<Diagnostic> Diagnostics) : CompilationResult;
public record Success(Stream Assembly, List<CompilationDiagnostic> Diagnostics) : CompilationResult;

public record Fail(Language Language, List<Diagnostic> Diagnostics) : CompilationResult;
public record Fail(Language Language, List<CompilationDiagnostic> Diagnostics) : CompilationResult;

public record CompilerNotFound(Language Language) : CompilationResult;
}
25 changes: 7 additions & 18 deletions Bot/Sharp/Compilation/FSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.Text;
using FSharp.Compiler.Diagnostics;
using System.Text;
using Basic.Reference.Assemblies;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -40,7 +38,7 @@ private static string[] GetArguments(string projectPath)
return [.. _baseArguments, $"-o:{projectPath}/{OutputName}", $"{projectPath}/{SourceName}"];
}

public async ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<Diagnostic> diagnostics, Stream assembly, CompilationOutput? output)
public async ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<CompilationDiagnostic> diagnostics, Stream assembly, CompilationOutput? output)
{
var fileSystem = _fileSystem;
var checker = FSharpChecker.Create(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
Expand Down Expand Up @@ -70,8 +68,12 @@ public async ValueTask<bool> CompileAsync(ulong operationId, string code, IColle
int resultDiagnosticsLength = resultDiagnostics.Length;
for (int i = 0; i < resultDiagnosticsLength; i++)
{
var diagnostic = ConvertToDiagnostic(resultDiagnostics[i]);
diagnostics.Add(diagnostic);
var resultDiagnostic = resultDiagnostics[i];

diagnostics.Add(new((DiagnosticSeverity)resultDiagnostic.Severity.Tag,
$"FS{resultDiagnostic.ErrorNumber:D4}",
new(resultDiagnostic.StartLine - 1, resultDiagnostic.StartColumn),
resultDiagnostic.Message));
}

return resultCode is 0;
Expand All @@ -83,19 +85,6 @@ public async ValueTask<bool> CompileAsync(ulong operationId, string code, IColle
}
}

private static Diagnostic ConvertToDiagnostic(FSharpDiagnostic fSharpDiagnostic)
{
LinePositionSpan position = new(
new(fSharpDiagnostic.StartLine - 1, fSharpDiagnostic.StartColumn),
new(fSharpDiagnostic.EndLine - 1, fSharpDiagnostic.EndColumn)
);
var location = Location.Create(string.Empty, default, position);

var severity = (DiagnosticSeverity)fSharpDiagnostic.Severity.Tag;

return Diagnostic.Create($"FS{fSharpDiagnostic.ErrorNumber:D4}", "FSharp", fSharpDiagnostic.Message, severity, severity, true, severity is DiagnosticSeverity.Warning ? 1 : 0, location: location);
}

private class NonDisposableStream(Stream stream) : Stream
{
public override bool CanRead => stream.CanRead;
Expand Down
6 changes: 2 additions & 4 deletions Bot/Sharp/Compilation/ICompiler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Microsoft.CodeAnalysis;

namespace Sharp.Compilation;
namespace Sharp.Compilation;

public interface ICompiler
{
public Language Language { get; }

public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<Diagnostic> diagnostics, Stream assembly, CompilationOutput? output);
public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<CompilationDiagnostic> diagnostics, Stream assembly, CompilationOutput? output);
}
15 changes: 4 additions & 11 deletions Bot/Sharp/Compilation/ILCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static Driver.Target GetTarget(CompilationOutput? output)
return Driver.Target.Dll;
}

public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<Diagnostic> diagnostics, Stream assembly, CompilationOutput? output)
public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<CompilationDiagnostic> diagnostics, Stream assembly, CompilationOutput? output)
{
CompilationLogger logger = new(diagnostics);
Driver driver = new(logger, GetTarget(output), new() { ResourceResolver = NullManifestResourceResolver.Default });
Expand Down Expand Up @@ -57,7 +57,7 @@ static bool Compile(Driver driver, string code, MemoryStream assembly)
}
}

private class CompilationLogger(ICollection<Diagnostic> diagnostics) : ILogger
private class CompilationLogger(ICollection<CompilationDiagnostic> diagnostics) : ILogger
{
public void Error(string message)
{
Expand Down Expand Up @@ -85,16 +85,9 @@ public void Warning(Mono.ILASM.Location location, string message)

public void Add(Mono.ILASM.Location? location, string message, DiagnosticSeverity severity)
{
Location? l;
if (location is null)
l = null;
else
{
LinePosition position = new(location.line - 1, location.column - 1);
l = Location.Create(string.Empty, default, new(position, position));
}
var position = location is null ? LinePosition.Zero : new(location.line - 1, location.column);

diagnostics.Add(Diagnostic.Create("IL", "IL", message, severity, severity, true, severity is DiagnosticSeverity.Warning ? 1 : 0, location: l));
diagnostics.Add(new(severity, "IL", position, message));
}
}

Expand Down
11 changes: 9 additions & 2 deletions Bot/Sharp/Compilation/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class RoslynCompiler : ICompiler

protected abstract Microsoft.CodeAnalysis.Compilation CreateCompilation(string code, CompilationOutput? output);

public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<Diagnostic> diagnostics, Stream assembly, CompilationOutput? output)
public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<CompilationDiagnostic> diagnostics, Stream assembly, CompilationOutput? output)
{
var compilation = CreateCompilation(code, output);

Expand All @@ -22,7 +22,14 @@ public ValueTask<bool> CompileAsync(ulong operationId, string code, ICollection<
int length = resultDiagnostics.Length;

for (int i = 0; i < length; i++)
diagnostics.Add(resultDiagnostics[i]);
{
var resultDiagnostic = resultDiagnostics[i];

diagnostics.Add(new(resultDiagnostic.Severity,
resultDiagnostic.Id,
resultDiagnostic.Location.GetMappedLineSpan().Span.Start,
resultDiagnostic.GetMessage()));
}

return new(result.Success);
}
Expand Down
5 changes: 3 additions & 2 deletions Bot/Sharp/CompilationResponse/CompilationResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Options;

using NetCord;

using NetCord.Rest;

using Sharp.Compilation;

using Sharp.Diagnostics;

namespace Sharp.CompilationResponse;
Expand Down Expand Up @@ -35,7 +36,7 @@ public CompilationFormatResult CompilationResponse(ulong operationId, bool succe
};
}

public CompilationFormatResult.Success CompilationResponse(ulong operationId, bool success, IReadOnlyList<Diagnostic> diagnostics)
public CompilationFormatResult.Success CompilationResponse(ulong operationId, bool success, IReadOnlyList<CompilationDiagnostic> diagnostics)
{
var (title, description) = GetContent(success);

Expand Down
6 changes: 3 additions & 3 deletions Bot/Sharp/CompilationResponse/ICompilationResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.CodeAnalysis;

using NetCord.Rest;

using Sharp.Compilation;

namespace Sharp.CompilationResponse;

public abstract record CompilationFormatResult
Expand All @@ -15,5 +15,5 @@ public interface ICompilationFormatter
{
public CompilationFormatResult CompilationResponse(ulong operationId, bool success, int page);

public CompilationFormatResult.Success CompilationResponse(ulong operationId, bool success, IReadOnlyList<Diagnostic> diagnostics);
public CompilationFormatResult.Success CompilationResponse(ulong operationId, bool success, IReadOnlyList<CompilationDiagnostic> diagnostics);
}
27 changes: 13 additions & 14 deletions Bot/Sharp/Diagnostics/DiagnosticsFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using NetCord;
using NetCord.Rest;

using Sharp.Compilation;

namespace Sharp.Diagnostics;

public class DiagnosticsFormatter(IOptions<Options> options, IMemoryCache cache) : IDiagnosticsFormatter
{
private record DiagnosticsCacheEntry(IReadOnlyList<Diagnostic> Diagnostics);
private record DiagnosticsCacheEntry(IReadOnlyList<CompilationDiagnostic> Diagnostics);

public DiagnosticsFormatResult FormatDiagnostics(ulong operationId, bool success, int page, int embedContentLength)
{
Expand All @@ -25,9 +27,9 @@ public DiagnosticsFormatResult FormatDiagnostics(ulong operationId, bool success
return new DiagnosticsFormatResult.Success(fields, [actionRow]);
}

public DiagnosticsFormatResult.Success FormatDiagnostics(ulong operationId, bool success, IReadOnlyList<Diagnostic> diagnostics, int embedContentLength)
public DiagnosticsFormatResult.Success FormatDiagnostics(ulong operationId, bool success, IReadOnlyList<CompilationDiagnostic> diagnostics, int embedContentLength)
{
IReadOnlyList<Diagnostic> visibleDiagnostics = [.. diagnostics.Where(d => d.Severity is not DiagnosticSeverity.Hidden)];
IReadOnlyList<CompilationDiagnostic> visibleDiagnostics = [.. diagnostics.Where(d => d.Severity is not DiagnosticSeverity.Hidden)];

var fields = CreateDiagnosticsFields(visibleDiagnostics, 1, embedContentLength, out var more);

Expand All @@ -47,11 +49,11 @@ public DiagnosticsFormatResult.Success FormatDiagnostics(ulong operationId, bool

private static ActionRowProperties CreateActionRow(bool success, int page, bool previousDisabled, bool nextDisabled)
{
return new(
return
[
new ButtonProperties($"diagnostics:{success}:{page - 1}", new EmojiProperties("⬅️"), ButtonStyle.Secondary) { Disabled = previousDisabled },
new ButtonProperties($"diagnostics:{success}:{page + 1}", new EmojiProperties("➡️"), ButtonStyle.Secondary) { Disabled = nextDisabled }
]);
];
}

private string GetSeverityEmoji(DiagnosticSeverity severity)
Expand All @@ -65,7 +67,7 @@ private string GetSeverityEmoji(DiagnosticSeverity severity)
};
}

private List<EmbedFieldProperties> CreateDiagnosticsFields(IReadOnlyList<Diagnostic> diagnostics, int page, int length, out bool more)
private List<EmbedFieldProperties> CreateDiagnosticsFields(IReadOnlyList<CompilationDiagnostic> diagnostics, int page, int length, out bool more)
{
int diagnosticCount = diagnostics.Count;
var pageStartIndex = GetPageStartIndex(diagnostics, page, length);
Expand Down Expand Up @@ -107,7 +109,7 @@ private List<EmbedFieldProperties> CreateDiagnosticsFields(IReadOnlyList<Diagnos
return fields;
}

private int? GetPageStartIndex(IReadOnlyList<Diagnostic> diagnostics, int page, int length)
private int? GetPageStartIndex(IReadOnlyList<CompilationDiagnostic> diagnostics, int page, int length)
{
int diagnosticIndex = 0;
int diagnosticCount = diagnostics.Count;
Expand Down Expand Up @@ -158,16 +160,13 @@ private List<EmbedFieldProperties> CreateDiagnosticsFields(IReadOnlyList<Diagnos
return diagnosticIndex;
}

private string FormatName(Diagnostic diagnostic)
private string FormatName(CompilationDiagnostic diagnostic)
{
var position = diagnostic.Location.GetMappedLineSpan().Span.Start;
return $"{GetSeverityEmoji(diagnostic.Severity)} {diagnostic.Id} ({position.Line + 1},{position.Character + 1})";
var location = diagnostic.Location;
return $"{GetSeverityEmoji(diagnostic.Severity)} {diagnostic.Id} ({location.Line + 1},{location.Character + 1})";
}

private static string FormatValue(Diagnostic diagnostic)
{
return diagnostic.GetMessage();
}
private static string FormatValue(CompilationDiagnostic diagnostic) => diagnostic.Message;

private static string LimitLength(string value, int maxLength)
{
Expand Down
6 changes: 3 additions & 3 deletions Bot/Sharp/Diagnostics/IDiagnosticsFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.CodeAnalysis;
using NetCord.Rest;

using NetCord.Rest;
using Sharp.Compilation;

namespace Sharp.Diagnostics;

Expand All @@ -15,5 +15,5 @@ public interface IDiagnosticsFormatter
{
public DiagnosticsFormatResult FormatDiagnostics(ulong operationId, bool success, int page, int embedContentLength);

public DiagnosticsFormatResult.Success FormatDiagnostics(ulong operationId, bool success, IReadOnlyList<Diagnostic> diagnostics, int embedContentLength);
public DiagnosticsFormatResult.Success FormatDiagnostics(ulong operationId, bool success, IReadOnlyList<CompilationDiagnostic> diagnostics, int embedContentLength);
}
8 changes: 3 additions & 5 deletions Bot/Sharp/Responding/IResponseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.CodeAnalysis;

using NetCord.Rest;
using NetCord.Rest;

using Sharp.Attachments;
using Sharp.Compilation;
Expand All @@ -18,9 +16,9 @@ public interface IResponseProvider

public T UnknownError<T>(string reason) where T : IMessageProperties, new();

public T DecompilationResponse<T>(ulong operationId, Language language, string decompiledCode, List<Diagnostic> diagnostics) where T : IMessageProperties, new();
public T DecompilationResponse<T>(ulong operationId, Language language, string decompiledCode, List<CompilationDiagnostic> diagnostics) where T : IMessageProperties, new();

public T RunResponse<T>(ulong operationId, Language language, string output, List<Diagnostic> diagnostics) where T : IMessageProperties, new();
public T RunResponse<T>(ulong operationId, Language language, string output, List<CompilationDiagnostic> diagnostics) where T : IMessageProperties, new();

public T RateLimitResponse<T>(ulong operationId) where T : IMessageProperties, new();

Expand Down
6 changes: 3 additions & 3 deletions Bot/Sharp/Responding/ResponseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ResponseProvider(IOptions<Options> options, IOptions<CommandService
return Error<T>("Compiler not found", $"The compiler for the language {nameFormatter.Format(language)} was not found.");
}

private T CompilationFailResponse<T>(ulong operationId, IReadOnlyList<Diagnostic> diagnostics) where T : IMessageProperties, new()
private T CompilationFailResponse<T>(ulong operationId, IReadOnlyList<CompilationDiagnostic> diagnostics) where T : IMessageProperties, new()
{
var compilationFormatResult = compilationFormatter.CompilationResponse(operationId, false, diagnostics);

Expand Down Expand Up @@ -101,7 +101,7 @@ public class ResponseProvider(IOptions<Options> options, IOptions<CommandService
return Error<T>("An error occurred", reason);
}

public T DecompilationResponse<T>(ulong operationId, Language language, string decompiledCode, List<Diagnostic> diagnostics) where T : IMessageProperties, new()
public T DecompilationResponse<T>(ulong operationId, Language language, string decompiledCode, List<CompilationDiagnostic> diagnostics) where T : IMessageProperties, new()
{
T message = new();

Expand All @@ -115,7 +115,7 @@ public class ResponseProvider(IOptions<Options> options, IOptions<CommandService
return message;
}

public T RunResponse<T>(ulong operationId, Language language, string output, List<Diagnostic> diagnostics) where T : IMessageProperties, new()
public T RunResponse<T>(ulong operationId, Language language, string output, List<CompilationDiagnostic> diagnostics) where T : IMessageProperties, new()
{
T message = new();

Expand Down

0 comments on commit 2bb012b

Please sign in to comment.