Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to view the formatting document to the syntax visualizer #11463

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,10 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
Expand Down Expand Up @@ -171,4 +173,8 @@ private async Task<SourceText> FormatCSharpAsync(SourceText generatedCSharpText,

return generatedCSharpText.WithChanges(csharpChanges);
}

[Obsolete("Only for the syntax visualizer, do not call")]
internal static string GetFormattingDocumentContentsForSyntaxVisualizer(RazorCodeDocument codeDocument)
=> CSharpDocumentGenerator.Generate(codeDocument, new()).SourceText.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Controls;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Serialization.Json;
using Microsoft.CodeAnalysis.Razor.Formatting.New;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage.Extensions;
using Microsoft.VisualStudio.Razor;
Expand Down Expand Up @@ -108,6 +109,30 @@ void IDisposable.Dispose()
}
}

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

EnsureInitialized();

if (_fileUriProvider.TryGet(_activeWpfTextView.TextBuffer, out var hostDocumentUri))
{
var codeDocument = GetCodeDocument();
if (codeDocument is null)
{
return;
}

#pragma warning disable CS0618 // Type or member is obsolete
var formattingDocument = CSharpFormattingPass.GetFormattingDocumentContentsForSyntaxVisualizer(codeDocument);
#pragma warning restore CS0618 // Type or member is obsolete
OpenGeneratedCode(hostDocumentUri.AbsoluteUri + ".formatting.cs", formattingDocument);
}
}

public void ShowGeneratedCode()
{
if (_activeWpfTextView is 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 CmdIdShowAllTagHelpers = 0x0113;
private const int CmdIdShowInScopeTagHelpers = 0x0114;
private const int CmdIdShowReferencedTagHelpers = 0x0115;
private const int CmdidShowFormattingDocument = 0x0116;

private MenuCommand? _showSourceMappingsCommand;
private SyntaxVisualizerControl _visualizerControl => (SyntaxVisualizerControl)Content;
Expand Down Expand Up @@ -82,6 +83,7 @@ internal void InitializeCommands(IMenuCommandService mcs, Guid guidSyntaxVisuali
};

mcs.AddCommand(_showSourceMappingsCommand);
mcs.AddCommand(new MenuCommand(ShowFormattingDocument, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdidShowFormattingDocument)));
mcs.AddCommand(new MenuCommand(ShowGeneratedCode, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowGeneratedCode)));
mcs.AddCommand(new MenuCommand(ShowGeneratedHtml, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowGeneratedHtml)));
mcs.AddCommand(new MenuCommand(ShowAllTagHelpers, new CommandID(guidSyntaxVisualizerMenuCmdSet, CmdIdShowAllTagHelpers)));
Expand All @@ -100,6 +102,11 @@ private void ShowSourceMappings(object sender, EventArgs e)
}
}

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

private void ShowGeneratedCode(object sender, EventArgs e)
{
_visualizerControl.ShowGeneratedCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
</Strings>
</Button>

<Button guid="guidSyntaxVisualizerMenuCmdSet" id="cmdidShowFormattingDocument" priority="0x202" type="Button">
<Parent guid="guidSyntaxVisualizerMenuCmdSet" id="grpidRazorSyntaxVisualizer" />
<Strings>
<ButtonText>Show Formatting Document</ButtonText>
</Strings>
</Button>

<Button guid="guidSyntaxVisualizerMenuCmdSet" id="cmdIdShowAllTagHelpers" priority="0x203" type="Button">
<Parent guid="guidSyntaxVisualizerMenuCmdSet" id="grpidRazorSyntaxVisualizer" />
<Strings>
Expand Down Expand Up @@ -140,6 +147,7 @@
<IDSymbol name="cmdIdShowAllTagHelpers" value="0x0113" />
<IDSymbol name="cmdIdShowInScopeTagHelpers" value="0x0114" />
<IDSymbol name="cmdIdShowReferencedTagHelpers" value="0x0115" />
<IDSymbol name="cmdidShowFormattingDocument" value="0x0116" />
</GuidSymbol>
</Symbols>
</CommandTable>

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.