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 throw if we get an old/bad Inlay Hint request #10968

Merged
merged 1 commit into from
Oct 4, 2024
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 @@ -29,6 +29,16 @@ internal sealed class InlayHintService(IDocumentMappingService documentMappingSe

var span = range.ToLinePositionSpan();

cancellationToken.ThrowIfCancellationRequested();

// Sometimes the client sends us a request that doesn't match the file contents. Could be a bug with old requests
// not being cancelled, but no harm in being defensive
if (!codeDocument.Source.Text.TryGetAbsoluteIndex(span.Start, out var startIndex) ||
!codeDocument.Source.Text.TryGetAbsoluteIndex(span.End, out var endIndex))
{
return null;
}

// We are given a range by the client, but our mapping only succeeds if the start and end of the range can both be mapped
// to C#. Since that doesn't logically match what we want from inlay hints, we instead get the minimum range of mappable
// C# to get hints for. We'll filter that later, to remove the sections that can't be mapped back.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ protected override IRemoteInlayHintService CreateService(in ServiceArgs args)

var span = inlayHintParams.Range.ToLinePositionSpan();

cancellationToken.ThrowIfCancellationRequested();

// Sometimes the client sends us a request that doesn't match the file contents. Could be a bug with old requests
// not being cancelled, but no harm in being defensive
if (!codeDocument.Source.Text.TryGetAbsoluteIndex(span.Start, out var startIndex) ||
!codeDocument.Source.Text.TryGetAbsoluteIndex(span.End, out var endIndex))
{
return null;
}

// We are given a range by the client, but our mapping only succeeds if the start and end of the range can both be mapped
// to C#. Since that doesn't logically match what we want from inlay hints, we instead get the minimum range of mappable
// C# to get hints for. We'll filter that later, to remove the sections that can't be mapped back.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public Task InlayHints_ComponentAttributes()

""",
toolTipMap: new Dictionary<string, string>
{
},
{
},
output: """

<div>
Expand All @@ -85,6 +85,42 @@ public Task InlayHints_ComponentAttributes()

""");

[Theory]
[InlineData(0, 0, 0, 20)]
[InlineData(0, 0, 2, 0)]
[InlineData(2, 0, 4, 0)]
public async Task InlayHints_InvalidRange(int startLine, int starChar, int endLine, int endChar)
{
var input = """
<div></div>
""";
var razorFilePath = "C:/path/to/file.razor";
var codeDocument = CreateCodeDocument(input, filePath: razorFilePath);

var languageServer = await CreateLanguageServerAsync(codeDocument, razorFilePath);

var service = new InlayHintService(DocumentMappingService);

var endpoint = new InlayHintEndpoint(service, languageServer);

var request = new InlayHintParams()
{
TextDocument = new VSTextDocumentIdentifier
{
Uri = new Uri(razorFilePath)
},
Range = VsLspFactory.CreateRange(startLine, starChar, endLine, endChar)
};
Assert.True(DocumentContextFactory.TryCreate(request.TextDocument, out var documentContext));
var requestContext = CreateRazorRequestContext(documentContext);

// Act
var hints = await endpoint.HandleRequestAsync(request, requestContext, DisposalToken);

// Assert
Assert.Null(hints);
}

private async Task VerifyInlayHintsAsync(string input, Dictionary<string, string> toolTipMap, string output)
{
TestFileMarkupParser.GetSpans(input, out input, out ImmutableDictionary<string, ImmutableArray<TextSpan>> spansDict);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ public Task InlayHints_ComponentAttributes()

""");

[Theory]
[InlineData(0, 0, 0, 20)]
[InlineData(0, 0, 2, 0)]
[InlineData(2, 0, 4, 0)]
public async Task InlayHints_InvalidRange(int startLine, int starChar, int endLine, int endChar)
{
var input = """
<div></div>
""";
var document = await CreateProjectAndRazorDocumentAsync(input);
var endpoint = new CohostInlayHintEndpoint(RemoteServiceInvoker);

var request = new InlayHintParams()
{
TextDocument = new TextDocumentIdentifier() { Uri = document.CreateUri() },
Range = RoslynLspFactory.CreateRange(startLine, starChar, endLine, endChar)
};

var hints = await endpoint.GetTestAccessor().HandleRequestAsync(request, document, displayAllOverride: false, DisposalToken);

// Assert
Assert.Null(hints);
}

private async Task VerifyInlayHintsAsync(string input, Dictionary<string, string> toolTipMap, string output, bool displayAllOverride = false)
{
TestFileMarkupParser.GetSpans(input, out input, out ImmutableDictionary<string, ImmutableArray<TextSpan>> spansDict);
Expand Down