-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Recalculate if LSP inlay hint cache miss #75709
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 0 additions & 25 deletions
25
src/LanguageServer/Protocol/Handler/CodeLens/CodeLensResolveHandlerFactory.cs
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
|
@@ -2,27 +2,27 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.InlineHints; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Roslyn.LanguageServer.Protocol; | ||
using Roslyn.Utilities; | ||
using StreamJsonRpc; | ||
using LSP = Roslyn.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler.InlayHint | ||
{ | ||
[ExportCSharpVisualBasicStatelessLspService(typeof(InlayHintResolveHandler)), Shared] | ||
[Method(Methods.InlayHintResolveName)] | ||
internal sealed class InlayHintResolveHandler : ILspServiceDocumentRequestHandler<LSP.InlayHint, LSP.InlayHint> | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal sealed class InlayHintResolveHandler(IGlobalOptionService globalOptions) : ILspServiceDocumentRequestHandler<LSP.InlayHint, LSP.InlayHint> | ||
{ | ||
private readonly InlayHintCache _inlayHintCache; | ||
|
||
public InlayHintResolveHandler(InlayHintCache inlayHintCache) | ||
{ | ||
_inlayHintCache = inlayHintCache; | ||
} | ||
|
||
public bool MutatesSolutionState => false; | ||
|
||
public bool RequiresLSPSolution => true; | ||
|
@@ -33,39 +33,53 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.InlayHint request) | |
public Task<LSP.InlayHint> HandleRequestAsync(LSP.InlayHint request, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
var document = context.GetRequiredDocument(); | ||
return ResolveInlayHintAsync(document, request, _inlayHintCache, cancellationToken); | ||
var options = globalOptions.GetInlineHintsOptions(document.Project.Language); | ||
var inlayHintCache = context.GetRequiredService<InlayHintCache>(); | ||
var resolveData = GetInlayHintResolveData(request); | ||
return ResolveInlayHintAsync(document, request, inlayHintCache, resolveData, options, cancellationToken); | ||
} | ||
|
||
internal static async Task<LSP.InlayHint> ResolveInlayHintAsync(Document document, LSP.InlayHint request, InlayHintCache inlayHintCache, CancellationToken cancellationToken) | ||
internal static async Task<LSP.InlayHint> ResolveInlayHintAsync( | ||
Document document, | ||
LSP.InlayHint request, | ||
InlayHintCache inlayHintCache, | ||
InlayHintResolveData resolveData, | ||
InlineHintsOptions options, | ||
CancellationToken cancellationToken) | ||
{ | ||
var resolveData = GetInlayHintResolveData(request); | ||
var (cacheEntry, inlineHintToResolve) = GetCacheEntry(resolveData, inlayHintCache); | ||
|
||
var currentSyntaxVersion = await document.GetSyntaxVersionAsync(cancellationToken).ConfigureAwait(false); | ||
var cachedSyntaxVersion = cacheEntry.SyntaxVersion; | ||
var resolveSyntaxVersion = resolveData.SyntaxVersion; | ||
|
||
if (currentSyntaxVersion != cachedSyntaxVersion) | ||
if (currentSyntaxVersion.ToString() != resolveSyntaxVersion) | ||
{ | ||
throw new LocalRpcException($"Cached resolve version {cachedSyntaxVersion} does not match current version {currentSyntaxVersion}") | ||
throw new LocalRpcException($"Request resolve version {resolveSyntaxVersion} does not match current version {currentSyntaxVersion}") | ||
{ | ||
ErrorCode = LspErrorCodes.ContentModified | ||
}; | ||
} | ||
|
||
var taggedText = await inlineHintToResolve.GetDescriptionAsync(document, cancellationToken).ConfigureAwait(false); | ||
var inlineHintToResolve = GetCacheEntry(resolveData, inlayHintCache); | ||
if (inlineHintToResolve is null) | ||
{ | ||
// It is very possible that the cache no longer contains the hint being resolved (for example, multiple documents open side by side). | ||
// Instead of throwing, we can recompute the hints since we've already verified above that the version has not changed. | ||
dibarbet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var hints = await InlayHintHandler.CalculateInlayHintsAsync(document, resolveData.Range, options, resolveData.DisplayAllOverride, cancellationToken).ConfigureAwait(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you :) |
||
inlineHintToResolve = hints[resolveData.ListIndex]; | ||
} | ||
|
||
var taggedText = await inlineHintToResolve.Value.GetDescriptionAsync(document, cancellationToken).ConfigureAwait(false); | ||
|
||
request.ToolTip = ProtocolConversions.GetDocumentationMarkupContent(taggedText, document, true); | ||
return request; | ||
} | ||
|
||
private static (InlayHintCache.InlayHintCacheEntry CacheEntry, InlineHint InlineHintToResolve) GetCacheEntry(InlayHintResolveData resolveData, InlayHintCache inlayHintCache) | ||
private static InlineHint? GetCacheEntry(InlayHintResolveData resolveData, InlayHintCache inlayHintCache) | ||
{ | ||
var cacheEntry = inlayHintCache.GetCachedEntry(resolveData.ResultId); | ||
Contract.ThrowIfNull(cacheEntry, "Missing cache entry for inlay hint resolve request"); | ||
return (cacheEntry, cacheEntry.InlayHintMembers[resolveData.ListIndex]); | ||
return cacheEntry?.InlayHintMembers[resolveData.ListIndex]; | ||
} | ||
|
||
private static InlayHintResolveData GetInlayHintResolveData(LSP.InlayHint inlayHint) | ||
internal static InlayHintResolveData GetInlayHintResolveData(LSP.InlayHint inlayHint) | ||
{ | ||
Contract.ThrowIfNull(inlayHint.Data); | ||
var resolveData = JsonSerializer.Deserialize<InlayHintResolveData>((JsonElement)inlayHint.Data, ProtocolConversions.LspJsonSerializerOptions); | ||
|
26 changes: 0 additions & 26 deletions
26
src/LanguageServer/Protocol/Handler/InlayHint/InlayHintResolveHandlerFactory.cs
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to audit any other cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you're talking about other caches in different handlers, I believe we're good. We only also use a cache in completion, which doesn't have this same issue - switching editor focus will always dismiss the previous list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps. Though that seemsl ike a host-specific behavior. Would we have an issue if the host chnaged and decided to not do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: i'm not saying we need to do anything here. Just wanted to have this occupy a few brain cycles thinking about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the client ever allowed multiple completion sessions at once, then completion will function, but at a slightly degraded experience - the items could be committed, but would not have documentation. Additionally items with additional text edits (for example unimported types), or complex edits (for example override completion) would only apply the word edit, and not the additional ones.
It may be technically possible to recalculate in completion - but that would slow down the resolve request a fair amount. IMHO if we want to support the multiple lists at once scenario I'd much prefer adding something to the spec similar to microsoft/language-server-protocol#1802 (comment) that allows the client to tell us which completion items to hold onto (and for how long).