Skip to content

Commit

Permalink
Add setting for left padding
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Jan 30, 2024
1 parent 741f4df commit 2f819fb
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
6 changes: 6 additions & 0 deletions dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"commands": [
"dotnet-example"
]
},
"verify.tool": {
"version": "0.6.0",
"commands": [
"dotnet-verify"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -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
└─
29 changes: 29 additions & 0 deletions src/Errata.Tests/ReportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,35 @@ public Task Should_Render_Compact_Labels_Correctly()
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)]
Expand Down
2 changes: 2 additions & 0 deletions src/Errata/Rendering/DiagnosticContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 9 additions & 9 deletions src/Errata/Rendering/DiagnosticRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Render(Diagnostic diagnostic)
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);
Expand All @@ -52,7 +52,7 @@ public void Render(Diagnostic diagnostic)
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();

Expand Down Expand Up @@ -102,15 +102,15 @@ public void Render(Diagnostic diagnostic)
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();
}
}

// 🔎 ···(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();

Expand All @@ -124,7 +124,7 @@ public void Render(Diagnostic diagnostic)
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);
Expand All @@ -135,7 +135,7 @@ public void Render(Diagnostic diagnostic)
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();
}
Expand All @@ -145,7 +145,7 @@ public void Render(Diagnostic diagnostic)
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();
Expand Down Expand Up @@ -281,15 +281,15 @@ private static void RenderMargin(
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);
}
else
{
// 🔎 ····(dot)
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + 2);
ctx.Builder.AppendSpaces(ctx.LineNumberWidth + ctx.LeftPadding);
ctx.Builder.Append(Character.Dot, Color.Grey);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Errata/Rendering/ReportContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -26,6 +27,7 @@ public ReportContext(IAnsiConsole console, ISourceRepository repository, ReportS
Formatter = _settings.Formatter ?? new DiagnosticFormatter();
Builder = new ReportBuilder(_console, Characters);
Compact = _settings.Compact;
LeftPadding = _settings.LeftPadding;
PropagateExceptions = _settings.PropagateExceptions;
ExcludeStackTrace = _settings.ExcludeStackTrace;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Errata/ReportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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; }
}

0 comments on commit 2f819fb

Please sign in to comment.