Skip to content

Commit a6902b8

Browse files
Copilotdavidwengier
andcommitted
Address code review feedback - return DocumentContentsResponse from service, remove spanLength, use CSharpFormattingPass, parameterize tests
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent 4191d45 commit a6902b8

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,26 @@ internal sealed class CohostGeneratedDocumentContentsEndpoint(
3535

3636
protected override async Task<DocumentContentsResponse?> HandleRequestAsync(DocumentContentsRequest request, TextDocument razorDocument, CancellationToken cancellationToken)
3737
{
38-
string? contents = null;
38+
DocumentContentsResponse? response = null;
3939

4040
switch (request.Kind)
4141
{
4242
case GeneratedDocumentKind.CSharp:
43-
contents = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string?>(
43+
response = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, DocumentContentsResponse?>(
4444
razorDocument.Project.Solution,
4545
(service, solutionInfo, cancellationToken) => service.GetCSharpDocumentTextAsync(solutionInfo, razorDocument.Id, cancellationToken),
4646
cancellationToken).ConfigureAwait(false);
4747
break;
4848

4949
case GeneratedDocumentKind.Html:
50-
contents = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string?>(
50+
response = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, DocumentContentsResponse?>(
5151
razorDocument.Project.Solution,
5252
(service, solutionInfo, cancellationToken) => service.GetHtmlDocumentTextAsync(solutionInfo, razorDocument.Id, cancellationToken),
5353
cancellationToken).ConfigureAwait(false);
5454
break;
5555

5656
case GeneratedDocumentKind.Formatting:
57-
contents = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string?>(
57+
response = await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, DocumentContentsResponse?>(
5858
razorDocument.Project.Solution,
5959
(service, solutionInfo, cancellationToken) => service.GetFormattingDocumentTextAsync(solutionInfo, razorDocument.Id, cancellationToken),
6060
cancellationToken).ConfigureAwait(false);
@@ -64,14 +64,7 @@ internal sealed class CohostGeneratedDocumentContentsEndpoint(
6464
throw new ArgumentException($"Unsupported document kind: {request.Kind}", nameof(request));
6565
}
6666

67-
if (contents == null)
68-
return null;
69-
70-
return new DocumentContentsResponse
71-
{
72-
Contents = contents,
73-
FilePath = razorDocument.FilePath ?? string.Empty
74-
};
67+
return response;
7568
}
7669

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

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ internal sealed class RazorSyntaxNode
1616
[JsonPropertyName("spanEnd")]
1717
public required int SpanEnd { get; set; }
1818

19-
[JsonPropertyName("spanLength")]
20-
public required int SpanLength { get; set; }
21-
2219
[JsonPropertyName("children")]
2320
public required RazorSyntaxNode[] Children { get; set; }
2421
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace Microsoft.CodeAnalysis.Razor.Remote;
99

1010
internal interface IRemoteDevToolsService
1111
{
12-
ValueTask<string?> GetCSharpDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
12+
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetCSharpDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
1313

14-
ValueTask<string?> GetHtmlDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
14+
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetHtmlDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
1515

16-
ValueTask<string?> GetFormattingDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
16+
ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetFormattingDocumentTextAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
1717

1818
ValueTask<string> GetTagHelpersJsonAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
1919

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNetCore.Razor.Language;
99
using Microsoft.AspNetCore.Razor.Language.Syntax;
1010
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
11+
using Microsoft.CodeAnalysis.Razor.Formatting;
1112
using Microsoft.CodeAnalysis.Razor.Remote;
1213
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
1314

@@ -21,7 +22,7 @@ protected override IRemoteDevToolsService CreateService(in ServiceArgs args)
2122
=> new RemoteDevToolsService(in args);
2223
}
2324

24-
public ValueTask<string?> GetCSharpDocumentTextAsync(
25+
public ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetCSharpDocumentTextAsync(
2526
RazorPinnedSolutionInfoWrapper solutionInfo,
2627
DocumentId razorDocumentId,
2728
CancellationToken cancellationToken)
@@ -31,7 +32,7 @@ protected override IRemoteDevToolsService CreateService(in ServiceArgs args)
3132
context => GetCSharpDocumentTextAsync(context, cancellationToken),
3233
cancellationToken);
3334

34-
public ValueTask<string?> GetHtmlDocumentTextAsync(
35+
public ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetHtmlDocumentTextAsync(
3536
RazorPinnedSolutionInfoWrapper solutionInfo,
3637
DocumentId razorDocumentId,
3738
CancellationToken cancellationToken)
@@ -41,7 +42,7 @@ protected override IRemoteDevToolsService CreateService(in ServiceArgs args)
4142
context => GetHtmlDocumentTextAsync(context, cancellationToken),
4243
cancellationToken);
4344

44-
public ValueTask<string?> GetFormattingDocumentTextAsync(
45+
public ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetFormattingDocumentTextAsync(
4546
RazorPinnedSolutionInfoWrapper solutionInfo,
4647
DocumentId razorDocumentId,
4748
CancellationToken cancellationToken)
@@ -71,23 +72,45 @@ public ValueTask<string> GetTagHelpersJsonAsync(
7172
context => GetRazorSyntaxTreeAsync(context, cancellationToken),
7273
cancellationToken);
7374

74-
private async ValueTask<string?> GetCSharpDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
75+
private async ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetCSharpDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
7576
{
7677
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
77-
return codeDocument.GetCSharpSourceText().ToString();
78+
var contents = codeDocument.GetCSharpSourceText().ToString();
79+
var filePath = documentContext.Snapshot.FilePath + ".g.cs";
80+
81+
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse
82+
{
83+
Contents = contents,
84+
FilePath = filePath
85+
};
7886
}
7987

80-
private async ValueTask<string?> GetHtmlDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
88+
private async ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetHtmlDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
8189
{
8290
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
83-
return codeDocument.GetHtmlSourceText().ToString();
91+
var contents = codeDocument.GetHtmlSourceText().ToString();
92+
var filePath = documentContext.Snapshot.FilePath + ".g.html";
93+
94+
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse
95+
{
96+
Contents = contents,
97+
FilePath = filePath
98+
};
8499
}
85100

86-
private async ValueTask<string?> GetFormattingDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
101+
private async ValueTask<Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse?> GetFormattingDocumentTextAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
87102
{
88-
// For formatting, we typically want the C# generated document
89103
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
90-
return codeDocument.GetCSharpSourceText().ToString();
104+
#pragma warning disable CS0618 // Type or member is obsolete
105+
var contents = CSharpFormattingPass.GetFormattingDocumentContentsForSyntaxVisualizer(codeDocument);
106+
#pragma warning restore CS0618 // Type or member is obsolete
107+
var filePath = documentContext.Snapshot.FilePath + ".formatting.cs";
108+
109+
return new Microsoft.CodeAnalysis.Razor.Protocol.DevTools.DocumentContentsResponse
110+
{
111+
Contents = contents,
112+
FilePath = filePath
113+
};
91114
}
92115

93116
private async ValueTask<string> GetTagHelpersJsonAsync(RemoteDocumentContext documentContext, CancellationToken cancellationToken)
@@ -119,7 +142,6 @@ private static Microsoft.CodeAnalysis.Razor.Protocol.DevTools.RazorSyntaxNode Co
119142
Kind = node.Kind.ToString(),
120143
SpanStart = node.SpanStart,
121144
SpanEnd = node.Span.End,
122-
SpanLength = node.Span.Length,
123145
Children = children
124146
};
125147
}

src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.Test/Cohost/Shared/CohostDevToolsEndpointTest.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task GeneratedDocumentContents_CSharp()
2424
<div>@message</div>
2525
""";
2626

27-
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharp);
27+
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharp, "message", ".g.cs");
2828
}
2929

3030
[Fact]
@@ -37,7 +37,7 @@ public async Task GeneratedDocumentContents_Html()
3737
<div>@message</div>
3838
""";
3939

40-
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.Html);
40+
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.Html, "div", ".g.html");
4141
}
4242

4343
[Fact]
@@ -50,7 +50,7 @@ public async Task GeneratedDocumentContents_Formatting()
5050
<div>@message</div>
5151
""";
5252

53-
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.Formatting);
53+
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.Formatting, "message", ".formatting.cs");
5454
}
5555

5656
[Fact]
@@ -102,11 +102,11 @@ public async Task GetSyntaxTree_ReturnsTree()
102102
Assert.NotNull(result);
103103
Assert.NotNull(result.Root);
104104
Assert.NotEmpty(result.Root.Kind);
105-
Assert.True(result.Root.SpanLength > 0);
105+
Assert.True(result.Root.SpanEnd > result.Root.SpanStart);
106106
Assert.NotNull(result.Root.Children);
107107
}
108108

109-
private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedDocumentKind kind)
109+
private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedDocumentKind kind, string expectedContentSubstring, string expectedFileExtension)
110110
{
111111
var razorDocument = await CreateDocumentAsync(input);
112112
var endpoint = new CohostGeneratedDocumentContentsEndpoint(IncompatibleProjectService, RemoteServiceInvoker);
@@ -123,19 +123,10 @@ private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedD
123123
Assert.NotEmpty(result.Contents);
124124
Assert.NotEmpty(result.FilePath);
125125

126-
// Verify content based on kind
127-
switch (kind)
128-
{
129-
case GeneratedDocumentKind.CSharp:
130-
Assert.Contains("message", result.Contents);
131-
break;
132-
case GeneratedDocumentKind.Html:
133-
Assert.Contains("div", result.Contents);
134-
break;
135-
case GeneratedDocumentKind.Formatting:
136-
// Formatting document should contain C# content
137-
Assert.Contains("message", result.Contents);
138-
break;
139-
}
126+
// Verify content contains expected substring
127+
Assert.Contains(expectedContentSubstring, result.Contents);
128+
129+
// Verify file path ends with expected extension
130+
Assert.EndsWith(expectedFileExtension, result.FilePath);
140131
}
141132
}

0 commit comments

Comments
 (0)