Skip to content

Commit b95e00f

Browse files
Add misc project when server is initialized
This change updates SnapshotResolver with an InitializeAsync method that is called when the LSP "initialized" endpoint is handled. When initialized, the SnapshotResolver adds the misc project to the ProjectSnapshotManager. Tests have been updated to call InitializeAsync whenever a SnapshotResolver is created.
1 parent 77b30be commit b95e00f

18 files changed

+296
-167
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT license. See License.txt in the project root for license information.
3+
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Razor.Language;
7+
using Microsoft.VisualStudio.LanguageServer.Protocol;
8+
9+
namespace Microsoft.AspNetCore.Razor.LanguageServer.Hover;
10+
11+
internal sealed partial class HoverService
12+
{
13+
internal TestAccessor GetTestAccessor() => new(this);
14+
15+
internal sealed class TestAccessor(HoverService instance)
16+
{
17+
public Task<VSInternalHover?> GetHoverInfoAsync(
18+
string documentFilePath,
19+
RazorCodeDocument codeDocument,
20+
SourceLocation location,
21+
VSInternalClientCapabilities clientCapabilities,
22+
CancellationToken cancellationToken)
23+
=> instance.GetHoverInfoAsync(documentFilePath, codeDocument, location, clientCapabilities, cancellationToken);
24+
}
25+
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Hover/HoverService.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace Microsoft.AspNetCore.Razor.LanguageServer.Hover;
2626

27-
internal sealed class HoverService(
27+
internal sealed partial class HoverService(
2828
LSPTagHelperTooltipFactory lspTagHelperTooltipFactory,
2929
VSLSPTagHelperTooltipFactory vsLspTagHelperTooltipFactory,
3030
IRazorDocumentMappingService mappingService,
@@ -94,15 +94,13 @@ internal sealed class HoverService(
9494
return response;
9595
}
9696

97-
public TestAccessor GetTestAccessor() => new(this);
98-
99-
public async Task<VSInternalHover?> GetHoverInfoAsync(string documentFilePath, RazorCodeDocument codeDocument, SourceLocation location, VSInternalClientCapabilities clientCapabilities, CancellationToken cancellationToken)
97+
private async Task<VSInternalHover?> GetHoverInfoAsync(
98+
string documentFilePath,
99+
RazorCodeDocument codeDocument,
100+
SourceLocation location,
101+
VSInternalClientCapabilities clientCapabilities,
102+
CancellationToken cancellationToken)
100103
{
101-
if (codeDocument is null)
102-
{
103-
throw new ArgumentNullException(nameof(codeDocument));
104-
}
105-
106104
var syntaxTree = codeDocument.GetSyntaxTree();
107105

108106
var owner = syntaxTree.Root.FindInnermostNode(location.AbsoluteIndex);
@@ -350,10 +348,4 @@ private static VisualStudioMarkupKind GetHoverContentFormat(ClientCapabilities c
350348
var hoverKind = hoverContentFormat?.Contains(VisualStudioMarkupKind.Markdown) == true ? VisualStudioMarkupKind.Markdown : VisualStudioMarkupKind.PlainText;
351349
return hoverKind;
352350
}
353-
354-
public class TestAccessor(HoverService service)
355-
{
356-
public Task<VSInternalHover?> GetHoverInfoAsync(string documentFilePath, RazorCodeDocument codeDocument, SourceLocation location, VSInternalClientCapabilities clientCapabilities, CancellationToken cancellationToken)
357-
=> service.GetHoverInfoAsync(documentFilePath, codeDocument, location, clientCapabilities, cancellationToken);
358-
}
359351
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/ISnapshotResolver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
1010

1111
internal interface ISnapshotResolver
1212
{
13+
Task InitializeAsync(CancellationToken cancellationToken);
14+
1315
/// <summary>
1416
/// Finds all the projects where the document path starts with the path of the folder that contains the project file.
1517
/// </summary>

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/SnapshotResolver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public SnapshotResolver(IProjectSnapshotManager projectManager, ILoggerFactory l
3333
MiscellaneousHostProject = new HostProject(normalizedPath, normalizedPath, FallbackRazorConfiguration.Latest, rootNamespace: null, "Miscellaneous Files");
3434
}
3535

36+
public Task InitializeAsync(CancellationToken cancellationToken)
37+
{
38+
// This is called when the language server is initialized.
39+
40+
return _projectManager.UpdateAsync(
41+
(updater, miscHostProject) => updater.ProjectAdded(miscHostProject),
42+
state: MiscellaneousHostProject,
43+
cancellationToken);
44+
}
45+
3646
/// <inheritdoc/>
3747
public ImmutableArray<IProjectSnapshot> FindPotentialProjects(string documentFilePath)
3848
{

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorInitializedEndpoint.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
7+
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
78
using Microsoft.CodeAnalysis.Razor.Protocol;
89
using Microsoft.CommonLanguageServerProtocol.Framework;
910
using Microsoft.VisualStudio.LanguageServer.Protocol;
@@ -20,6 +21,9 @@ public async Task HandleNotificationAsync(InitializedParams request, RazorReques
2021
var onStartedItems = requestContext.LspServices.GetRequiredServices<IOnInitialized>();
2122
var capabilitiesService = requestContext.GetRequiredService<IClientCapabilitiesService>();
2223

24+
var snapshotResolver = requestContext.LspServices.GetRequiredService<ISnapshotResolver>();
25+
await snapshotResolver.InitializeAsync(cancellationToken).ConfigureAwait(false);
26+
2327
var fileChangeDetectorManager = requestContext.LspServices.GetRequiredService<RazorFileChangeDetectorManager>();
2428
await fileChangeDetectorManager.InitializedAsync(cancellationToken).ConfigureAwait(false);
2529

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/DefaultLSPTagHelperTooltipFactoryTest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public async Task TryCreateTooltip_Markup_NoAssociatedTagHelperDescriptions_Retu
6767
{
6868
// Arrange
6969
var snapshotResolver = new TestSnapshotResolver();
70+
await snapshotResolver.InitializeAsync(DisposalToken);
7071
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
7172
var elementDescription = AggregateBoundElementDescription.Empty;
7273

@@ -82,6 +83,7 @@ public async Task TryCreateTooltip_Markup_Element_SingleAssociatedTagHelper_Retu
8283
{
8384
// Arrange
8485
var snapshotResolver = new TestSnapshotResolver();
86+
await snapshotResolver.InitializeAsync(DisposalToken);
8587
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
8688
var associatedTagHelperInfos = new[]
8789
{
@@ -104,6 +106,7 @@ public async Task TryCreateTooltip_Markup_Element_PlainText_NoBold()
104106
{
105107
// Arrange
106108
var snapshotResolver = new TestSnapshotResolver();
109+
await snapshotResolver.InitializeAsync(DisposalToken);
107110
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
108111
var associatedTagHelperInfos = new[]
109112
{
@@ -123,10 +126,11 @@ public async Task TryCreateTooltip_Markup_Element_PlainText_NoBold()
123126
}
124127

125128
[Fact]
126-
public void TryCreateTooltip_Markup_Attribute_PlainText_NoBold()
129+
public async Task TryCreateTooltip_Markup_Attribute_PlainText_NoBold()
127130
{
128131
// Arrange
129132
var snapshotResolver = new TestSnapshotResolver();
133+
await snapshotResolver.InitializeAsync(DisposalToken);
130134
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
131135
var associatedAttributeDescriptions = new[]
132136
{
@@ -154,6 +158,7 @@ public async Task TryCreateTooltip_Markup_Element_MultipleAssociatedTagHelpers_R
154158
{
155159
// Arrange
156160
var snapshotResolver = new TestSnapshotResolver();
161+
await snapshotResolver.InitializeAsync(DisposalToken);
157162
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
158163
var associatedTagHelperInfos = new[]
159164
{
@@ -178,10 +183,11 @@ public async Task TryCreateTooltip_Markup_Element_MultipleAssociatedTagHelpers_R
178183
}
179184

180185
[Fact]
181-
public void TryCreateTooltip_Markup_Attribute_SingleAssociatedAttribute_ReturnsTrue()
186+
public async Task TryCreateTooltip_Markup_Attribute_SingleAssociatedAttribute_ReturnsTrue()
182187
{
183188
// Arrange
184189
var snapshotResolver = new TestSnapshotResolver();
190+
await snapshotResolver.InitializeAsync(DisposalToken);
185191
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
186192
var associatedAttributeDescriptions = new[]
187193
{
@@ -205,10 +211,11 @@ public void TryCreateTooltip_Markup_Attribute_SingleAssociatedAttribute_ReturnsT
205211
}
206212

207213
[Fact]
208-
public void TryCreateTooltip_Markup_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
214+
public async Task TryCreateTooltip_Markup_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
209215
{
210216
// Arrange
211217
var snapshotResolver = new TestSnapshotResolver();
218+
await snapshotResolver.InitializeAsync(DisposalToken);
212219
var descriptionFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
213220
var associatedAttributeDescriptions = new[]
214221
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/DefaultVSLSPTagHelperTooltipFactoryTest.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public async Task TryCreateTooltip_ClassifiedTextElement_NoAssociatedTagHelperDe
171171
{
172172
// Arrange
173173
var snapshotResolver = new TestSnapshotResolver();
174+
await snapshotResolver.InitializeAsync(DisposalToken);
174175
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
175176
var elementDescription = AggregateBoundElementDescription.Empty;
176177

@@ -186,6 +187,7 @@ public async Task TryCreateTooltip_ClassifiedTextElement_Element_SingleAssociate
186187
{
187188
// Arrange
188189
var snapshotResolver = new TestSnapshotResolver();
190+
await snapshotResolver.InitializeAsync(DisposalToken);
189191
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
190192
var associatedTagHelperInfos = new[]
191193
{
@@ -227,6 +229,7 @@ public async Task TryCreateTooltip_ClassifiedTextElement_Element_NamespaceContai
227229
{
228230
// Arrange
229231
var snapshotResolver = new TestSnapshotResolver();
232+
await snapshotResolver.InitializeAsync(DisposalToken);
230233
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
231234
var associatedTagHelperInfos = new[]
232235
{
@@ -267,6 +270,7 @@ public async Task TryCreateTooltip_ClassifiedTextElement_Element_MultipleAssocia
267270
{
268271
// Arrange
269272
var snapshotResolver = new TestSnapshotResolver();
273+
await snapshotResolver.InitializeAsync(DisposalToken);
270274
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
271275
var associatedTagHelperInfos = new[]
272276
{
@@ -317,10 +321,11 @@ public async Task TryCreateTooltip_ClassifiedTextElement_Element_MultipleAssocia
317321
}
318322

319323
[Fact]
320-
public void TryCreateTooltip_ClassifiedTextElement_NoAssociatedAttributeDescriptions_ReturnsFalse()
324+
public async Task TryCreateTooltip_ClassifiedTextElement_NoAssociatedAttributeDescriptions_ReturnsFalse()
321325
{
322326
// Arrange
323327
var snapshotResolver = new TestSnapshotResolver();
328+
await snapshotResolver.InitializeAsync(DisposalToken);
324329
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
325330
var elementDescription = AggregateBoundAttributeDescription.Empty;
326331

@@ -333,10 +338,11 @@ public void TryCreateTooltip_ClassifiedTextElement_NoAssociatedAttributeDescript
333338
}
334339

335340
[Fact]
336-
public void TryCreateTooltip_ClassifiedTextElement_Attribute_SingleAssociatedAttribute_ReturnsTrue_NestedTypes()
341+
public async Task TryCreateTooltip_ClassifiedTextElement_Attribute_SingleAssociatedAttribute_ReturnsTrue_NestedTypes()
337342
{
338343
// Arrange
339344
var snapshotResolver = new TestSnapshotResolver();
345+
await snapshotResolver.InitializeAsync(DisposalToken);
340346
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
341347
var associatedAttributeDescriptions = new[]
342348
{
@@ -382,10 +388,11 @@ public void TryCreateTooltip_ClassifiedTextElement_Attribute_SingleAssociatedAtt
382388
}
383389

384390
[Fact]
385-
public void TryCreateTooltip_ClassifiedTextElement_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
391+
public async Task TryCreateTooltip_ClassifiedTextElement_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
386392
{
387393
// Arrange
388394
var snapshotResolver = new TestSnapshotResolver();
395+
await snapshotResolver.InitializeAsync(DisposalToken);
389396
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
390397
var associatedAttributeDescriptions = new[]
391398
{
@@ -461,6 +468,7 @@ public async Task TryCreateTooltip_ContainerElement_NoAssociatedTagHelperDescrip
461468
{
462469
// Arrange
463470
var snapshotResolver = new TestSnapshotResolver();
471+
await snapshotResolver.InitializeAsync(DisposalToken);
464472
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
465473
var elementDescription = AggregateBoundElementDescription.Empty;
466474

@@ -476,6 +484,7 @@ public async Task TryCreateTooltip_ContainerElement_Attribute_MultipleAssociated
476484
{
477485
// Arrange
478486
var snapshotResolver = new TestSnapshotResolver();
487+
await snapshotResolver.InitializeAsync(DisposalToken);
479488
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
480489
var associatedTagHelperInfos = new[]
481490
{
@@ -556,10 +565,11 @@ public async Task TryCreateTooltip_ContainerElement_Attribute_MultipleAssociated
556565
}
557566

558567
[Fact]
559-
public void TryCreateTooltip_ContainerElement_NoAssociatedAttributeDescriptions_ReturnsFalse()
568+
public async Task TryCreateTooltip_ContainerElement_NoAssociatedAttributeDescriptions_ReturnsFalse()
560569
{
561570
// Arrange
562571
var snapshotResolver = new TestSnapshotResolver();
572+
await snapshotResolver.InitializeAsync(DisposalToken);
563573
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
564574
var elementDescription = AggregateBoundAttributeDescription.Empty;
565575

@@ -572,10 +582,11 @@ public void TryCreateTooltip_ContainerElement_NoAssociatedAttributeDescriptions_
572582
}
573583

574584
[Fact]
575-
public void TryCreateTooltip_ContainerElement_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
585+
public async Task TryCreateTooltip_ContainerElement_Attribute_MultipleAssociatedAttributes_ReturnsTrue()
576586
{
577587
// Arrange
578588
var snapshotResolver = new TestSnapshotResolver();
589+
await snapshotResolver.InitializeAsync(DisposalToken);
579590
var descriptionFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
580591
var associatedAttributeDescriptions = new[]
581592
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/LegacyRazorCompletionResolveEndpointTest.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020

2121
namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion;
2222

23-
public class LegacyRazorCompletionResolveEndpointTest : LanguageServerTestBase
23+
public class LegacyRazorCompletionResolveEndpointTest(ITestOutputHelper testOutput) : LanguageServerTestBase(testOutput)
2424
{
25-
private readonly LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory;
26-
private readonly VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory;
27-
private readonly CompletionListCache _completionListCache;
28-
private readonly VSInternalCompletionSetting _completionCapability;
29-
private readonly VSInternalClientCapabilities _defaultClientCapability;
30-
31-
public LegacyRazorCompletionResolveEndpointTest(ITestOutputHelper testOutput)
32-
: base(testOutput)
25+
private LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory;
26+
private VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory;
27+
private CompletionListCache _completionListCache;
28+
private VSInternalCompletionSetting _completionCapability;
29+
private VSInternalClientCapabilities _defaultClientCapability;
30+
31+
protected async override Task InitializeAsync()
3332
{
3433
var snapshotResolver = new TestSnapshotResolver();
34+
await snapshotResolver.InitializeAsync(DisposalToken);
3535
_lspTagHelperTooltipFactory = new Mock<LSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver).Object;
3636
_vsLspTagHelperTooltipFactory = new Mock<VSLSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver).Object;
3737
_completionListCache = new CompletionListCache();
@@ -97,6 +97,7 @@ public async Task Handle_Resolve_DirectiveAttributeCompletion_ReturnsCompletionI
9797
{
9898
// Arrange
9999
var snapshotResolver = new TestSnapshotResolver();
100+
await snapshotResolver.InitializeAsync(DisposalToken);
100101
var lspDescriptionFactory = new Mock<LSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver);
101102
var markdown = new MarkupContent
102103
{
@@ -126,6 +127,7 @@ public async Task Handle_Resolve_DirectiveAttributeParameterCompletion_ReturnsCo
126127
{
127128
// Arrange
128129
var snapshotResolver = new TestSnapshotResolver();
130+
await snapshotResolver.InitializeAsync(DisposalToken);
129131
var descriptionFactory = new Mock<LSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver);
130132
var markdown = new MarkupContent
131133
{
@@ -155,6 +157,7 @@ public async Task Handle_Resolve_TagHelperElementCompletion_ReturnsCompletionIte
155157
{
156158
// Arrange
157159
var snapshotResolver = new TestSnapshotResolver();
160+
await snapshotResolver.InitializeAsync(DisposalToken);
158161
var lspDescriptionFactory = new Mock<LSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver);
159162
var markdown = new MarkupContent
160163
{
@@ -184,6 +187,7 @@ public async Task Handle_Resolve_TagHelperAttribute_ReturnsCompletionItemWithDoc
184187
{
185188
// Arrange
186189
var snapshotResolver = new TestSnapshotResolver();
190+
await snapshotResolver.InitializeAsync(DisposalToken);
187191
var lspDescriptionFactory = new Mock<LSPTagHelperTooltipFactory>(MockBehavior.Strict, snapshotResolver);
188192
var markdown = new MarkupContent
189193
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/RazorCompletionItemResolverTest.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717

1818
namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion;
1919

20-
public class RazorCompletionItemResolverTest : LanguageServerTestBase
20+
public class RazorCompletionItemResolverTest(ITestOutputHelper testOutput) : LanguageServerTestBase(testOutput)
2121
{
22-
private readonly LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory;
23-
private readonly VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory;
24-
private readonly VSInternalCompletionSetting _completionCapability;
25-
private readonly VSInternalClientCapabilities _defaultClientCapability;
26-
private readonly VSInternalClientCapabilities _vsClientCapability;
27-
private readonly AggregateBoundAttributeDescription _attributeDescription;
28-
private readonly AggregateBoundElementDescription _elementDescription;
29-
30-
public RazorCompletionItemResolverTest(ITestOutputHelper testOutput)
31-
: base(testOutput)
22+
private LSPTagHelperTooltipFactory _lspTagHelperTooltipFactory;
23+
private VSLSPTagHelperTooltipFactory _vsLspTagHelperTooltipFactory;
24+
private VSInternalCompletionSetting _completionCapability;
25+
private VSInternalClientCapabilities _defaultClientCapability;
26+
private VSInternalClientCapabilities _vsClientCapability;
27+
private AggregateBoundAttributeDescription _attributeDescription;
28+
private AggregateBoundElementDescription _elementDescription;
29+
30+
protected async override Task InitializeAsync()
3231
{
3332
var snapshotResolver = new TestSnapshotResolver();
33+
await snapshotResolver.InitializeAsync(DisposalToken);
34+
3435
_lspTagHelperTooltipFactory = new DefaultLSPTagHelperTooltipFactory(snapshotResolver);
3536
_vsLspTagHelperTooltipFactory = new DefaultVSLSPTagHelperTooltipFactory(snapshotResolver);
3637
_completionCapability = new VSInternalCompletionSetting()

0 commit comments

Comments
 (0)