This commit is contained in:
Patrik Svensson 2024-01-30 14:47:59 +01:00
Родитель 741f4df79c
Коммит 2f819fb402
7 изменённых файлов: 71 добавлений и 9 удалений

Просмотреть файл

@ -13,6 +13,12 @@
"commands": [
"dotnet-example"
]
},
"verify.tool": {
"version": "0.6.0",
"commands": [
"dotnet-verify"
]
}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
Error [CS0019]: Operator '/' cannot be applied to operands of type 'string' and 'int'
NOTE: Try changing the type
┌─[Program.cs]
15 │ var qux = foo / bar;
· ─┬─ ┬ ─┬─
· ╰──────── This is of type 'int'
· │ │
· │ ╰── This is of type 'string'
· │
· ╰───── Division is not possible
└─

Просмотреть файл

@ -385,6 +385,35 @@ public sealed class ReportTests
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("LeftPadding")]
public Task Should_Render_Without_Left_Padding_Correctly()
{
// Given
var console = new TestConsole().Width(80);
var report = new Report(new EmbeddedResourceRepository());
report.AddDiagnostic(
Diagnostic.Error("Operator '/' cannot be applied to operands of type 'string' and 'int'")
.WithCode("CS0019")
.WithNote("Try changing the type")
.WithLabel(new Label("Program.cs", new Location(15, 23), "This is of type 'int'")
.WithColor(Color.Yellow).WithLength(3).WithPriority(1))
.WithLabel(new Label("Program.cs", new Location(15, 27), "Division is not possible")
.WithColor(Color.Red).WithLength(1).WithPriority(3))
.WithLabel(new Label("Program.cs", new Location(15, 29), "This is of type 'string'")
.WithColor(Color.Blue).WithLength(3).WithPriority(2)));
// When
report.Render(console, new ReportSettings
{
LeftPadding = false,
});
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("LastCharacter")]
[GitHubIssue(9)]

Просмотреть файл

@ -14,6 +14,8 @@ internal sealed class DiagnosticContext
public Diagnostic Diagnostic { get; }
public SourceGroupCollection Groups { get; }
public int LineNumberWidth { get; }
public bool HasLeftPadding => _ctx.LeftPadding;
public int LeftPadding => HasLeftPadding ? 2 : 1;
public DiagnosticContext(ReportContext ctx, Diagnostic diagnostic, SourceGroupCollection groups)
{

Просмотреть файл

@ -43,7 +43,7 @@ internal sealed class DiagnosticRenderer
foreach (var (_, first, last, group) in ctx.Groups.Enumerate())
{
// 🔎 ···┌─[Program.cs]\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(first ? Character.TopLeftCornerHard : Character.LeftConnector, Color.Grey);
ctx.Builder.Append(Character.HorizontalLine, Color.Grey);
ctx.Builder.Append("[", Color.Grey);
@ -52,7 +52,7 @@ internal sealed class DiagnosticRenderer
ctx.Builder.CommitLine();
// 🔎 ···│\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.VerticalLine, Color.Grey);
ctx.Builder.CommitLine();
@ -102,7 +102,7 @@ internal sealed class DiagnosticRenderer
if (!lastLine)
{
// 🔎 ···(dot)\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.Dot, Color.Grey);
ctx.Builder.CommitLine();
}
@ -110,7 +110,7 @@ internal sealed class DiagnosticRenderer
// 🔎 ···(separator)\n
var separator = last ? Character.VerticalLine : Character.Dot;
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(separator, Color.Grey);
ctx.Builder.CommitLine();
@ -124,7 +124,7 @@ internal sealed class DiagnosticRenderer
if (!string.IsNullOrWhiteSpace(labelWithNote.Note))
{
// 🔎 ···(dot) NOTE: This is a note\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.Dot, Color.Grey);
ctx.Builder.AppendSpace();
ctx.Builder.Append("NOTE: ", Color.Aqua);
@ -135,7 +135,7 @@ internal sealed class DiagnosticRenderer
if (lastLabel)
{
// 🔎 ···│\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.VerticalLine, Color.Grey);
ctx.Builder.CommitLine();
}
@ -145,7 +145,7 @@ internal sealed class DiagnosticRenderer
if (last)
{
// 🔎 ···└─\n
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.BottomLeftCornerHard, Color.Grey);
ctx.Builder.Append(Character.HorizontalLine, Color.Grey);
ctx.Builder.CommitLine();
@ -281,7 +281,7 @@ internal sealed class DiagnosticRenderer
if (showLineNumber)
{
// 🔎 ·38·│
ctx.Builder.AppendSpace();
ctx.Builder.AppendSpaces(ctx.HasLeftPadding ? 1 : 0);
ctx.Builder.Append((line.Index + 1).ToString().PadRight(ctx.LineNumberWidth));
ctx.Builder.AppendSpace();
ctx.Builder.Append(Character.VerticalLine, Color.Grey);
@ -289,7 +289,7 @@ internal sealed class DiagnosticRenderer
else
{
// 🔎 ····(dot)
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.Dot, Color.Grey);
}
}

Просмотреть файл

@ -13,6 +13,7 @@ internal sealed class ReportContext
public CharacterSet Characters { get; }
public DiagnosticFormatter Formatter { get; }
public bool Compact { get; }
public bool LeftPadding { get; }
public bool PropagateExceptions { get; }
public bool ExcludeStackTrace { get; }
@ -26,6 +27,7 @@ internal sealed class ReportContext
Formatter = _settings.Formatter ?? new DiagnosticFormatter();
Builder = new ReportBuilder(_console, Characters);
Compact = _settings.Compact;
LeftPadding = _settings.LeftPadding;
PropagateExceptions = _settings.PropagateExceptions;
ExcludeStackTrace = _settings.ExcludeStackTrace;
}

Просмотреть файл

@ -21,6 +21,12 @@ public sealed class ReportSettings
/// </summary>
public bool Compact { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not
/// the report should be padded on the left side.
/// </summary>
public bool LeftPadding { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether or not exceptions
/// should get propagated to the caller if rendering would fail.
@ -29,5 +35,9 @@ public sealed class ReportSettings
/// </summary>
public bool PropagateExceptions { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not stack traces
/// should be excluded.
/// </summary>
internal bool ExcludeStackTrace { get; set; }
}