Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ internal sealed class CohostGeneratedDocumentContentsEndpoint(
{
var response = request.Kind switch
{
GeneratedDocumentKind.CSharp => await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string>(
GeneratedDocumentKind.CSharpImplementation => await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string?>(
razorDocument.Project.Solution,
(service, solutionInfo, cancellationToken) => service.GetCSharpDocumentTextAsync(solutionInfo, razorDocument.Id, cancellationToken),
(service, solutionInfo, cancellationToken) => service.GetCSharpDocumentTextAsync(solutionInfo, razorDocument.Id, declarationDocument: false, cancellationToken),
cancellationToken).ConfigureAwait(false),
GeneratedDocumentKind.CSharpDeclaration => await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string?>(
razorDocument.Project.Solution,
(service, solutionInfo, cancellationToken) => service.GetCSharpDocumentTextAsync(solutionInfo, razorDocument.Id, declarationDocument: true, cancellationToken),
cancellationToken).ConfigureAwait(false),
GeneratedDocumentKind.Html => await _remoteServiceInvoker.TryInvokeAsync<IRemoteDevToolsService, string>(
razorDocument.Project.Solution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace Microsoft.CodeAnalysis.Razor.Protocol.DevTools;

internal enum GeneratedDocumentKind
{
CSharp,
CSharpImplementation,
CSharpDeclaration,
Html,
Formatting
}
Comment thread
davidwengier marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.CodeAnalysis.Razor.Remote;

internal interface IRemoteDevToolsService
{
ValueTask<string> GetCSharpDocumentTextAsync(RazorSolutionWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);
ValueTask<string?> GetCSharpDocumentTextAsync(RazorSolutionWrapper solutionInfo, DocumentId razorDocumentId, bool declarationDocument, CancellationToken cancellationToken);

ValueTask<string> GetHtmlDocumentTextAsync(RazorSolutionWrapper solutionInfo, DocumentId razorDocumentId, CancellationToken cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ protected override IRemoteDevToolsService CreateService(in ServiceArgs args)
WriteIndented = true
};

public ValueTask<string> GetCSharpDocumentTextAsync(
public ValueTask<string?> GetCSharpDocumentTextAsync(
RazorSolutionWrapper solutionInfo,
DocumentId razorDocumentId,
bool declarationDocument,
CancellationToken cancellationToken)
=> RunServiceAsync(
solutionInfo,
razorDocumentId,
async context =>
{
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
return codeDocument.GetCSharpSourceText().ToString();
var csharpDocument = codeDocument.GetCSharpDocument(declarationDocument);

return csharpDocument?.Text.ToString();
},
cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<IDSymbol name="cmdIdShowInScopeTagHelpers" value="0x0114" />
<IDSymbol name="cmdIdShowReferencedTagHelpers" value="0x0115" />
<IDSymbol name="cmdidShowFormattingDocument" value="0x0116" />
<IDSymbol name="cmdidShowGeneratedDeclarationCode" value="0x0117" />
</GuidSymbol>

<!-- Command set GUID for Razor nested file commands -->
Expand Down Expand Up @@ -95,6 +96,13 @@
</Strings>
</Button>

<Button guid="guidSyntaxVisualizerMenuCmdSet" id="cmdidShowGeneratedDeclarationCode" priority="0x202" type="Button">
<Parent guid="guidSyntaxVisualizerMenuCmdSet" id="grpidRazorSyntaxVisualizer" />
<Strings>
<ButtonText>Show Generated C# Declaration</ButtonText>
</Strings>
</Button>

<Button guid="guidSyntaxVisualizerMenuCmdSet" id="cmdidShowGeneratedHtml" priority="0x202" type="Button">
<Parent guid="guidSyntaxVisualizerMenuCmdSet" id="grpidRazorSyntaxVisualizer" />
<Strings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,22 @@ public void ShowGeneratedCode()

if (_fileUriProvider.TryGet(_activeWpfTextView.TextBuffer, out var hostDocumentUri))
{
ShowGeneratedCode(_activeWpfTextView.TextBuffer, hostDocumentUri, GeneratedDocumentKind.CSharp);
ShowGeneratedCode(_activeWpfTextView.TextBuffer, hostDocumentUri, GeneratedDocumentKind.CSharpImplementation);
}
}

public void ShowGeneratedDeclarationCode()
{
if (_activeWpfTextView is null)
{
return;
}

EnsureInitialized();

if (_fileUriProvider.TryGet(_activeWpfTextView.TextBuffer, out var hostDocumentUri))
{
ShowGeneratedCode(_activeWpfTextView.TextBuffer, hostDocumentUri, GeneratedDocumentKind.CSharpDeclaration);
}
}

Expand Down Expand Up @@ -203,7 +218,8 @@ private void ShowGeneratedCode(ITextBuffer textBuffer, Uri hostDocumentUri, Gene

var extension = kind switch
{
GeneratedDocumentKind.CSharp => ".g.cs",
GeneratedDocumentKind.CSharpImplementation => ".g.cs",
GeneratedDocumentKind.CSharpDeclaration => ".decl.g.cs",
GeneratedDocumentKind.Html => ".g.html",
GeneratedDocumentKind.Formatting => ".formatting.cs",
_ => null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class SyntaxVisualizerToolWindow : ToolWindowPane
private const int CmdIdShowInScopeTagHelpers = 0x0114;
private const int CmdIdShowReferencedTagHelpers = 0x0115;
private const int CmdidShowFormattingDocument = 0x0116;
private const int CmdIdShowGeneratedDeclarationCode = 0x0117;

private SyntaxVisualizerControl _visualizerControl => (SyntaxVisualizerControl)Content;

Expand Down Expand Up @@ -77,6 +78,7 @@ internal void InitializeCommands(IMenuCommandService mcs, Guid guidSyntaxVisuali

mcs.AddCommand(new MenuCommand(ShowFormattingDocument, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdidShowFormattingDocument)));
mcs.AddCommand(new MenuCommand(ShowGeneratedCode, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowGeneratedCode)));
mcs.AddCommand(new MenuCommand(ShowGeneratedDeclarationCode, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowGeneratedDeclarationCode)));
mcs.AddCommand(new MenuCommand(ShowGeneratedHtml, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowGeneratedHtml)));
mcs.AddCommand(new MenuCommand(ShowAllTagHelpers, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowAllTagHelpers)));
mcs.AddCommand(new MenuCommand(ShowInScopeTagHelpers, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowInScopeTagHelpers)));
Expand All @@ -93,6 +95,11 @@ private void ShowGeneratedCode(object sender, EventArgs e)
_visualizerControl.ShowGeneratedCode();
}

private void ShowGeneratedDeclarationCode(object sender, EventArgs e)
{
_visualizerControl.ShowGeneratedDeclarationCode();
}

private void ShowGeneratedHtml(object sender, EventArgs e)
{
_visualizerControl.ShowGeneratedHtml();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.Razor.Protocol.DevTools;
using Xunit;
Expand All @@ -12,7 +13,7 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
public class CohostGeneratedDocumentContentsEndpointTest(ITestOutputHelper testOutputHelper) : CohostEndpointTestBase(testOutputHelper)
{
[Fact]
public async Task CSharp()
public async Task CSharpImplementation()
{
var input = """
@{
Expand All @@ -21,7 +22,31 @@ public async Task CSharp()
<div>@message</div>
""";

await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharp, """var message = "Hello World";""");
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharpImplementation, """var message = "Hello World";""");
}

[Fact]
public async Task CSharpDeclaration()
{
var input = """
<h1>@Title</h1>

@code {
private string Title { get; set; } = "Hello World";
}
""";

await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharpDeclaration, """private string Title { get; set; } = "Hello World";""");
}

[Fact]
public async Task CSharpDeclaration_ReturnsNullIfDocumentDoesNotExist()
{
var input = """
<h1>Hello World</h1>
""";

await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.CSharpDeclaration, expectedContentSubstring: null, RazorFileKind.Legacy);
Comment thread
davidwengier marked this conversation as resolved.
Resolved
}

[Fact]
Expand Down Expand Up @@ -51,9 +76,9 @@ public async Task Formatting()
await VerifyGeneratedDocumentContentsAsync(input, GeneratedDocumentKind.Formatting, "class @code{");
}

private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedDocumentKind kind, string expectedContentSubstring)
private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedDocumentKind kind, string? expectedContentSubstring, RazorFileKind? fileKind = null)
{
var razorDocument = CreateProjectAndRazorDocument(input);
var razorDocument = CreateProjectAndRazorDocument(input, fileKind);
var endpoint = new CohostGeneratedDocumentContentsEndpoint(IncompatibleProjectService, RemoteServiceInvoker);

var request = new DocumentContentsRequest
Expand All @@ -64,7 +89,14 @@ private async Task VerifyGeneratedDocumentContentsAsync(string input, GeneratedD

var result = await endpoint.GetTestAccessor().HandleRequestAsync(request, razorDocument, DisposalToken);

Assert.NotNull(result);
Assert.Contains(expectedContentSubstring, result);
if (expectedContentSubstring is null)
{
Assert.Null(result);
}
else
{
Assert.NotNull(result);
Assert.Contains(expectedContentSubstring, result);
}
}
}
Loading