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

Group code actions 'Change accessibility to' #1305

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Improve refactoring 'Remove comment' [RR0098](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0098) ([PR](https://github.com/dotnet/roslynator/pull/1284))
- Improve refactoring "Remove comment" [RR0098](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0098) ([PR](https://github.com/dotnet/roslynator/pull/1284))
- Remove trailing dot from analyzer's title ([PR](https://github.com/dotnet/roslynator/pull/1298))
- Group code fix "Change accessibility to ..." ([PR](https://github.com/dotnet/roslynator/pull/1305))

### Fixed

Expand All @@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1176](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1176) ([PR](https://github.com/dotnet/roslynator/pull/1291))
- Fix analyzer [RCS1197](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1197) ([PR](https://github.com/dotnet/roslynator/pull/1166))
- Fix analyzer [RCS1093](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1093) ([PR](https://github.com/dotnet/roslynator/pull/1296))
- [Testing] Detect nested code actions ([PR](https://github.com/dotnet/roslynator/pull/1305))

## [4.6.4] - 2023-11-24

Expand Down
11 changes: 10 additions & 1 deletion src/CSharp/CSharp/SyntaxAccessibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static Accessibility GetDefaultAccessibility(SyntaxNode declaration)
return SyntaxAccessibility<EnumMemberDeclarationSyntax>.Instance.GetDefaultAccessibility((EnumMemberDeclarationSyntax)declaration);
case SyntaxKind.NamespaceDeclaration:
return SyntaxAccessibility<NamespaceDeclarationSyntax>.Instance.GetDefaultAccessibility((NamespaceDeclarationSyntax)declaration);
case SyntaxKind.FileScopedNamespaceDeclaration:
return SyntaxAccessibility<FileScopedNamespaceDeclarationSyntax>.Instance.GetDefaultAccessibility((FileScopedNamespaceDeclarationSyntax)declaration);
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
Expand Down Expand Up @@ -125,6 +127,8 @@ public static Accessibility GetDefaultExplicitAccessibility(SyntaxNode declarati
return SyntaxAccessibility<EnumMemberDeclarationSyntax>.Instance.GetDefaultExplicitAccessibility((EnumMemberDeclarationSyntax)declaration);
case SyntaxKind.NamespaceDeclaration:
return SyntaxAccessibility<NamespaceDeclarationSyntax>.Instance.GetDefaultExplicitAccessibility((NamespaceDeclarationSyntax)declaration);
case SyntaxKind.FileScopedNamespaceDeclaration:
return SyntaxAccessibility<FileScopedNamespaceDeclarationSyntax>.Instance.GetDefaultExplicitAccessibility((FileScopedNamespaceDeclarationSyntax)declaration);
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
Expand Down Expand Up @@ -188,6 +192,8 @@ public static Accessibility GetAccessibility(SyntaxNode declaration)
return SyntaxAccessibility<EnumMemberDeclarationSyntax>.Instance.GetAccessibility((EnumMemberDeclarationSyntax)declaration);
case SyntaxKind.NamespaceDeclaration:
return SyntaxAccessibility<NamespaceDeclarationSyntax>.Instance.GetAccessibility((NamespaceDeclarationSyntax)declaration);
case SyntaxKind.FileScopedNamespaceDeclaration:
return SyntaxAccessibility<FileScopedNamespaceDeclarationSyntax>.Instance.GetAccessibility((FileScopedNamespaceDeclarationSyntax)declaration);
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
Expand Down Expand Up @@ -251,6 +257,8 @@ public static Accessibility GetExplicitAccessibility(SyntaxNode declaration)
return SyntaxAccessibility<EnumMemberDeclarationSyntax>.Instance.GetExplicitAccessibility((EnumMemberDeclarationSyntax)declaration);
case SyntaxKind.NamespaceDeclaration:
return SyntaxAccessibility<NamespaceDeclarationSyntax>.Instance.GetExplicitAccessibility((NamespaceDeclarationSyntax)declaration);
case SyntaxKind.FileScopedNamespaceDeclaration:
return SyntaxAccessibility<FileScopedNamespaceDeclarationSyntax>.Instance.GetExplicitAccessibility((FileScopedNamespaceDeclarationSyntax)declaration);
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
Expand Down Expand Up @@ -334,7 +342,7 @@ public static bool IsPubliclyVisible(MemberDeclarationSyntax declaration)

do
{
if (declaration.IsKind(SyntaxKind.NamespaceDeclaration))
if (declaration.IsKind(SyntaxKind.NamespaceDeclaration, SyntaxKind.FileScopedNamespaceDeclaration))
return true;

if (!GetAccessibility(declaration).Is(
Expand Down Expand Up @@ -411,6 +419,7 @@ public static bool IsValidAccessibility(SyntaxNode node, Accessibility accessibi
switch (node.Parent?.Kind())
{
case SyntaxKind.NamespaceDeclaration:
case SyntaxKind.FileScopedNamespaceDeclaration:
case SyntaxKind.CompilationUnit:
{
return accessibility.Is(Accessibility.Public, Accessibility.Internal);
Expand Down
35 changes: 35 additions & 0 deletions src/CSharp/CSharp/SyntaxAccessibility`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ private static object GetInstance()
if (typeof(TNode) == typeof(NamespaceDeclarationSyntax))
return new NamespaceAccessibility();

if (typeof(TNode) == typeof(FileScopedNamespaceDeclarationSyntax))
return new FileScopedNamespaceAccessibility();

if (typeof(TNode) == typeof(OperatorDeclarationSyntax))
return new OperatorAccessibility();

Expand Down Expand Up @@ -772,6 +775,38 @@ public override Accessibility GetExplicitAccessibility(NamespaceDeclarationSynta
}
}

private class FileScopedNamespaceAccessibility : SyntaxAccessibility<FileScopedNamespaceDeclarationSyntax>
{
public override Accessibility GetDefaultAccessibility(FileScopedNamespaceDeclarationSyntax declaration)
{
if (declaration is null)
throw new ArgumentNullException(nameof(declaration));

return Accessibility.Public;
}

public override Accessibility GetDefaultExplicitAccessibility(FileScopedNamespaceDeclarationSyntax declaration)
{
if (declaration is null)
throw new ArgumentNullException(nameof(declaration));

return Accessibility.NotApplicable;
}

public override Accessibility GetAccessibility(FileScopedNamespaceDeclarationSyntax declaration)
{
if (declaration is null)
throw new ArgumentNullException(nameof(declaration));

return Accessibility.Public;
}

public override Accessibility GetExplicitAccessibility(FileScopedNamespaceDeclarationSyntax declaration)
{
return Accessibility.NotApplicable;
}
}

private class OperatorAccessibility : SyntaxAccessibility<OperatorDeclarationSyntax>
{
public override Accessibility GetDefaultAccessibility(OperatorDeclarationSyntax declaration)
Expand Down
31 changes: 22 additions & 9 deletions src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CSharp.Syntax;
Expand Down Expand Up @@ -40,23 +41,31 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
syntaxReferences,
f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken));

ImmutableArray<CodeAction>.Builder typeDeclarationActions = ImmutableArray.CreateBuilder<CodeAction>();

foreach (Accessibility accessibility in AvailableAccessibilities)
{
if (accessibility != modifiersInfo.ExplicitAccessibility
&& SyntaxAccessibility.IsValidAccessibility(node, accessibility))
{
context.RegisterRefactoring(
GetTitle(accessibility),
typeDeclarationActions.Add(CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
ct => RefactorAsync(context.Solution, memberDeclarations, accessibility, ct),
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString());
accessibility.ToString()));
}
}

context.RegisterRefactoring(
"Change accessibility to",
typeDeclarationActions.ToImmutable());

return;
}
}

ImmutableArray<CodeAction>.Builder codeActions = ImmutableArray.CreateBuilder<CodeAction>();

foreach (Accessibility accessibility in AvailableAccessibilities)
{
if (accessibility == modifiersInfo.ExplicitAccessibility)
Expand All @@ -70,23 +79,27 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
{
if (SyntaxAccessibility.IsValidAccessibility(node, accessibility, ignoreOverride: true))
{
context.RegisterRefactoring(
GetTitle(accessibility),
codeActions.Add(CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
ct => RefactorAsync(context.Solution, symbol, accessibility, ct),
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString());
accessibility.ToString()));
}
}
else if (SyntaxAccessibility.IsValidAccessibility(node, accessibility))
{
context.RegisterRefactoring(
GetTitle(accessibility),
codeActions.Add(CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
ct => RefactorAsync(context.Document, node, accessibility, ct),
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString());
accessibility.ToString()));
}
}

context.RegisterRefactoring(
"Change accessibility to",
codeActions.ToImmutable());

ISymbol GetBaseSymbolOrDefault(SemanticModel semanticModel, CancellationToken cancellationToken)
{
if (modifiersInfo.GetFilter().HasAnyFlag(ModifierFilter.AbstractVirtualOverride))
Expand Down
11 changes: 11 additions & 0 deletions src/Refactorings/CSharp/Refactorings/RefactoringContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Collections;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -130,6 +132,15 @@ public Task<SemanticModel> GetSemanticModelAsync()
return Document.GetSemanticModelAsync(CancellationToken);
}

public void RegisterRefactoring(
string title,
ImmutableArray<CodeAction> nestedActions,
bool isInlinable = false)
{
if (nestedActions.Any())
RegisterRefactoring(CodeAction.Create(title, nestedActions, isInlinable));
}

public void RegisterRefactoring(
string title,
Func<CancellationToken, Task<Document>> createChangedDocument,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CSharp.Analysis;
Expand All @@ -12,14 +12,6 @@ namespace Roslynator.CSharp.Refactorings;

internal static class SelectedMemberDeclarationsRefactoring
{
public static ImmutableDictionary<Accessibility, string> _accessibilityIdentifierMap = ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<Accessibility, string>(Accessibility.Public, EquivalenceKey.Create(RefactoringDescriptors.ChangeAccessibility.Id, nameof(Accessibility.Public))),
new KeyValuePair<Accessibility, string>(Accessibility.Internal, EquivalenceKey.Create(RefactoringDescriptors.ChangeAccessibility.Id, nameof(Accessibility.Internal))),
new KeyValuePair<Accessibility, string>(Accessibility.Protected, EquivalenceKey.Create(RefactoringDescriptors.ChangeAccessibility.Id, nameof(Accessibility.Protected))),
new KeyValuePair<Accessibility, string>(Accessibility.Private, EquivalenceKey.Create(RefactoringDescriptors.ChangeAccessibility.Id, nameof(Accessibility.Private))),
});

public static async Task ComputeRefactoringAsync(RefactoringContext context, MemberDeclarationListSelection selectedMembers)
{
if (context.IsRefactoringEnabled(RefactoringDescriptors.ChangeAccessibility)
Expand All @@ -33,10 +25,16 @@ public static async Task ComputeRefactoringAsync(RefactoringContext context, Mem
{
bool canHaveMultipleDeclarations = CanHaveMultipleDeclarations();

TryRegisterRefactoring(validAccessibilities, Accessibility.Public, canHaveMultipleDeclarations);
TryRegisterRefactoring(validAccessibilities, Accessibility.Internal, canHaveMultipleDeclarations);
TryRegisterRefactoring(validAccessibilities, Accessibility.Protected, canHaveMultipleDeclarations);
TryRegisterRefactoring(validAccessibilities, Accessibility.Private, canHaveMultipleDeclarations);
ImmutableArray<CodeAction>.Builder codeActions = ImmutableArray.CreateBuilder<CodeAction>();

TryAddCodeAction(validAccessibilities, Accessibility.Public, canHaveMultipleDeclarations, codeActions);
TryAddCodeAction(validAccessibilities, Accessibility.Internal, canHaveMultipleDeclarations, codeActions);
TryAddCodeAction(validAccessibilities, Accessibility.Protected, canHaveMultipleDeclarations, codeActions);
TryAddCodeAction(validAccessibilities, Accessibility.Private, canHaveMultipleDeclarations, codeActions);

context.RegisterRefactoring(
"Change accessibility to",
codeActions.ToImmutable());
}
}

Expand All @@ -58,30 +56,41 @@ public static async Task ComputeRefactoringAsync(RefactoringContext context, Mem
AddEmptyLineBetweenDeclarationsRefactoring.ComputeRefactoring(context, selectedMembers);
}

void TryRegisterRefactoring(AccessibilityFilter accessibilities, Accessibility accessibility, bool canHaveMultipleDeclarations)
void TryAddCodeAction(AccessibilityFilter accessibilities, Accessibility accessibility, bool canHaveMultipleDeclarations, ImmutableArray<CodeAction>.Builder codeActions)
{
CodeAction codeAction = TryCreateCodeAction(accessibilities, accessibility, canHaveMultipleDeclarations);

if (codeAction is not null)
codeActions.Add(codeAction);
}

CodeAction TryCreateCodeAction(AccessibilityFilter accessibilities, Accessibility accessibility, bool canHaveMultipleDeclarations)
{
if ((accessibilities & accessibility.GetAccessibilityFilter()) != 0)
{
if (canHaveMultipleDeclarations)
{
context.RegisterRefactoring(
ChangeAccessibilityRefactoring.GetTitle(accessibility),
return CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
async ct =>
{
SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(ct).ConfigureAwait(false);
return await ChangeAccessibilityRefactoring.RefactorAsync(context.Document.Solution(), selectedMembers, accessibility, semanticModel, ct).ConfigureAwait(false);
},
_accessibilityIdentifierMap[accessibility]);
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString());
}
else
{
context.RegisterRefactoring(
ChangeAccessibilityRefactoring.GetTitle(accessibility),
return CodeActionFactory.Create(
SyntaxFacts.GetText(accessibility),
ct => ChangeAccessibilityRefactoring.RefactorAsync(context.Document, selectedMembers, accessibility, ct),
RefactoringDescriptors.ChangeAccessibility,
accessibility.ToString());
}
}

return null;
}

bool CanHaveMultipleDeclarations()
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Testing.Common/Extensions/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

using System;
using System.Collections.Immutable;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
Expand All @@ -13,6 +15,18 @@ namespace Roslynator;

internal static class TestExtensions
{
public static ImmutableArray<CodeAction> GetNestedActions(this CodeAction codeAction)
{
Type type = codeAction.GetType();
if (type.Name == "CodeActionWithNestedActions")
{
PropertyInfo propertyInfo = type.GetProperty("NestedCodeActions", BindingFlags.NonPublic | BindingFlags.Instance);
return (ImmutableArray<CodeAction>)propertyInfo.GetValue(codeAction);
}

return ImmutableArray<CodeAction>.Empty;
}

public static async Task<SyntaxNode?> GetSyntaxRootAsync(
this Document document,
bool simplify,
Expand Down
Loading