Skip to content

Commit

Permalink
Don't crash if there are diagnostics without spans
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier committed Sep 15, 2023
1 parent c3b3fab commit f1b4588
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public static Range ToRange(this SourceSpan sourceSpan, SourceText sourceText)
End = new Position(endLine, endChar),
};
}

public static LinePositionSpan ToLinePositionSpan(this SourceSpan sourceSpan, SourceText sourceText)
{
sourceText.GetLinesAndOffsets(sourceSpan, out var startLine, out var startChar, out var endLine, out var endChar);

return new LinePositionSpan(new(startLine, startChar), new(endLine, endChar));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public async Task<TextEdit[]> FormatAsync(

// Despite what it looks like, codeDocument.GetCSharpDocument().Diagnostics is actually the
// Razor diagnostics, not the C# diagnostics 🤦‍
if (range is not null &&
codeDocument.GetCSharpDocument().Diagnostics.Any(d => range.OverlapsWith(d.Span.ToRange(codeDocument.GetSourceText()))))
if (range is not null)
{
return Array.Empty<TextEdit>();
var sourceText = codeDocument.GetSourceText();
if (codeDocument.GetCSharpDocument().Diagnostics.Any(d => d.Span != SourceSpan.Undefined && d.Span.ToLinePositionSpan(sourceText).OverlapsWith(range.ToLinePositionSpan())))
{
return Array.Empty<TextEdit>();
}
}

var uri = documentContext.Uri;
Expand Down

0 comments on commit f1b4588

Please sign in to comment.