Skip to content

Commit 257ee96

Browse files
Move 'use auto prop' to code style layer. (#79737)
2 parents f9715b2 + 814dd21 commit 257ee96

File tree

42 files changed

+381
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+381
-265
lines changed

src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<Compile Include="$(MSBuildThisFileDirectory)UnsealClass\CSharpUnsealClassCodeFixProvider.cs" />
9797
<Compile Include="$(MSBuildThisFileDirectory)UpdateProjectToAllowUnsafe\CSharpUpdateProjectToAllowUnsafeCodeFixProvider.cs" />
9898
<Compile Include="$(MSBuildThisFileDirectory)UpgradeProject\CSharpUpgradeProjectCodeFixProvider.cs" />
99+
<Compile Include="$(MSBuildThisFileDirectory)UseAutoProperty\CSharpUseAutoPropertyCodeFixProvider.cs" />
100+
<Compile Include="$(MSBuildThisFileDirectory)UseAutoProperty\SingleLinePropertyFormattingRule.cs" />
101+
<Compile Include="$(MSBuildThisFileDirectory)UseAutoProperty\UseAutoPropertyRewriter.cs" />
99102
<Compile Include="$(MSBuildThisFileDirectory)UseExplicitArrayInExpressionTree\CSharpUseExplicitArrayInExpressionTreeCodeFixProvider.cs" />
100103
<Compile Include="$(MSBuildThisFileDirectory)UseCoalesceExpression\CSharpUseCoalesceExpressionForIfNullStatementCheckCodeFixProvider.cs" />
101104
<Compile Include="$(MSBuildThisFileDirectory)UseCollectionExpression\CSharpCollectionExpressionRewriter.cs" />

src/Analyzers/CSharp/CodeFixes/ImplementInterface/CSharpImplementInterfaceService.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
1212
using Microsoft.CodeAnalysis.CSharp.Extensions;
13-
using Microsoft.CodeAnalysis.CSharp.Formatting;
1413
using Microsoft.CodeAnalysis.CSharp.Syntax;
1514
using Microsoft.CodeAnalysis.Editing;
16-
using Microsoft.CodeAnalysis.Formatting;
1715
using Microsoft.CodeAnalysis.Host.Mef;
1816
using Microsoft.CodeAnalysis.ImplementInterface;
1917
using Microsoft.CodeAnalysis.PooledObjects;
@@ -26,9 +24,6 @@ namespace Microsoft.CodeAnalysis.CSharp.ImplementInterface;
2624
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
2725
internal sealed class CSharpImplementInterfaceService() : AbstractImplementInterfaceService<TypeDeclarationSyntax>
2826
{
29-
protected override ISyntaxFormatting SyntaxFormatting
30-
=> CSharpSyntaxFormatting.Instance;
31-
3227
protected override SyntaxGeneratorInternal SyntaxGeneratorInternal
3328
=> CSharpSyntaxGeneratorInternal.Instance;
3429

src/Features/CSharp/Portable/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs renamed to src/Analyzers/CSharp/CodeFixes/UseAutoProperty/CSharpUseAutoPropertyCodeFixProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading.Tasks;
1212
using Microsoft.CodeAnalysis.CodeFixes;
1313
using Microsoft.CodeAnalysis.CSharp.Extensions;
14+
using Microsoft.CodeAnalysis.CSharp.Formatting;
1415
using Microsoft.CodeAnalysis.CSharp.Syntax;
1516
using Microsoft.CodeAnalysis.Editing;
1617
using Microsoft.CodeAnalysis.FindSymbols;
@@ -38,6 +39,8 @@ internal sealed partial class CSharpUseAutoPropertyCodeFixProvider()
3839
ConstructorDeclarationSyntax,
3940
ExpressionSyntax>
4041
{
42+
protected override ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance;
43+
4144
protected override PropertyDeclarationSyntax GetPropertyDeclaration(SyntaxNode node)
4245
=> (PropertyDeclarationSyntax)node;
4346

@@ -222,7 +225,7 @@ protected override ImmutableArray<AbstractFormattingRule> GetFormattingRules(
222225
if (propertyDeclaration is PropertyDeclarationSyntax { AccessorList.Accessors: var accessors } &&
223226
accessors.All(a => a is { ExpressionBody: null, Body: null, AttributeLists.Count: 0 }))
224227
{
225-
return [new SingleLinePropertyFormattingRule(), .. Formatter.GetDefaultFormattingRules(document)];
228+
return [new SingleLinePropertyFormattingRule(), .. this.SyntaxFormatting.GetDefaultFormattingRules()];
226229
}
227230

228231
return default;

src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpUseCollectionExpressionForFluentCodeFixProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ static async Task<SeparatedSyntaxList<ArgumentSyntax>> GetArgumentsAsync(
146146
return default;
147147

148148
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
149-
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(
150-
CSharpSyntaxFormatting.Instance, cancellationToken).ConfigureAwait(false);
149+
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
151150

152151
using var _ = ArrayBuilder<SyntaxNodeOrToken>.GetInstance(out var nodesAndTokens);
153152

src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed partial class UseAutoPropertyTests(ITestOutputHelper logger)
2525
private readonly ParseOptions CSharp12 = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12);
2626

2727
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
28-
=> (new CSharpUseAutoPropertyAnalyzer(), GetCSharpUseAutoPropertyCodeFixProvider());
28+
=> (new CSharpUseAutoPropertyAnalyzer(), new CSharpUseAutoPropertyCodeFixProvider());
2929

3030
[Fact]
3131
public Task TestSingleGetterFromField()

src/Analyzers/Core/CodeFixes/CodeFixes.projitems

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@
128128
<Compile Include="$(MSBuildThisFileDirectory)SimplifyInterpolation\AbstractSimplifyInterpolationCodeFixProvider.cs" />
129129
<Compile Include="$(MSBuildThisFileDirectory)SimplifyLinqExpression\SimplifyLinqExpressionCodeFixProvider.cs" />
130130
<Compile Include="$(MSBuildThisFileDirectory)UpgradeProject\AbstractUpgradeProjectCodeFixProvider.cs" />
131+
<Compile Include="$(MSBuildThisFileDirectory)UseAutoProperty\AbstractUseAutoPropertyCodeFixProvider.cs" />
132+
<Compile Include="$(MSBuildThisFileDirectory)UseAutoProperty\UseAutoPropertyFixAllProvider.cs" />
131133
<Compile Include="$(MSBuildThisFileDirectory)UseCoalesceExpression\AbstractUseCoalesceExpressionForIfNullStatementCheckCodeFixProvider.cs" />
132134
<Compile Include="$(MSBuildThisFileDirectory)UseCoalesceExpression\UseCoalesceExpressionForTernaryConditionalCheckCodeFixProvider.cs" />
133135
<Compile Include="$(MSBuildThisFileDirectory)UseCoalesceExpression\UseCoalesceExpressionForNullableTernaryConditionalCheckCodeFixProvider.cs" />

src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ private async Task<Document> FixOneAsync(CodeFixContext context, Diagnostic diag
7979
text.Lines[diagnosticLinePositionSpan.Start.Line].Start,
8080
text.Lines[diagnosticLinePositionSpan.End.Line].End);
8181

82-
var formattingOptions = await context.Document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);
82+
var formattingOptions = await context.Document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
8383
var formattedRoot = SyntaxFormatting.GetFormattingResult(root, [spanToFormat], formattingOptions, rules: default, cancellationToken: cancellationToken).GetFormattedRoot(cancellationToken);
8484

8585
return context.Document.WithSyntaxRoot(formattedRoot);
8686
}
8787

8888
protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
8989
{
90-
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false);
90+
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
9191
var updatedRoot = SyntaxFormatting.GetFormattingResult(editor.OriginalRoot, [editor.OriginalRoot.FullSpan], formattingOptions, rules: default, cancellationToken).GetFormattedRoot(cancellationToken);
9292
editor.ReplaceNode(editor.OriginalRoot, updatedRoot);
9393
}

src/Analyzers/Core/CodeFixes/ImplementInterface/AbstractImplementInterfaceService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ internal abstract partial class AbstractImplementInterfaceService<TTypeDeclarati
2828
{
2929
protected const string DisposingName = "disposing";
3030

31-
protected abstract ISyntaxFormatting SyntaxFormatting { get; }
3231
protected abstract SyntaxGeneratorInternal SyntaxGeneratorInternal { get; }
3332

3433
protected abstract string ToDisplayString(IMethodSymbol disposeImplMethod, SymbolDisplayFormat format);

0 commit comments

Comments
 (0)