Skip to content

Commit 7b05d27

Browse files
committed
Introduce a lifetime service in remote
1 parent 540d743 commit 7b05d27

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 Microsoft.CodeAnalysis.Razor.Remote;
5+
6+
namespace Microsoft.CodeAnalysis.Remote.Razor;
7+
8+
internal interface ILspLifetimeService
9+
{
10+
void OnLspInitialized(RemoteClientLSPInitializationOptions options);
11+
void OnLspUninitialized();
12+
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Initialization/RemoteClientCapabilitiesService.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33

44
using System.Composition;
55
using Microsoft.CodeAnalysis.Razor.Protocol;
6+
using Microsoft.CodeAnalysis.Razor.Remote;
67

78
namespace Microsoft.CodeAnalysis.Remote.Razor;
89

910
[Shared]
1011
[Export(typeof(IClientCapabilitiesService))]
11-
[Export(typeof(RemoteClientCapabilitiesService))]
12-
internal sealed class RemoteClientCapabilitiesService : AbstractClientCapabilitiesService;
12+
[Export(typeof(ILspLifetimeService))]
13+
internal sealed class RemoteClientCapabilitiesService : AbstractClientCapabilitiesService, ILspLifetimeService
14+
{
15+
public void OnLspInitialized(RemoteClientLSPInitializationOptions options)
16+
{
17+
SetCapabilities(options.ClientCapabilities);
18+
}
19+
20+
public void OnLspUninitialized()
21+
{
22+
}
23+
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Initialization/RemoteClientInitializationService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.CodeAnalysis.Razor.Remote;
7-
using Microsoft.CodeAnalysis.Remote.Razor.SemanticTokens;
88

99
namespace Microsoft.CodeAnalysis.Remote.Razor;
1010

@@ -16,9 +16,8 @@ protected override IRemoteClientInitializationService CreateService(in ServiceAr
1616
=> new RemoteClientInitializationService(in args);
1717
}
1818

19-
private readonly RemoteClientCapabilitiesService _remoteClientCapabilitiesService = args.ExportProvider.GetExportedValue<RemoteClientCapabilitiesService>();
2019
private readonly RemoteLanguageServerFeatureOptions _remoteLanguageServerFeatureOptions = args.ExportProvider.GetExportedValue<RemoteLanguageServerFeatureOptions>();
21-
private readonly RemoteSemanticTokensLegendService _remoteSemanticTokensLegendService = args.ExportProvider.GetExportedValue<RemoteSemanticTokensLegendService>();
20+
private readonly IEnumerable<ILspLifetimeService> _lspLifetimeServices = args.ExportProvider.GetExportedValues<ILspLifetimeService>();
2221

2322
public ValueTask InitializeAsync(RemoteClientInitializationOptions options, CancellationToken cancellationToken)
2423
=> RunServiceAsync(ct =>
@@ -31,8 +30,11 @@ public ValueTask InitializeAsync(RemoteClientInitializationOptions options, Canc
3130
public ValueTask InitializeLSPAsync(RemoteClientLSPInitializationOptions options, CancellationToken cancellationToken)
3231
=> RunServiceAsync(ct =>
3332
{
34-
_remoteSemanticTokensLegendService.SetLegend(options.TokenTypes, options.TokenModifiers);
35-
_remoteClientCapabilitiesService.SetCapabilities(options.ClientCapabilities);
33+
foreach (var service in _lspLifetimeServices)
34+
{
35+
service.OnLspInitialized(options);
36+
}
37+
3638
return default;
3739
},
3840
cancellationToken);

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/SemanticTokens/RemoteSemanticTokensLegendService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Composition;
5+
using Microsoft.CodeAnalysis.Razor.Remote;
56
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
67
using SemanticTokenModifiers = Microsoft.CodeAnalysis.Razor.SemanticTokens.SemanticTokenModifiers;
78
using SemanticTokenTypes = Microsoft.CodeAnalysis.Razor.SemanticTokens.SemanticTokenTypes;
@@ -10,8 +11,8 @@ namespace Microsoft.CodeAnalysis.Remote.Razor.SemanticTokens;
1011

1112
[Shared]
1213
[Export(typeof(ISemanticTokensLegendService))]
13-
[Export(typeof(RemoteSemanticTokensLegendService))]
14-
internal sealed class RemoteSemanticTokensLegendService : ISemanticTokensLegendService
14+
[Export(typeof(ILspLifetimeService))]
15+
internal sealed class RemoteSemanticTokensLegendService : ISemanticTokensLegendService, ILspLifetimeService
1516
{
1617
private SemanticTokenModifiers _tokenModifiers = null!;
1718
private SemanticTokenTypes _tokenTypes = null!;
@@ -20,6 +21,15 @@ internal sealed class RemoteSemanticTokensLegendService : ISemanticTokensLegendS
2021

2122
public SemanticTokenTypes TokenTypes => _tokenTypes;
2223

24+
public void OnLspInitialized(RemoteClientLSPInitializationOptions options)
25+
{
26+
SetLegend(options.TokenTypes, options.TokenModifiers);
27+
}
28+
29+
public void OnLspUninitialized()
30+
{
31+
}
32+
2333
public void SetLegend(string[] tokenTypes, string[] tokenModifiers)
2434
{
2535
_tokenTypes = new SemanticTokenTypes(tokenTypes);

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/CohostEndpointTestBase.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
using Microsoft.CodeAnalysis.CSharp;
1717
using Microsoft.CodeAnalysis.Diagnostics;
1818
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
19+
using Microsoft.CodeAnalysis.Razor.Protocol;
1920
using Microsoft.CodeAnalysis.Razor.Remote;
2021
using Microsoft.CodeAnalysis.Razor.Workspaces;
2122
using Microsoft.CodeAnalysis.Remote.Razor;
2223
using Microsoft.CodeAnalysis.Remote.Razor.Logging;
23-
using Microsoft.CodeAnalysis.Remote.Razor.SemanticTokens;
2424
using Microsoft.CodeAnalysis.Text;
2525
using Microsoft.NET.Sdk.Razor.SourceGenerators;
2626
using Microsoft.VisualStudio.Composition;
@@ -43,8 +43,7 @@ public abstract class CohostEndpointTestBase(ITestOutputHelper testOutputHelper)
4343
private protected TestRemoteServiceInvoker RemoteServiceInvoker => _remoteServiceInvoker.AssumeNotNull();
4444
private protected IFilePathService FilePathService => _filePathService.AssumeNotNull();
4545
private protected RemoteLanguageServerFeatureOptions FeatureOptions => OOPExportProvider.GetExportedValue<RemoteLanguageServerFeatureOptions>();
46-
private protected RemoteClientCapabilitiesService ClientCapabilitiesService => OOPExportProvider.GetExportedValue<RemoteClientCapabilitiesService>();
47-
private protected RemoteSemanticTokensLegendService SemanticTokensLegendService => OOPExportProvider.GetExportedValue<RemoteSemanticTokensLegendService>();
46+
private protected RemoteClientCapabilitiesService ClientCapabilitiesService => (RemoteClientCapabilitiesService)OOPExportProvider.GetExportedValue<IClientCapabilitiesService>();
4847

4948
/// <summary>
5049
/// The export provider for Razor OOP services (not Roslyn)
@@ -145,8 +144,23 @@ private protected void UpdateClientInitializationOptions(Func<RemoteClientInitia
145144
private protected void UpdateClientLSPInitializationOptions(Func<RemoteClientLSPInitializationOptions, RemoteClientLSPInitializationOptions> mutation)
146145
{
147146
_clientLSPInitializationOptions = mutation(_clientLSPInitializationOptions);
148-
ClientCapabilitiesService.SetCapabilities(_clientLSPInitializationOptions.ClientCapabilities);
149-
SemanticTokensLegendService.SetLegend(_clientLSPInitializationOptions.TokenTypes, _clientLSPInitializationOptions.TokenModifiers);
147+
148+
var lifetimeServices = OOPExportProvider.GetExportedValues<ILspLifetimeService>();
149+
foreach (var service in lifetimeServices)
150+
{
151+
service.OnLspInitialized(_clientLSPInitializationOptions);
152+
}
153+
}
154+
155+
protected override Task DisposeAsync()
156+
{
157+
var lifetimeServices = OOPExportProvider.GetExportedValues<ILspLifetimeService>();
158+
foreach (var service in lifetimeServices)
159+
{
160+
service.OnLspUninitialized();
161+
}
162+
163+
return Task.CompletedTask;
150164
}
151165

152166
private protected virtual TestComposition ConfigureRoslynDevenvComposition(TestComposition composition)

0 commit comments

Comments
 (0)