Skip to content

Commit 6d92dd9

Browse files
committed
Update LocationConverter.cs
1 parent c58307b commit 6d92dd9

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/Verify.SourceGenerators/Converters/LocationConverter.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,41 @@ public override void Write(VerifyJsonWriter writer, Location value)
77
{
88
var lineSpan = value.GetMappedLineSpan();
99

10+
WriteSourceTree(writer, value, lineSpan);
11+
12+
writer.Serialize(lineSpan);
13+
}
14+
15+
static void WriteSourceTree(VerifyJsonWriter writer, Location value, FileLinePositionSpan lineSpan)
16+
{
1017
// Pretty-print the error with the source code snippet.
11-
if (value.SourceTree is { } source)
18+
if (value.SourceTree is not { } source)
1219
{
13-
var comment = new StringBuilder().AppendLine();
14-
var lines = source.GetText().Lines;
15-
var startLine = Math.Max(lineSpan.StartLinePosition.Line - contextLines, 0);
16-
var endLine = Math.Min(lineSpan.EndLinePosition.Line + contextLines, lines.Count - 1);
17-
for (var lineIdx = startLine; lineIdx <= endLine; lineIdx++)
20+
return;
21+
}
22+
23+
var comment = new StringBuilder();
24+
comment.AppendLine();
25+
var lines = source.GetText().Lines;
26+
var startLine = Math.Max(lineSpan.StartLinePosition.Line - contextLines, 0);
27+
var endLine = Math.Min(lineSpan.EndLinePosition.Line + contextLines, lines.Count - 1);
28+
for (var index = startLine; index <= endLine; index++)
29+
{
30+
var line = lines[index];
31+
// print the source line
32+
comment.AppendLine(line.ToString());
33+
// print squiggly line highlighting the location
34+
if (line.Span.Intersection(value.SourceSpan) is not { } intersection)
1835
{
19-
var line = lines[lineIdx];
20-
// print the source line
21-
comment.AppendLine(line.ToString());
22-
// print squiggly line highlighting the location
23-
if (line.Span.Intersection(value.SourceSpan) is { } intersection)
24-
{
25-
comment
26-
.Append(' ', intersection.Start - line.Start)
27-
.Append('^', intersection.Length)
28-
.AppendLine();
29-
}
36+
continue;
3037
}
31-
writer.WriteComment(comment.ToString());
32-
writer.WriteWhitespace("\n");
38+
39+
comment.Append(' ', intersection.Start - line.Start);
40+
comment.Append('^', intersection.Length);
41+
comment.AppendLine();
3342
}
3443

35-
writer.Serialize(lineSpan);
44+
writer.WriteComment(comment.ToString());
45+
writer.WriteWhitespace("\n");
3646
}
37-
}
47+
}

0 commit comments

Comments
 (0)