Skip to content

Commit 6de19ee

Browse files
committed
Use caches with proper lifetimes, to replace the existing static caches
1 parent 0e67970 commit 6de19ee

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Completion/RemoteCompletionService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected override IRemoteCompletionService CreateService(in ServiceArgs args)
3636

3737
private readonly RazorCompletionListProvider _razorCompletionListProvider = args.ExportProvider.GetExportedValue<RazorCompletionListProvider>();
3838
private readonly CompletionListCache _completionListCache = args.ExportProvider.GetExportedValue<CompletionListCache>();
39+
private readonly RoslynCompletionListCacheWrapper _roslynCompletionListCacheWrapper = args.ExportProvider.GetExportedValue<RoslynCompletionListCacheWrapper>();
3940
private readonly IClientCapabilitiesService _clientCapabilitiesService = args.ExportProvider.GetExportedValue<IClientCapabilitiesService>();
4041
private readonly CompletionTriggerAndCommitCharacters _triggerAndCommitCharacters = args.ExportProvider.GetExportedValue<CompletionTriggerAndCommitCharacters>();
4142
private readonly IRazorFormattingService _formattingService = args.ExportProvider.GetExportedValue<IRazorFormattingService>();
@@ -204,16 +205,15 @@ private async ValueTask<Response> GetCompletionAsync(
204205
VSInternalCompletionList? completionList = null;
205206
using (_telemetryReporter.TrackLspRequest(Methods.TextDocumentCompletionName, Constants.ExternalAccessServerName, TelemetryThresholds.CompletionSubLSPTelemetryThreshold, correlationId))
206207
{
207-
#pragma warning disable CS0618 // Type or member is obsolete. Will be addressed in a future PR but Roslyn changes are batched
208208
completionList = await ExternalAccess.Razor.Cohost.Handlers.Completion.GetCompletionListAsync(
209209
generatedDocument,
210210
mappedLinePosition,
211211
completionContext,
212212
clientCapabilities.SupportsVisualStudioExtensions,
213213
completionSetting,
214+
_roslynCompletionListCacheWrapper.GetCache(),
214215
cancellationToken)
215216
.ConfigureAwait(false);
216-
#pragma warning restore CS0618 // Type or member is obsolete
217217
}
218218

219219
if (completionList is null)
@@ -330,14 +330,13 @@ private async ValueTask<VSInternalCompletionItem> ResolveCSharpCompletionItemAsy
330330

331331
var clientCapabilities = _clientCapabilitiesService.ClientCapabilities;
332332
var completionListSetting = clientCapabilities.TextDocument?.Completion;
333-
#pragma warning disable CS0618 // Type or member is obsolete. Will be addressed in a future PR but Roslyn changes are batched
334333
var result = await ExternalAccess.Razor.Cohost.Handlers.Completion.ResolveCompletionItemAsync(
335334
request,
336335
generatedDocument,
337336
clientCapabilities.SupportsVisualStudioExtensions,
338337
completionListSetting ?? new(),
338+
_roslynCompletionListCacheWrapper.GetCache(),
339339
cancellationToken).ConfigureAwait(false);
340-
#pragma warning restore CS0618 // Type or member is obsolete
341340

342341
var item = JsonHelpers.Convert<CompletionItem, VSInternalCompletionItem>(result).AssumeNotNull();
343342

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Composition;
5+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers;
6+
using Microsoft.CodeAnalysis.Razor.Remote;
7+
8+
namespace Microsoft.CodeAnalysis.Remote.Razor;
9+
10+
[Shared]
11+
[Export(typeof(ILspLifetimeService))]
12+
[Export(typeof(RoslynCompletionListCacheWrapper))]
13+
internal class RoslynCompletionListCacheWrapper : ILspLifetimeService
14+
{
15+
private CompletionListCacheWrapper? _cacheWrapper;
16+
17+
public CompletionListCacheWrapper GetCache()
18+
{
19+
_cacheWrapper ??= new();
20+
return _cacheWrapper;
21+
}
22+
23+
void ILspLifetimeService.OnLspInitialized(RemoteClientLSPInitializationOptions options)
24+
{
25+
}
26+
27+
void ILspLifetimeService.OnLspUninitialized()
28+
{
29+
_cacheWrapper = null;
30+
}
31+
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/InlayHints/RemoteInlayHintService.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ protected override IRemoteInlayHintService CreateService(in ServiceArgs args)
2626
=> new RemoteInlayHintService(in args);
2727
}
2828

29+
private readonly RoslynInlayHintCacheWrapper _cacheWrapper = args.ExportProvider.GetExportedValue<RoslynInlayHintCacheWrapper>();
30+
2931
public ValueTask<InlayHint[]?> GetInlayHintsAsync(JsonSerializableRazorPinnedSolutionInfoWrapper solutionInfo, JsonSerializableDocumentId razorDocumentId, InlayHintParams inlayHintParams, bool displayAllOverride, CancellationToken cancellationToken)
3032
=> RunServiceAsync(
3133
solutionInfo,
@@ -67,9 +69,7 @@ protected override IRemoteInlayHintService CreateService(in ServiceArgs args)
6769
var textDocument = inlayHintParams.TextDocument.WithUri(generatedDocument.CreateUri());
6870
var range = projectedLinePositionSpan.ToRange();
6971

70-
#pragma warning disable CS0618 // Type or member is obsolete. Will be addressed in a future PR but Roslyn changes are batched
71-
var hints = await InlayHints.GetInlayHintsAsync(generatedDocument, textDocument, range, displayAllOverride, cancellationToken).ConfigureAwait(false);
72-
#pragma warning restore CS0618 // Type or member is obsolete
72+
var hints = await InlayHints.GetInlayHintsAsync(generatedDocument, textDocument, range, displayAllOverride, _cacheWrapper.GetCache(), cancellationToken).ConfigureAwait(false);
7373
if (hints is null)
7474
{
7575
return null;
@@ -131,8 +131,6 @@ private async ValueTask<InlayHint> ResolveInlayHintAsync(RemoteDocumentContext c
131131
.GetGeneratedDocumentAsync(cancellationToken)
132132
.ConfigureAwait(false);
133133

134-
#pragma warning disable CS0618 // Type or member is obsolete. Will be addressed in a future PR but Roslyn changes are batched
135-
return await InlayHints.ResolveInlayHintAsync(generatedDocument, inlayHint, cancellationToken).ConfigureAwait(false);
136-
#pragma warning restore CS0618 // Type or member is obsolete
134+
return await InlayHints.ResolveInlayHintAsync(generatedDocument, inlayHint, _cacheWrapper.GetCache(), cancellationToken).ConfigureAwait(false);
137135
}
138136
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Composition;
5+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers;
6+
using Microsoft.CodeAnalysis.Razor.Remote;
7+
8+
namespace Microsoft.CodeAnalysis.Remote.Razor;
9+
10+
[Shared]
11+
[Export(typeof(ILspLifetimeService))]
12+
[Export(typeof(RoslynInlayHintCacheWrapper))]
13+
internal class RoslynInlayHintCacheWrapper : ILspLifetimeService
14+
{
15+
private InlayHintCacheWrapper? _inlayHintCacheWrapper;
16+
17+
public InlayHintCacheWrapper GetCache()
18+
{
19+
_inlayHintCacheWrapper ??= new();
20+
return _inlayHintCacheWrapper;
21+
}
22+
23+
void ILspLifetimeService.OnLspInitialized(RemoteClientLSPInitializationOptions options)
24+
{
25+
}
26+
27+
void ILspLifetimeService.OnLspUninitialized()
28+
{
29+
_inlayHintCacheWrapper = null;
30+
}
31+
}

0 commit comments

Comments
 (0)