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

Initial work to move indentation services down to codestyle layer (step 3/N) #60440

Merged
merged 6 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -35,8 +35,8 @@ public CSharpRemoveUnusedValuesCodeFixProvider()
}

#if CODE_STYLE
protected override ISyntaxFormattingService GetSyntaxFormattingService()
=> CSharpSyntaxFormattingService.Instance;
protected override ISyntaxFormatting GetSyntaxFormatting()
=> CSharpSyntaxFormatting.Instance;
#endif

protected override BlockSyntax WrapWithBlockIfNecessary(IEnumerable<StatementSyntax> statements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOpe
=> CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation);

#if CODE_STYLE
protected override ISyntaxFormattingService GetSyntaxFormattingService()
=> CSharpSyntaxFormattingService.Instance;
protected override ISyntaxFormatting GetSyntaxFormatting()
=> CSharpSyntaxFormatting.Instance;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOpe
=> CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation);

#if CODE_STYLE
protected override ISyntaxFormattingService GetSyntaxFormattingService()
=> CSharpSyntaxFormattingService.Instance;
protected override ISyntaxFormatting GetSyntaxFormatting()
=> CSharpSyntaxFormatting.Instance;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public sealed override ImmutableArray<string> FixableDiagnosticIds
internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.CodeQuality;

#if CODE_STYLE
protected abstract ISyntaxFormattingService GetSyntaxFormattingService();
protected abstract ISyntaxFormatting GetSyntaxFormatting();
#endif
/// <summary>
/// Method to update the identifier token for the local/parameter declaration or reference
Expand Down Expand Up @@ -269,7 +269,7 @@ private static async Task<Document> PreprocessDocumentAsync(Document document, I
protected sealed override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
#if CODE_STYLE
var provider = GetSyntaxFormattingService();
var provider = GetSyntaxFormatting();
var options = provider.GetFormattingOptions(document.Project.AnalyzerOptions.GetAnalyzerOptionSet(editor.OriginalRoot.SyntaxTree, cancellationToken));
#else
var provider = document.Project.Solution.Workspace.Services;
Expand Down Expand Up @@ -832,7 +832,7 @@ private async Task<SyntaxNode> AdjustLocalDeclarationsAsync(

// Run formatter prior to invoking IMoveDeclarationNearReferenceService.
#if CODE_STYLE
var provider = GetSyntaxFormattingService();
var provider = GetSyntaxFormatting();
rootWithTrackedNodes = FormatterHelper.Format(rootWithTrackedNodes, originalDeclStatementsToMoveOrRemove.Select(s => s.Span), provider, options, rules: null, cancellationToken);
#else
var provider = document.Project.Solution.Workspace.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal abstract class AbstractUseConditionalExpressionCodeFixProvider<
protected abstract AbstractFormattingRule GetMultiLineFormattingRule();

#if CODE_STYLE
protected abstract ISyntaxFormattingService GetSyntaxFormattingService();
protected abstract ISyntaxFormatting GetSyntaxFormatting();
#endif

protected abstract TExpressionSyntax ConvertToExpression(IThrowOperation throwOperation);
Expand Down Expand Up @@ -77,7 +77,7 @@ await FixOneAsync(
var rules = new List<AbstractFormattingRule> { GetMultiLineFormattingRule() };

#if CODE_STYLE
var provider = GetSyntaxFormattingService();
var provider = GetSyntaxFormatting();
var options = provider.GetFormattingOptions(document.Project.AnalyzerOptions.GetAnalyzerOptionSet(root.SyntaxTree, cancellationToken));
#else
var provider = document.Project.Solution.Workspace.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
End Sub

#If CODE_STYLE Then
Protected Overrides Function GetSyntaxFormattingService() As ISyntaxFormattingService
Return VisualBasicSyntaxFormattingService.Instance
Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting
Return VisualBasicSyntaxFormatting.Instance
End Function
#End If

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression
End Function

#If CODE_STYLE Then
Protected Overrides Function GetSyntaxFormattingService() As ISyntaxFormattingService
Return VisualBasicSyntaxFormattingService.Instance
Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting
Return VisualBasicSyntaxFormatting.Instance
End Function
#End If
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression
End Function

#If CODE_STYLE Then
Protected Overrides Function GetSyntaxFormattingService() As ISyntaxFormattingService
Return VisualBasicSyntaxFormattingService.Instance
Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting
Return VisualBasicSyntaxFormatting.Instance
End Function
#End If
End Class
Expand Down
6 changes: 2 additions & 4 deletions src/CodeStyle/CSharp/Analyzers/CSharpFormattingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
Expand All @@ -13,7 +11,7 @@ namespace Microsoft.CodeAnalysis.CodeStyle
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpFormattingAnalyzer : AbstractFormattingAnalyzer
{
protected override ISyntaxFormattingService SyntaxFormattingService
=> CSharpSyntaxFormattingService.Instance;
protected override ISyntaxFormatting SyntaxFormatting
=> CSharpSyntaxFormatting.Instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Composition;
using Microsoft.CodeAnalysis.CodeFixes;
Expand All @@ -13,8 +11,7 @@

namespace Microsoft.CodeAnalysis.CodeStyle
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.FixFormatting)]
[Shared]
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.FixFormatting), Shared]
internal class CSharpFormattingCodeFixProvider : AbstractFormattingCodeFixProvider
{
[ImportingConstructor]
Expand All @@ -23,6 +20,6 @@ public CSharpFormattingCodeFixProvider()
{
}

protected override ISyntaxFormattingService SyntaxFormattingService => CSharpSyntaxFormattingService.Instance;
protected override ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance;
}
}
6 changes: 3 additions & 3 deletions src/CodeStyle/Core/Analyzers/AbstractFormattingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
public sealed override DiagnosticAnalyzerCategory GetAnalyzerCategory()
=> DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis;

