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

Ensure that 'extract method' follows the user's 'use this/me' preference #76366

Merged
merged 15 commits into from
Dec 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ private abstract partial class CSharpCodeGenerator
private sealed class ExpressionCodeGenerator(
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
bool localFunction) : CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
ExtractMethodGenerationOptions options,
bool localFunction)
: CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
{
protected override SyntaxToken CreateMethodName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

Expand All @@ -23,8 +23,9 @@ private abstract partial class CSharpCodeGenerator
public sealed class MultipleStatementsCodeGenerator(
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
bool localFunction) : CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
ExtractMethodGenerationOptions options,
bool localFunction)
: CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
{
protected override SyntaxToken CreateMethodName()
=> GenerateMethodNameForStatementGenerators();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod;
Expand All @@ -20,8 +20,9 @@ private abstract partial class CSharpCodeGenerator
public sealed class SingleStatementCodeGenerator(
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
bool localFunction) : CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
ExtractMethodGenerationOptions options,
bool localFunction)
: CSharpCodeGenerator(selectionResult, analyzerResult, options, localFunction)
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
{
protected override SyntaxToken CreateMethodName() => GenerateMethodNameForStatementGenerators();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Task<GeneratedCode> GenerateAsync(
InsertionPoint insertionPoint,
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
ExtractMethodGenerationOptions options,
bool localFunction,
CancellationToken cancellationToken)
{
Expand All @@ -56,7 +56,7 @@ public static Task<GeneratedCode> GenerateAsync(
public static CSharpCodeGenerator Create(
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
ExtractMethodGenerationOptions options,
bool localFunction)
{
if (selectionResult.SelectionInExpression)
Expand All @@ -74,7 +74,7 @@ public static CSharpCodeGenerator Create(
protected CSharpCodeGenerator(
CSharpSelectionResult selectionResult,
AnalyzerResult analyzerResult,
CSharpCodeGenerationOptions options,
ExtractMethodGenerationOptions options,
bool localFunction)
: base(selectionResult, analyzerResult, options, localFunction)
{
Expand Down Expand Up @@ -574,7 +574,13 @@ protected override StatementSyntax CreateReturnStatement(string identifierName =

protected override ExpressionSyntax CreateCallSignature()
{
var methodName = CreateMethodNameForInvocation().WithAdditionalAnnotations(Simplifier.Annotation);
var methodName = CreateMethodNameForInvocation();
ExpressionSyntax methodExpression =
this.AnalyzerResult.UseInstanceMember && this.ExtractMethodGenerationOptions.SimplifierOptions.QualifyMethodAccess.Value && !LocalFunction
? MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), methodName)
: methodName;

methodExpression = methodExpression.WithAdditionalAnnotations(Simplifier.Annotation);
var isLocalFunction = LocalFunction && ShouldLocalFunctionCaptureParameter(SemanticDocument.Root);

using var _ = ArrayBuilder<ArgumentSyntax>.GetInstance(out var arguments);
Expand All @@ -589,7 +595,7 @@ protected override ExpressionSyntax CreateCallSignature()
}
}

var invocation = (ExpressionSyntax)InvocationExpression(methodName, ArgumentList([.. arguments]));
var invocation = (ExpressionSyntax)InvocationExpression(methodExpression, ArgumentList([.. arguments]));
if (this.SelectionResult.ShouldPutAsyncModifier())
{
if (this.SelectionResult.ShouldCallConfigureAwaitFalse())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Formatting.Rules;
Expand All @@ -23,7 +21,7 @@ internal sealed partial class CSharpMethodExtractor(CSharpSelectionResult result
: MethodExtractor<CSharpSelectionResult, StatementSyntax, ExpressionSyntax>(result, options, localFunction)
{
protected override CodeGenerator CreateCodeGenerator(AnalyzerResult analyzerResult)
=> CSharpCodeGenerator.Create(this.OriginalSelectionResult, analyzerResult, (CSharpCodeGenerationOptions)this.Options.CodeGenerationOptions, this.LocalFunction);
=> CSharpCodeGenerator.Create(this.OriginalSelectionResult, analyzerResult, this.Options, this.LocalFunction);

protected override AnalyzerResult Analyze(CSharpSelectionResult selectionResult, bool localFunction, CancellationToken cancellationToken)
=> CSharpAnalyzer.Analyze(selectionResult, localFunction, cancellationToken);
Expand Down Expand Up @@ -155,8 +153,8 @@ private bool OriginalSelectionWithin(SyntaxNode node)
protected override async Task<TriviaResult> PreserveTriviaAsync(CSharpSelectionResult selectionResult, CancellationToken cancellationToken)
=> await CSharpTriviaResult.ProcessAsync(selectionResult, cancellationToken).ConfigureAwait(false);

protected override Task<GeneratedCode> GenerateCodeAsync(InsertionPoint insertionPoint, CSharpSelectionResult selectionResult, AnalyzerResult analyzeResult, CodeGenerationOptions options, CancellationToken cancellationToken)
=> CSharpCodeGenerator.GenerateAsync(insertionPoint, selectionResult, analyzeResult, (CSharpCodeGenerationOptions)options, LocalFunction, cancellationToken);
protected override Task<GeneratedCode> GenerateCodeAsync(InsertionPoint insertionPoint, CSharpSelectionResult selectionResult, AnalyzerResult analyzeResult, ExtractMethodGenerationOptions options, CancellationToken cancellationToken)
=> CSharpCodeGenerator.GenerateAsync(insertionPoint, selectionResult, analyzeResult, options, LocalFunction, cancellationToken);

protected override AbstractFormattingRule GetCustomFormattingRule(Document document)
=> FormattingRule.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using VerifyCS = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.CSharpCodeRefactoringVerifier<
Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod.ExtractMethodCodeRefactoringProvider>;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.ExtractMethod;

using VerifyCS = CSharpCodeRefactoringVerifier<
ExtractMethodCodeRefactoringProvider>;

public sealed class ExtractLocalFunctionTests : AbstractCSharpCodeActionTest_NoEditor
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(TestWorkspace workspace, TestParameters parameters)
Expand Down
Loading
Loading