From 1151bfe3cba04bd460824ed5b238c6e85e10b01a Mon Sep 17 00:00:00 2001 From: akhera99 Date: Tue, 20 Feb 2024 13:57:14 -0800 Subject: [PATCH 1/3] dont show option for trampoline if refs contain object creation --- ...troduceParameterCodeRefactoringProvider.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs index 461fbe1605a56..3395eb2c4da53 100644 --- a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs +++ b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs @@ -148,6 +148,7 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService using var actionsBuilder = TemporaryArray.Empty; using var actionsBuilderAllOccurrences = TemporaryArray.Empty; var syntaxFacts = document.GetRequiredLanguageService(); + var methodCallSites = await FindCallSitesAsync(document, methodSymbol, cancellationToken).ConfigureAwait(false); if (!containsClassExpression) { @@ -157,10 +158,14 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService if (methodSymbol.MethodKind is not MethodKind.Constructor) { - actionsBuilder.Add(CreateNewCodeAction( - FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: false, IntroduceParameterCodeActionKind.Trampoline)); - actionsBuilderAllOccurrences.Add(CreateNewCodeAction( - FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: true, IntroduceParameterCodeActionKind.Trampoline)); + var containsObjectCreationReferences = methodCallSites.Values.SelectMany(invocations => invocations).Any(invocation => syntaxFacts.IsObjectCreationExpression(invocation)); + if (!containsObjectCreationReferences) + { + actionsBuilder.Add(CreateNewCodeAction( + FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: false, IntroduceParameterCodeActionKind.Trampoline)); + actionsBuilderAllOccurrences.Add(CreateNewCodeAction( + FeaturesResources.into_extracted_method_to_invoke_at_call_sites, allOccurrences: true, IntroduceParameterCodeActionKind.Trampoline)); + } if (methodSymbol.MethodKind is not MethodKind.LocalFunction) { @@ -178,7 +183,7 @@ CodeAction CreateNewCodeAction(string actionName, bool allOccurrences, Introduce { return CodeAction.Create( actionName, - cancellationToken => IntroduceParameterAsync(document, expression, methodSymbol, containingMethod, allOccurrences, selectedCodeAction, fallbackOptions, cancellationToken), + cancellationToken => IntroduceParameterAsync(document, expression, methodSymbol, containingMethod, methodCallSites, allOccurrences, selectedCodeAction, fallbackOptions, cancellationToken), actionName); } } @@ -229,11 +234,9 @@ CodeAction CreateNewCodeAction(string actionName, bool allOccurrences, Introduce /// Introduces a new parameter and refactors all the call sites based on the selected code action. /// private async Task IntroduceParameterAsync(Document originalDocument, TExpressionSyntax expression, - IMethodSymbol methodSymbol, SyntaxNode containingMethod, bool allOccurrences, IntroduceParameterCodeActionKind selectedCodeAction, + IMethodSymbol methodSymbol, SyntaxNode containingMethod, Dictionary> methodCallSites, bool allOccurrences, IntroduceParameterCodeActionKind selectedCodeAction, CodeGenerationOptionsProvider fallbackOptions, CancellationToken cancellationToken) { - var methodCallSites = await FindCallSitesAsync(originalDocument, methodSymbol, cancellationToken).ConfigureAwait(false); - var modifiedSolution = originalDocument.Project.Solution; var rewriter = new IntroduceParameterDocumentRewriter(this, originalDocument, expression, methodSymbol, containingMethod, selectedCodeAction, fallbackOptions, allOccurrences); From 13a22ffebc91e662608ffef9c823bef7bcb0b81c Mon Sep 17 00:00:00 2001 From: akhera99 Date: Wed, 21 Feb 2024 10:04:28 -0800 Subject: [PATCH 2/3] feedback --- .../AbstractIntroduceParameterCodeRefactoringProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs index 3395eb2c4da53..2779ad296a27a 100644 --- a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs +++ b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs @@ -158,7 +158,7 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService if (methodSymbol.MethodKind is not MethodKind.Constructor) { - var containsObjectCreationReferences = methodCallSites.Values.SelectMany(invocations => invocations).Any(invocation => syntaxFacts.IsObjectCreationExpression(invocation)); + var containsObjectCreationReferences = methodCallSites.Values.Flatten().Any(invocation => syntaxFacts.IsObjectCreationExpression(invocation)); if (!containsObjectCreationReferences) { actionsBuilder.Add(CreateNewCodeAction( From d0242b0d75bd39a9fbf3d2a2dc745f2c59794f31 Mon Sep 17 00:00:00 2001 From: akhera99 Date: Wed, 21 Feb 2024 14:30:49 -0800 Subject: [PATCH 3/3] feedback --- .../AbstractIntroduceParameterCodeRefactoringProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs index 2779ad296a27a..7ccd98283bdb5 100644 --- a/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs +++ b/src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs @@ -158,7 +158,7 @@ private static bool IsValidExpression(SyntaxNode expression, ISyntaxFactsService if (methodSymbol.MethodKind is not MethodKind.Constructor) { - var containsObjectCreationReferences = methodCallSites.Values.Flatten().Any(invocation => syntaxFacts.IsObjectCreationExpression(invocation)); + var containsObjectCreationReferences = methodCallSites.Values.Flatten().OfType().Any(); if (!containsObjectCreationReferences) { actionsBuilder.Add(CreateNewCodeAction(