Skip to content

Commit

Permalink
Add Kind property to CodeRefactoringProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Jan 14, 2025
1 parent 4d5167b commit 59a55dc
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod;
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class ExtractMethodCodeRefactoringProvider() : CodeRefactoringProvider
{
internal override CodeRefactoringKind Kind => CodeRefactoringKind.Extract;

public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
// Don't bother if there isn't a selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal abstract class AbstractExtractClassRefactoringProvider(IExtractClassOpt
protected abstract Task<ImmutableArray<SyntaxNode>> GetSelectedNodesAsync(CodeRefactoringContext context);
protected abstract Task<SyntaxNode?> GetSelectedClassDeclarationAsync(CodeRefactoringContext context);

internal override CodeRefactoringKind Kind => CodeRefactoringKind.Extract;

public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var solution = context.Document.Project.Solution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public ExtractInterfaceCodeRefactoringProvider()
{
}

internal override CodeRefactoringKind Kind => CodeRefactoringKind.Extract;

public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var (document, textSpan, cancellationToken) = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ protected AbstractInlineMethodRefactoringProvider(
_semanticFactsService = semanticFactsService;
}

internal override CodeRefactoringKind Kind => CodeRefactoringKind.Inline;

public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var (document, _, cancellationToken) = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal abstract class AbstractInlineTemporaryCodeRefactoringProvider<
where TIdentifierNameSyntax : SyntaxNode
where TVariableDeclaratorSyntax : SyntaxNode
{
internal override CodeRefactoringKind Kind => CodeRefactoringKind.Inline;

protected static async Task<ImmutableArray<TIdentifierNameSyntax>> GetReferenceLocationsAsync(
Document document,
TVariableDeclaratorSyntax variableDeclarator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod;
using Microsoft.CodeAnalysis.ExtractClass;
using Microsoft.CodeAnalysis.ExtractInterface;
using Microsoft.CodeAnalysis.InlineMethod;
using Microsoft.CodeAnalysis.InlineTemporary;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.UnifiedSuggestions;
Expand Down Expand Up @@ -427,31 +424,12 @@ private static CodeActionKind GetRefactoringKind(IUnifiedSuggestedAction suggest
if (suggestedAction is not ICodeRefactoringSuggestedAction refactoringAction)
return CodeActionKind.Refactor;

if (refactoringAction.CodeRefactoringProvider is ExtractInterfaceCodeRefactoringProvider
or AbstractExtractClassRefactoringProvider
or ExtractMethodCodeRefactoringProvider)
return CodeActionKind.RefactorExtract;

if (IsInstanceOfGenericType(typeof(AbstractInlineMethodRefactoringProvider<,,,>), refactoringAction.CodeRefactoringProvider)
|| IsInstanceOfGenericType(typeof(AbstractInlineTemporaryCodeRefactoringProvider<,>), refactoringAction.CodeRefactoringProvider))
return CodeActionKind.RefactorInline;

return CodeActionKind.Refactor;

static bool IsInstanceOfGenericType(Type genericType, object instance)
return refactoringAction.CodeRefactoringProvider.Kind switch
{
Type? type = instance.GetType();
while (type != null)
{
if (type.IsGenericType &&
type.GetGenericTypeDefinition() == genericType)
{
return true;
}
type = type.BaseType;
}
return false;
}
CodeRefactoringKind.Extract => CodeActionKind.RefactorExtract,
CodeRefactoringKind.Inline => CodeActionKind.RefactorInline,
_ => CodeActionKind.Refactor,
};
}

private static LSP.VSInternalPriorityLevel? UnifiedSuggestedActionSetPriorityToPriorityLevel(CodeActionPriority priority)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

namespace Microsoft.CodeAnalysis.CodeRefactorings;

/// <summary>
/// Most refactorings will have the <see cref="CodeRefactoringKind.Refactoring"/> kind. This allows us to draw
/// attention to Extract and Inline refactorings.
/// </summary>
/// <remarks>
/// When new values are added here we should account for them in the `CodeActionHelpers` class.
/// </remarks>
internal enum CodeRefactoringKind
{
Refactoring = 0,
Extract = 1,
Inline = 2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public abstract class CodeRefactoringProvider
/// registered by this code refactoring provider across the supported <see cref="CodeFixes.FixAllScope"/>s.
/// Return null if the provider doesn't support fix all operation.
/// </summary>
/// <remarks>
/// TODO: Make public, tracked with https://github.com/dotnet/roslyn/issues/60703
/// </remarks>
internal virtual FixAllProvider? GetFixAllProvider()
=> null;

/// <summary>
/// Gets the <see cref="CodeRefactoringKind"/> indicating what kind of refactoring it is.
/// </summary>
internal virtual CodeRefactoringKind Kind => CodeRefactoringKind.Refactoring;

/// <summary>
/// Computes the <see cref="CodeActionRequestPriority"/> group this provider should be considered to run at. Legal values
/// this can be must be between <see cref="CodeActionRequestPriority.Low"/> and <see cref="CodeActionPriority.High"/>.
Expand Down

0 comments on commit 59a55dc

Please sign in to comment.