Skip to content

Commit 47f13f0

Browse files
Copilotdavidwengier
andcommitted
Address additional feedback - add DevTools files to projitems, rename types to SyntaxVisualizer*, return ImmutableArray for TagHelpers, add System using directives, add EnsureInitialized call
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent 09080f8 commit 47f13f0

File tree

10 files changed

+42
-22
lines changed

10 files changed

+42
-22
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.CohostingShared/DevTools/CohostSyntaxTreeEndpoint.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
2222
internal sealed class CohostSyntaxTreeEndpoint(
2323
IIncompatibleProjectService incompatibleProjectService,
2424
IRemoteServiceInvoker remoteServiceInvoker)
25-
: AbstractCohostDocumentEndpoint<SyntaxTreeRequest, RazorSyntaxTree?>(incompatibleProjectService)
25+
: AbstractCohostDocumentEndpoint<SyntaxTreeRequest, SyntaxVisualizerTree?>(incompatibleProjectService)
2626
{
2727
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
2828

@@ -32,9 +32,9 @@ internal sealed class CohostSyntaxTreeEndpoint(
3232
protected override RazorTextDocumentIdentifier? GetRazorTextDocumentIdentifier(SyntaxTreeRequest request)
3333
=> request.TextDocument.ToRazorTextDocumentIdentifier();
3434

35-
protected override async Task<RazorSyntaxTree?> HandleRequestAsync(SyntaxTreeRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
35+
protected override async Task<SyntaxVisualizerTree?> HandleRequestAsync(SyntaxTreeRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
3636
{
37-
return await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, RazorSyntaxTree?>(
37+
return await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, SyntaxVisualizerTree?>(
3838
razorDocument.Project.Solution,
3939
(service, solutionInfo, cancellationToken) => service.GetRazorSyntaxTreeAsync(solutionInfo, razorDocument.Id, cancellationToken),
4040
cancellationToken).ConfigureAwait(false);
@@ -44,7 +44,7 @@ internal sealed class CohostSyntaxTreeEndpoint(
4444

4545
internal readonly struct TestAccessor(CohostSyntaxTreeEndpoint instance)
4646
{
47-
public Task<RazorSyntaxTree?> HandleRequestAsync(SyntaxTreeRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
47+
public Task<SyntaxVisualizerTree?> HandleRequestAsync(SyntaxTreeRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
4848
=> instance.HandleRequestAsync(request, razorDocument, cancellationToken);
4949
}
5050
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.CohostingShared/DevTools/CohostTagHelpersEndpoint.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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.Immutable;
45
using System.Composition;
6+
using System.Text.Json;
57
using System.Threading;
68
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Razor.Language;
710
using Microsoft.CodeAnalysis;
811
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
912
using Microsoft.CodeAnalysis.Razor.Cohost;
@@ -34,10 +37,17 @@ internal sealed class CohostTagHelpersEndpoint(
3437

3538
protected override async Task<string?> HandleRequestAsync(TagHelpersRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
3639
{
37-
return await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string>(
40+
var tagHelpers = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, ImmutableArray<TagHelperDescriptor>>(
3841
razorDocument.Project.Solution,
3942
(service, solutionInfo, cancellationToken) => service.GetTagHelpersJsonAsync(solutionInfo, razorDocument.Id, request.Kind, cancellationToken),
4043
cancellationToken).ConfigureAwait(false);
44+
45+
if (tagHelpers.IsDefault)
46+
{
47+
return null;
48+
}
49+
50+
return JsonSerializer.Serialize(tagHelpers, new JsonSerializerOptions { WriteIndented = true });
4151
}
4252

4353
internal TestAccessor GetTestAccessor() => new(this);

src/Razor/src/Microsoft.CodeAnalysis.Razor.CohostingShared/Microsoft.CodeAnalysis.Razor.CohostingShared.projitems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@
5757
<Compile Include="$(MSBuildThisFileDirectory)RazorCohostDynamicRegistrationService.cs" />
5858
<Compile Include="$(MSBuildThisFileDirectory)RazorLSPConstants.cs" />
5959
<Compile Include="$(MSBuildThisFileDirectory)WellKnownStartupOrder.cs" />
60+
<Compile Include="$(MSBuildThisFileDirectory)DevTools\CohostGeneratedDocumentContentsEndpoint.cs" />
61+
<Compile Include="$(MSBuildThisFileDirectory)DevTools\CohostTagHelpersEndpoint.cs" />
62+
<Compile Include="$(MSBuildThisFileDirectory)DevTools\CohostSyntaxTreeEndpoint.cs" />
6063
</ItemGroup>
6164
</Project>

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DevTools/DocumentContentsRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
45
using System.Text.Json.Serialization;
56

67
namespace Microsoft.CodeAnalysis.Razor.Protocol.DevTools;
@@ -13,7 +14,7 @@ internal sealed class DocumentContentsRequest
1314
[JsonPropertyName("kind")]
1415
public required GeneratedDocumentKind Kind { get; set; }
1516

16-
public static DocumentContentsRequest Create(System.Uri hostDocumentUri, GeneratedDocumentKind kind)
17+
public static DocumentContentsRequest Create(Uri hostDocumentUri, GeneratedDocumentKind kind)
1718
{
1819
return new DocumentContentsRequest
1920
{

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DevTools/RazorSyntaxNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.CodeAnalysis.Razor.Protocol.DevTools;
77

8-
internal sealed class RazorSyntaxNode
8+
internal sealed class SyntaxVisualizerNode
99
{
1010
[JsonPropertyName("kind")]
1111
public required string Kind { get; set; }
@@ -17,5 +17,5 @@ internal sealed class RazorSyntaxNode
1717
public required int SpanEnd { get; set; }
1818

1919
[JsonPropertyName("children")]
20-
public required RazorSyntaxNode[] Children { get; set; }
20+
public required SyntaxVisualizerNode[] Children { get; set; }
2121
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DevTools/RazorSyntaxTree.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
namespace Microsoft.CodeAnalysis.Razor.Protocol.DevTools;
77

8-
internal sealed class RazorSyntaxTree
8+
internal sealed class SyntaxVisualizerTree
99
{
1010
[JsonPropertyName("root")]
11-
public required RazorSyntaxNode Root { get; set; }
11+
public required SyntaxVisualizerNode Root { get; set; }
1212
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DevTools/TagHelpersRequest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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;
45
using System.Text.Json.Serialization;
56

67
namespace Microsoft.CodeAnalysis.Razor.Protocol.DevTools;
@@ -13,11 +14,11 @@ internal sealed class TagHelpersRequest
1314
[JsonPropertyName("kind")]
1415
public required TagHelpersKind Kind { get; set; }
1516

16-
public static TagHelpersRequest Create(System.Uri hostDocumentUri, TagHelpersKind kind)
17+
public static TagHelpersRequest Create(Uri hostDocumentUri, TagHelpersKind kind)
1718
{
1819
return new TagHelpersRequest
1920
{
20-
TextDocument = new TextDocumentIdentifier { DocumentUri = hostDocumentUri },
21+
TextDocument = new TextDocumentIdentifier { DocumentUri = new(hostDocumentUri) },
2122
Kind = kind
2223
};
2324
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/IRemoteDevToolsService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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.Immutable;
45
using System.Threading;
56
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Razor.Language;
68
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
79

810
namespace Microsoft.CodeAnalysis.Razor.Remote;
@@ -15,7 +17,7 @@ internal interface IRemoteDevToolsService
1517

1618
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetFormattingDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
1719

18-
ValueTask<string> GetTagHelpersJsonAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, Microsoft.CodeAnalysis.Razor.Protocol.DevTools.TagHelpersKind kind, CancellationToken cancellationToken);
20+
ValueTask<ImmutableArray<TagHelperDescriptor>> GetTagHelpersJsonAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, Microsoft.CodeAnalysis.Razor.Protocol.DevTools.TagHelpersKind kind, CancellationToken cancellationToken);
1921

20-
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxTree?> GetRazorSyntaxTreeAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
22+
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerTree?> GetRazorSyntaxTreeAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
2123
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/DevTools/RemoteDevToolsService.cs

Lines changed: 9 additions & 8 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.Collections.Generic;
5+
using System.Collections.Immutable;
56
using System.Linq;
67
using System.Text.Json;
78
using System.Threading;
@@ -53,7 +54,7 @@ protected override IRemoteDevToolsService CreateService(in ServiceArgs args)
5354
context => GetFormattingDocumentTextAsync(context, cancellationToken),
5455
cancellationToken);
5556

56-
public ValueTask<string> GetTagHelpersJsonAsync(
57+
public ValueTask<ImmutableArray<TagHelperDescriptor>> GetTagHelpersJsonAsync(
5758
RazorPinnedSolutionInfoWrapper solutionInfo,
5859
DocumentId razorDocumentId,
5960
Microsoft.CodeAnalysis.Razor.Protocol.DevTools.TagHelpersKind kind,
@@ -64,7 +65,7 @@ public ValueTask<string> GetTagHelpersJsonAsync(
6465
context => GetTagHelpersJsonAsync(context, kind, cancellationToken),
6566
cancellationToken);
6667

67-
public ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxTree?> GetRazorSyntaxTreeAsync(
68+
public ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerTree?> GetRazorSyntaxTreeAsync(
6869
RazorPinnedSolutionInfoWrapper solutionInfo,
6970
DocumentId razorDocumentId,
7071
CancellationToken cancellationToken)
@@ -115,7 +116,7 @@ public ValueTask<string> GetTagHelpersJsonAsync(
115116
};
116117
}
117118

118-
private async ValueTask<string> GetTagHelpersJsonAsync(RemoteDocumentContext documentContext, Microsoft.CodeAnalysis.Razor.Protocol.DevTools.TagHelpersKind kind, CancellationToken cancellationToken)
119+
private async ValueTask<ImmutableArray<TagHelperDescriptor>> GetTagHelpersJsonAsync(RemoteDocumentContext documentContext, Microsoft.CodeAnalysis.Razor.Protocol.DevTools.TagHelpersKind kind, CancellationToken cancellationToken)
119120
{
120121
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
121122
var tagHelpers = kind switch
@@ -127,28 +128,28 @@ private async ValueTask<string> GetTagHelpersJsonAsync(RemoteDocumentContext doc
127128
};
128129

129130
tagHelpers ??= [];
130-
return JsonSerializer.Serialize(tagHelpers, new JsonSerializerOptions { WriteIndented = true });
131+
return tagHelpers.ToImmutableArray();
131132
}
132133

133-
private async ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxTree?> GetRazorSyntaxTreeAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
134+
private async ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerTree?> GetRazorSyntaxTreeAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
134135
{
135136
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
136137
var razorSyntaxTree = codeDocument.GetSyntaxTree();
137138

138139
if (razorSyntaxTree?.Root == null)
139140
return null;
140141

141-
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxTree
142+
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerTree
142143
{
143144
Root = ConvertSyntaxNode(razorSyntaxTree.Root)
144145
};
145146
}
146147

147-
private static Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxNode ConvertSyntaxNode(SyntaxNode node)
148+
private static Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerNode ConvertSyntaxNode(SyntaxNode node)
148149
{
149150
var children = node.ChildNodes().Select(ConvertSyntaxNode).ToArray();
150151

151-
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxNode
152+
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.SyntaxVisualizerNode
152153
{
153154
Kind = node.Kind.ToString(),
154155
SpanStart = node.SpanStart,

src/Razor/src/Microsoft.VisualStudio.RazorExtension/SyntaxVisualizer/SyntaxVisualizerControl.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ public void ShowGeneratedHtml()
216216

217217
private bool ShowGeneratedCode_Cohost(ITextBuffer textBuffer, Uri hostDocumentUri, GeneratedDocumentKind kind)
218218
{
219+
EnsureInitialized();
220+
219221
var request = DocumentContentsRequest.Create(hostDocumentUri, kind);
220222

221223
var response = _joinableTaskFactory.Run(async () =>

0 commit comments

Comments
 (0)