Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash if there are diagnostics without spans #9290

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 && range.ToLinePositionSpan().OverlapsWith(d.Span.ToLinePositionSpan(sourceText))))
{
return Array.Empty<TextEdit>();
}
}

var uri = documentContext.Uri;
Expand Down