protected abstract ISyntaxFormattingService SyntaxFormattingService { get; }
protected abstract ISyntaxFormatting SyntaxFormatting { get; }

protected sealed override void InitializeWorker(AnalysisContext context)
=> context.RegisterSyntaxTreeAction(AnalyzeSyntaxTree);

private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
{
var options = SyntaxFormattingService.GetFormattingOptions(context.Options.AnalyzerConfigOptionsProvider.GetOptions(context.Tree));
FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormattingService, Descriptor, options);
var options = SyntaxFormatting.GetFormattingOptions(context.Options.AnalyzerConfigOptionsProvider.GetOptions(context.Tree));
FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormatting, Descriptor, options);
}
}
}
16 changes: 8 additions & 8 deletions src/CodeStyle/Core/Analyzers/Formatting/FormatterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class FormatterHelper
/// <summary>
/// Gets the formatting rules that would be applied if left unspecified.
/// </summary>
internal static IEnumerable<AbstractFormattingRule> GetDefaultFormattingRules(ISyntaxFormattingService syntaxFormattingService)
internal static IEnumerable<AbstractFormattingRule> GetDefaultFormattingRules(ISyntaxFormatting syntaxFormattingService)
=> syntaxFormattingService.GetDefaultFormattingRules();

/// <summary>
Expand All @@ -29,10 +29,10 @@ internal static IEnumerable<AbstractFormattingRule> GetDefaultFormattingRules(IS
/// <param name="options">An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used.</param>
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
public static SyntaxNode Format(SyntaxNode node, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
=> Format(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);

public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
=> Format(node, SpecializedCollections.SingletonEnumerable(spanToFormat), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);

/// <summary>
Expand All @@ -43,16 +43,16 @@ public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxF
/// <param name="options">An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used.</param>
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
=> Format(node, GetAnnotatedSpans(node, annotation), syntaxFormattingService, options, rules, cancellationToken: cancellationToken);

internal static SyntaxNode Format(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
internal static SyntaxNode Format(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
=> GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetFormattedRoot(cancellationToken);

internal static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
internal static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
=> GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetTextChanges(cancellationToken);

internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable<TextSpan> spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable<AbstractFormattingRule>? rules, CancellationToken cancellationToken)
=> syntaxFormattingService.GetFormattingResult(node, spans, options, rules, cancellationToken);

/// <summary>
Expand All @@ -62,7 +62,7 @@ internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerab
/// <param name="options">An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used.</param>
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
=> GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/CodeStyle/Core/Analyzers/FormattingAnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#if CODE_STYLE
using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper;
using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormattingService;
using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormatting;
#else
using FormattingProvider = Microsoft.CodeAnalysis.Host.HostWorkspaceServices;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/CodeStyle/Core/CodeFixes/FormattingCodeFixHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#if CODE_STYLE
using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper;
using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormattingService;
using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormatting;
#else
using Microsoft.CodeAnalysis.Options;
using FormattingProvider = Microsoft.CodeAnalysis.Host.HostWorkspaceServices;
Expand Down
8 changes: 4 additions & 4 deletions src/CodeStyle/Core/CodeFixes/FormattingCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal abstract class AbstractFormattingCodeFixProvider : CodeFixProvider
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(IDEDiagnosticIds.FormattingDiagnosticId);

protected abstract ISyntaxFormattingService SyntaxFormattingService { get; }
protected abstract ISyntaxFormatting SyntaxFormatting { get; }

public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
Expand All @@ -43,15 +43,15 @@ private async Task<Document> FixOneAsync(CodeFixContext context, Diagnostic diag
{
var options = await GetOptionsAsync(context.Document, cancellationToken).ConfigureAwait(false);
var tree = await context.Document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var updatedTree = await FormattingCodeFixHelper.FixOneAsync(tree, SyntaxFormattingService, options, diagnostic, cancellationToken).ConfigureAwait(false);
var updatedTree = await FormattingCodeFixHelper.FixOneAsync(tree, this.SyntaxFormatting, options, diagnostic, cancellationToken).ConfigureAwait(false);
return context.Document.WithText(await updatedTree.GetTextAsync(cancellationToken).ConfigureAwait(false));
}

private async Task<SyntaxFormattingOptions> GetOptionsAsync(Document document, CancellationToken cancellationToken)
{
var tree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var analyzerConfigOptions = document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(tree);
return SyntaxFormattingService.GetFormattingOptions(analyzerConfigOptions);
return this.SyntaxFormatting.GetFormattingOptions(analyzerConfigOptions);
}

public sealed override FixAllProvider GetFixAllProvider()
Expand All @@ -60,7 +60,7 @@ public sealed override FixAllProvider GetFixAllProvider()
var cancellationToken = context.CancellationToken;
var options = await GetOptionsAsync(document, cancellationToken).ConfigureAwait(false);
var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var updatedSyntaxRoot = Formatter.Format(syntaxRoot, this.SyntaxFormattingService, options, cancellationToken);
var updatedSyntaxRoot = Formatter.Format(syntaxRoot, this.SyntaxFormatting, options, cancellationToken);
return document.WithSyntaxRoot(updatedSyntaxRoot);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Namespace Microsoft.CodeAnalysis.CodeStyle
Friend Class VisualBasicFormattingAnalyzer
Inherits AbstractFormattingAnalyzer

Protected Overrides ReadOnly Property SyntaxFormattingService As ISyntaxFormattingService
Protected Overrides ReadOnly Property SyntaxFormatting As ISyntaxFormatting
Get
Return VisualBasicSyntaxFormattingService.Instance
Return VisualBasicSyntaxFormatting.Instance
End Get
End Property
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Namespace Microsoft.CodeAnalysis.CodeStyle
Public Sub New()
End Sub

Protected Overrides ReadOnly Property SyntaxFormattingService As ISyntaxFormattingService
Protected Overrides ReadOnly Property SyntaxFormatting As ISyntaxFormatting
Get
Return VisualBasicSyntaxFormattingService.Instance
Return VisualBasicSyntaxFormatting.Instance
End Get
End Property
End Class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB

var options = await IndentationOptions.FromDocumentAsync(document, CancellationToken.None);
var formatter = new CSharpSmartTokenFormatter(options, rules, root);
var changes = await formatter.FormatTokenAsync(workspace.Services, token, CancellationToken.None);
var changes = await formatter.FormatTokenAsync(token, CancellationToken.None);

ApplyChanges(buffer, changes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,7 @@ private static async Task AutoFormatOnMarkerAsync(string initialMarkup, string e
return;
}

var changes = formatter.FormatRange(workspace.Services, tokenRange.Value.Item1, tokenRange.Value.Item2, CancellationToken.None);
var changes = formatter.FormatRange(tokenRange.Value.Item1, tokenRange.Value.Item2, CancellationToken.None);
var actual = GetFormattedText(buffer, changes);
Assert.Equal(expected, actual);
}
Expand Down
Loading