-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/VerifyTests/Verify.SourceGe…
- Loading branch information
Showing
3 changed files
with
42 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
: (1,0)-(1,14) | ||
/* | ||
class Foo { | ||
void Bar() { } | ||
^^^^^^^^^^^^^^ | ||
} | ||
*/ | ||
theFile.cs: (1,0)-(1,14) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 34 additions & 3 deletions
37
src/Verify.SourceGenerators/Converters/LocationConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,37 @@ | ||
class LocationConverter : | ||
WriteOnlyJsonConverter<Location> | ||
{ | ||
public override void Write(VerifyJsonWriter writer, Location value) => | ||
writer.Serialize(value.GetMappedLineSpan()); | ||
} | ||
const int contextLines = 1; | ||
|
||
public override void Write(VerifyJsonWriter writer, Location value) | ||
{ | ||
var lineSpan = value.GetMappedLineSpan(); | ||
|
||
// Pretty-print the error with the source code snippet. | ||
if (value.SourceTree is { } 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++) | ||
{ | ||
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(); | ||
} | ||
} | ||
writer.WriteComment(comment.ToString()); | ||
writer.WriteWhitespace("\n"); | ||
} | ||
|
||
writer.Serialize(lineSpan); | ||
} | ||
} |