Skip to content

Commit

Permalink
Fix formatting when making an accessor readonly (#76054)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Nov 25, 2024
2 parents 40881ef + 5278256 commit 151e2d7
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class {|FixAllInContainingType:Program|}

var expected = """
Console.WriteLine("Hello, World!");

internal class Program
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public event Action e5 { add { } remove { } }
event Action I.e6 { add { } remove { } }

static C() { }

private C() { }
public C(int i) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static async Task<int> fibonacci(int n)
}
}
""",
parseOptions: CSharp8ParseOptions);
parseOptions: CSharp8ParseOptions);
}

[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
Expand All @@ -213,36 +213,37 @@ static async Task<int> fibonacci(int n)
public async Task TestLeadingTriviaAfterSemicolon(string leadingTrivia)
{
await TestInRegularAndScriptAsync(
$@"using System;
class C
{{
void M()
{{
int x;{leadingTrivia}
int [||]fibonacci(int n)
{{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}}
}}
}}",
"""
using System;
$$"""
using System;

class C
{
void M()
{
int x;
class C
{
void M()
{
int x;{{leadingTrivia}}
int [||]fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
$$"""
using System;

static int fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
parseOptions: CSharp8ParseOptions);
class C
{
void M()
{
int x;{{leadingTrivia}}
static int fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
parseOptions: CSharp8ParseOptions);
}

[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
Expand All @@ -252,33 +253,35 @@ static int fibonacci(int n)
public async Task TestLeadingTriviaAfterOpenBrace(string leadingTrivia)
{
await TestInRegularAndScriptAsync(
$@"using System;
$$"""
using System;

class C
{{
void M()
{{{leadingTrivia}
int [||]fibonacci(int n)
{{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}}
}}
}}",
"""
using System;
class C
{
void M()
{{{leadingTrivia}}
int [||]fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
$$"""
using System;

class C
{
void M()
{
static int fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
parseOptions: CSharp8ParseOptions);
class C
{
void M()
{{{leadingTrivia}}
static int fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
""",
parseOptions: CSharp8ParseOptions);
}

[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsMakeLocalFunctionStatic)]
Expand All @@ -304,7 +307,7 @@ bool otherFunction()
}}
}}
}}",
"""
$$"""
using System;

class C
Expand All @@ -314,8 +317,7 @@ void M()
bool otherFunction()
{
return true;
}

}{{leadingTrivia}}
static int fibonacci(int n)
{
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
Expand Down Expand Up @@ -343,15 +345,14 @@ void M()
int [||]fibonacci(int n) => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}}
}}",
"""
$$"""
using System;

class C
{
void M()
{
bool otherFunction() => true;

bool otherFunction() => true;{{leadingTrivia}}
static int fibonacci(int n) => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ namespace NS
public static class Foo
{
static int i;

static void M() { }

static object P { get; set; }

static event System.Action E;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2521,4 +2521,38 @@ internal struct Values
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71500")]
public async Task TestMultipleAccessors()
{
await new VerifyCS.Test
{
TestCode = """
struct S
{
public int M;

public int Z
{
[|get|] => M;
set => M = value;
}
}
""",
FixedCode = """
struct S
{
public int M;

public int Z
{
readonly get => M;
set => M = value;
}
}
""",
LanguageVersion = LanguageVersion.CSharp12,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}
}
3 changes: 0 additions & 3 deletions src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ static void Main(string[] args)

var expected = """
using System;

internal class Program
{
private static void Main(string[] args)
Expand Down Expand Up @@ -84,7 +83,6 @@ static void Main(string[] args)
var expected = """
using System;
using System.Collections.Generic;

internal class Program
{
private static void Main(string[] args)
Expand Down Expand Up @@ -122,7 +120,6 @@ static Task Main(string[] args)
global using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

internal class Program
{
private static Task Main(string[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
namespace Microsoft.CodeAnalysis.CSharp.AddDebuggerDisplay;

[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.AddDebuggerDisplay), Shared]
internal sealed class CSharpAddDebuggerDisplayCodeRefactoringProvider
: AbstractAddDebuggerDisplayCodeRefactoringProvider<
TypeDeclarationSyntax,
MethodDeclarationSyntax>
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpAddDebuggerDisplayCodeRefactoringProvider()
: AbstractAddDebuggerDisplayCodeRefactoringProvider<TypeDeclarationSyntax, MethodDeclarationSyntax>
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpAddDebuggerDisplayCodeRefactoringProvider()
{
}

protected override bool CanNameofAccessNonPublicMembersFromAttributeArgument => true;

protected override bool SupportsConstantInterpolatedStrings(Document document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static async Task<ClassDeclarationSyntax> GenerateProgramClassAsync(
var programType = mainMethod.ContainingType;

// Respect user settings on if they want explicit or implicit accessibility modifiers.
var useDeclaredAccessibity = accessibilityModifiersRequired is AccessibilityModifiersRequired.ForNonInterfaceMembers or AccessibilityModifiersRequired.Always;
var useDeclaredAccessibility = accessibilityModifiersRequired is AccessibilityModifiersRequired.ForNonInterfaceMembers or AccessibilityModifiersRequired.Always;

var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();
Expand All @@ -89,7 +89,7 @@ private static async Task<ClassDeclarationSyntax> GenerateProgramClassAsync(
GenerateProgramMainStatements(root, out var leadingTrivia));
method = method.WithReturnType(method.ReturnType.WithAdditionalAnnotations(Simplifier.AddImportsAnnotation));
method = (MethodDeclarationSyntax)generator.WithAccessibility(
method, useDeclaredAccessibity ? mainMethod.DeclaredAccessibility : Accessibility.NotApplicable);
method, useDeclaredAccessibility ? mainMethod.DeclaredAccessibility : Accessibility.NotApplicable);

// Workaround for simplification not being ready when we generate a new file. Substitute System.String[]
// with string[].
Expand All @@ -101,9 +101,9 @@ private static async Task<ClassDeclarationSyntax> GenerateProgramClassAsync(
// If we dodn't have any suitable class declaration in the same file then generate it
return FixupComments((ClassDeclarationSyntax)generator.ClassDeclaration(
WellKnownMemberNames.TopLevelStatementsEntryPointTypeName,
accessibility: useDeclaredAccessibity ? programType.DeclaredAccessibility : Accessibility.NotApplicable,
accessibility: useDeclaredAccessibility ? programType.DeclaredAccessibility : Accessibility.NotApplicable,
modifiers: hasExistingPart ? DeclarationModifiers.Partial : DeclarationModifiers.None,
members: new[] { method }).WithLeadingTrivia(leadingTrivia));
members: [method]).WithLeadingTrivia(leadingTrivia));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertProgram;
using static ConvertProgramTransform;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ConvertToProgramMain), Shared]
internal class ConvertToProgramMainCodeFixProvider : SyntaxEditorBasedCodeFixProvider
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class ConvertToProgramMainCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ConvertToProgramMainCodeFixProvider()
{
}

public override ImmutableArray<string> FixableDiagnosticIds
=> [IDEDiagnosticIds.UseProgramMainId];

Expand Down
Loading

0 comments on commit 151e2d7

Please sign in to comment.