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

Moving formatting service to common layer #10761

Merged
merged 10 commits into from
Aug 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Razor.Workspaces;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.AutoInsert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Threading;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -49,7 +50,7 @@ public static TextEdit[] CreateFormattedTextEdit(RazorCodeDocument code, string
|| !csharpCodeBlock.Children.TryGetCloseBraceNode(out var closeBrace))
{
// No well-formed @code block exists. Generate the method within an @code block at the end of the file and conduct manual formatting.
var indentedMethod = FormattingUtilities.AddIndentationToMethod(templateWithMethodSignature, options, startingIndent: 0);
var indentedMethod = FormattingUtilities.AddIndentationToMethod(templateWithMethodSignature, options.TabSize, options.InsertSpaces, startingIndent: 0);
var codeBlockStartText = "@code {" + Environment.NewLine;
var sourceText = code.Source.Text;
var lastCharacterLocation = sourceText.Lines[^1];
Expand Down Expand Up @@ -112,7 +113,8 @@ private static string FormatMethodInCodeBlock(
var numCharacterBefore = codeBlockSourceLocation.CharacterIndex - 5;
var formattedGeneratedMethod = FormattingUtilities.AddIndentationToMethod(
method,
options,
options.TabSize,
options.InsertSpaces,
codeBlockStartAbsoluteIndex,
numCharacterBefore,
source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down Expand Up @@ -107,7 +108,8 @@ razorClassName is null ||
var classLocationLineSpan = @class.GetLocation().GetLineSpan();
var formattedMethod = FormattingUtilities.AddIndentationToMethod(
templateWithMethodSignature,
_razorLSPOptionsMonitor.CurrentValue,
_razorLSPOptionsMonitor.CurrentValue.TabSize,
_razorLSPOptionsMonitor.CurrentValue.InsertSpaces,
@class.SpanStart,
classLocationLineSpan.StartLinePosition.Character,
content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.AspNetCore.Razor.ProjectEngineHost;
using Microsoft.CodeAnalysis.Razor.Completion;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
Expand Down Expand Up @@ -63,10 +64,10 @@ public static void AddFormattingServices(this IServiceCollection services)
// Formatting Passes
services.AddSingleton<IFormattingPass, HtmlFormattingPass>();
services.AddSingleton<IFormattingPass, CSharpFormattingPass>();
services.AddSingleton<IFormattingPass, CSharpOnTypeFormattingPass>();
services.AddSingleton<IFormattingPass, LspCSharpOnTypeFormattingPass>();
services.AddSingleton<IFormattingPass, FormattingDiagnosticValidationPass>();
services.AddSingleton<IFormattingPass, FormattingContentValidationPass>();
services.AddSingleton<IFormattingPass, RazorFormattingPass>();
services.AddSingleton<IFormattingPass, LspRazorFormattingPass>();

services.AddHandlerWithCapabilities<DocumentFormattingEndpoint>();
services.AddHandlerWithCapabilities<DocumentOnTypeFormattingEndpoint>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.AspNetCore.Razor.TextDifferencing;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Formatting;
using Microsoft.CodeAnalysis.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Text;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal sealed class LspCSharpOnTypeFormattingPass(
IDocumentMappingService documentMappingService,
ILoggerFactory loggerFactory)
: CSharpOnTypeFormattingPassBase(documentMappingService, loggerFactory)
{

protected override async Task<TextEdit[]> AddUsingStatementEditsIfNecessaryAsync(CodeAnalysis.Razor.Formatting.FormattingContext context, RazorCodeDocument codeDocument, SourceText csharpText, TextEdit[] textEdits, SourceText originalTextWithChanges, TextEdit[] finalEdits, CancellationToken cancellationToken)
{
if (context.AutomaticallyAddUsings)
{
// Because we need to parse the C# code twice for this operation, lets do a quick check to see if its even necessary
if (textEdits.Any(e => e.NewText.IndexOf("using") != -1))
{
var usingStatementEdits = await AddUsingsCodeActionProviderHelper.GetUsingStatementEditsAsync(codeDocument, csharpText, originalTextWithChanges, cancellationToken).ConfigureAwait(false);
alexgav marked this conversation as resolved.
Show resolved Hide resolved
finalEdits = [.. usingStatementEdits, .. finalEdits];
}
}

return finalEdits;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal sealed class LspRazorFormattingPass(
IDocumentMappingService documentMappingService,
RazorLSPOptionsMonitor optionsMonitor)
: RazorFormattingPassBase(documentMappingService)
{
protected override bool CodeBlockBraceOnNextLine => optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,3 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal static class ILoggerExtensions
{
public static bool TestOnlyLoggingEnabled = false;

[Conditional("DEBUG")]
public static void LogTestOnly(this ILogger logger, ref TestLogMessageInterpolatedStringHandler handler)
{
if (TestOnlyLoggingEnabled)
{
logger.Log(LogLevel.Debug, handler.ToString(), exception: null);
}
}
}

[InterpolatedStringHandler]
internal ref struct TestLogMessageInterpolatedStringHandler
{
private PooledObject<StringBuilder> _builder;

public TestLogMessageInterpolatedStringHandler(int literalLength, int _, out bool isEnabled)
{
isEnabled = ILoggerExtensions.TestOnlyLoggingEnabled;
if (isEnabled)
{
_builder = StringBuilderPool.GetPooledObject();
_builder.Object.EnsureCapacity(literalLength);
}
}

public void AppendLiteral(string s)
{
_builder.Object.Append(s);
}

public void AppendFormatted<T>(T t)
{
_builder.Object.Append(t?.ToString() ?? "[null]");
}

public void AppendFormatted<T>(T t, string format)
{
_builder.Object.AppendFormat(format, t);
}

public override string ToString()
{
var result = _builder.Object.ToString();
_builder.Dispose();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Completion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,21 @@
<data name="Create_Component_FromTag_Title" xml:space="preserve">
<value>Create component from tag</value>
</data>
<data name="Diagnostics_after" xml:space="preserve">
<value>Diagnostics after:</value>
</data>
<data name="Diagnostics_before" xml:space="preserve">
<value>Diagnostics before:</value>
</data>
<data name="Document_Not_Found" xml:space="preserve">
<value>Document {0} was not found.</value>
</data>
<data name="Edit_at_adds" xml:space="preserve">
<value>Edit at {0} adds the non-whitespace content '{1}'.</value>
</data>
<data name="Edit_at_deletes" xml:space="preserve">
<value>Edit at {0} deletes the non-whitespace content '{1}'.</value>
</data>
<data name="ExtractTo_CodeBehind_Title" xml:space="preserve">
<value>Extract block to code behind</value>
</data>
<data name="File_Externally_Modified" xml:space="preserve">
<value>File was externally modified: {0}</value>
</data>
<data name="Format_operation_changed_diagnostics" xml:space="preserve">
<value>A format operation is being abandoned because it would introduce or remove one of more diagnostics.</value>
</data>
<data name="Format_operation_changed_nonwhitespace" xml:space="preserve">
<value>A format operation is being abandoned because it would add or delete non-whitespace content.</value>
</data>
<data name="Generate_Async_Event_Handler_Title" xml:space="preserve">
<value>Generate Async Event Handler '{0}'</value>
</data>
<data name="Generate_Event_Handler_Title" xml:space="preserve">
<value>Generate Event Handler '{0}'</value>
</data>
<data name="Language_Services_Missing_Service" xml:space="preserve">
<value>Razor language services not configured properly, missing language service '{0}'.</value>
</data>
<data name="Not_Available_In" xml:space="preserve">
<value>Not available in</value>
</data>
Expand Down

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

Loading