Skip to content

Commit

Permalink
Update LocationConverter.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Sep 18, 2024
1 parent c58307b commit 6d92dd9
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/Verify.SourceGenerators/Converters/LocationConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,41 @@ public override void Write(VerifyJsonWriter writer, Location value)
{
var lineSpan = value.GetMappedLineSpan();

WriteSourceTree(writer, value, lineSpan);

writer.Serialize(lineSpan);
}

static void WriteSourceTree(VerifyJsonWriter writer, Location value, FileLinePositionSpan lineSpan)
{
// Pretty-print the error with the source code snippet.
if (value.SourceTree is { } source)
if (value.SourceTree is not { } source)
{
var comment = new StringBuilder().AppendLine();
var lines = source.GetText().Lines;
var startLine = Math.Max(lineSpan.StartLinePosition.Line - contextLines, 0);
var endLine = Math.Min(lineSpan.EndLinePosition.Line + contextLines, lines.Count - 1);
for (var lineIdx = startLine; lineIdx <= endLine; lineIdx++)
return;
}

var comment = new StringBuilder();
comment.AppendLine();
var lines = source.GetText().Lines;
var startLine = Math.Max(lineSpan.StartLinePosition.Line - contextLines, 0);
var endLine = Math.Min(lineSpan.EndLinePosition.Line + contextLines, lines.Count - 1);
for (var index = startLine; index <= endLine; index++)
{
var line = lines[index];
// print the source line
comment.AppendLine(line.ToString());
// print squiggly line highlighting the location
if (line.Span.Intersection(value.SourceSpan) is not { } intersection)
{
var line = lines[lineIdx];
// print the source line
comment.AppendLine(line.ToString());
// print squiggly line highlighting the location
if (line.Span.Intersection(value.SourceSpan) is { } intersection)
{
comment
.Append(' ', intersection.Start - line.Start)
.Append('^', intersection.Length)
.AppendLine();
}
continue;
}
writer.WriteComment(comment.ToString());
writer.WriteWhitespace("\n");

comment.Append(' ', intersection.Start - line.Start);
comment.Append('^', intersection.Length);
comment.AppendLine();
}

writer.Serialize(lineSpan);
writer.WriteComment(comment.ToString());
writer.WriteWhitespace("\n");
}
}
}

0 comments on commit 6d92dd9

Please # to comment.