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 "Make type partial" codefix #65018

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/Analyzers/CSharp/CodeFixes/CSharpCodeFixes.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="$(MSBuildThisFileDirectory)HideBase\HideBaseCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Iterator\CSharpAddYieldCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Iterator\CSharpChangeToIEnumerableCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeDeclarationPartial\CSharpMakeDeclarationPartialCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeMemberStatic\CSharpMakeMemberStaticCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeStatementAsynchronous\CSharpMakeStatementAsynchronousCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeStructReadOnly\CSharpMakeStructReadOnlyCodeFixProvider.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.MakeDeclarationPartial;

namespace Microsoft.CodeAnalysis.CSharp.MakeDeclarationPartial
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.MakeDeclarationPartial), Shared]
internal sealed class CSharpMakeDeclarationPartialCodeFixProvider : AbstractMakeDeclarationPartialCodeFixProvider
{
private const string CS0260 = nameof(CS0260);

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpMakeDeclarationPartialCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(CS0260);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<Compile Include="$(MSBuildThisFileDirectory)HideBase\HideBaseTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Iterator\AddYieldTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Iterator\ChangeToIEnumerableTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeDeclarationPartial\MakeDeclarationPartialFixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeDeclarationPartial\MakeDeclarationPartialTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeMemberStatic\MakeMemberStaticTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeStatementAsynchronous\CSharpMakeStatementAsynchronousCodeFixTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MakeTypeAbstract\MakeTypeAbstractTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.MakeDeclarationPartial;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.CSharp.UnitTests.MakeDeclarationPartial
{
using VerifyCS = CSharpCodeFixVerifier<
EmptyDiagnosticAnalyzer,
CSharpMakeDeclarationPartialCodeFixProvider>;

[Trait(Traits.Feature, Traits.Features.CodeActionsMakeDeclarationPartial)]
public sealed class MakeDeclarationPartialFixAllTests
{
public static IEnumerable<object[]> AllValidDeclarationTypes()
{
yield return new[] { "class" };
yield return new[] { "struct" };
yield return new[] { "interface" };
yield return new[] { "record" };
yield return new[] { "record class" };
yield return new[] { "record struct" };
}

[Theory]
[MemberData(nameof(AllValidDeclarationTypes))]
public async Task SeveralDeclarationsOfSingleClass(string declarationType)
{
await new VerifyCS.Test
{
TestCode = $$"""
partial {{declarationType}} Declaration
{
}

{{declarationType}} {|CS0260:Declaration|}
{
}

{{declarationType}} {|CS0260:Declaration|}
{
}
""",
FixedCode = $$"""
partial {{declarationType}} Declaration
{
}

partial {{declarationType}} Declaration
{
}

partial {{declarationType}} Declaration
{
}
""",
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
}

[Theory]
[MemberData(nameof(AllValidDeclarationTypes))]
public async Task SingleDeclarationOfSeveralClasses(string declarationType)
{
await new VerifyCS.Test
{
TestCode = $$"""
partial {{declarationType}} Declaration1
{
}

{{declarationType}} {|CS0260:Declaration1|}
{
}

partial {{declarationType}} Declaration2
{
}

{{declarationType}} {|CS0260:Declaration2|}
{
}
""",
FixedCode = $$"""
partial {{declarationType}} Declaration1
{
}

partial {{declarationType}} Declaration1
{
}

partial {{declarationType}} Declaration2
{
}

partial {{declarationType}} Declaration2
{
}
""",
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
}

[Theory]
[MemberData(nameof(AllValidDeclarationTypes))]
public async Task SeveralDeclarationsOfSeveralClasses(string declarationType)
{
await new VerifyCS.Test
{
TestCode = $$"""
partial {{declarationType}} Declaration1
{
}

{{declarationType}} {|CS0260:Declaration1|}
{
}

{{declarationType}} {|CS0260:Declaration1|}
{
}

partial {{declarationType}} Declaration2
{
}

{{declarationType}} {|CS0260:Declaration2|}
{
}

{{declarationType}} {|CS0260:Declaration2|}
{
}
""",
FixedCode = $$"""
partial {{declarationType}} Declaration1
{
}

partial {{declarationType}} Declaration1
{
}

partial {{declarationType}} Declaration1
{
}

partial {{declarationType}} Declaration2
{
}

partial {{declarationType}} Declaration2
{
}

partial {{declarationType}} Declaration2
{
}
""",
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
}
}
}
Loading