From 00b7a64382f85181a41ebf14210d838bb863ef34 Mon Sep 17 00:00:00 2001 From: akhera99 Date: Wed, 19 Jan 2022 17:07:24 -0800 Subject: [PATCH 1/5] fix bug --- .../IntroduceParameterTests.cs | 19 +++++++++++++++++++ .../AbstractIntroduceParameterService.cs | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs index fc3e13daa81cd..f6fc38fd89bd1 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs @@ -1932,5 +1932,24 @@ public Program(int x) "; await TestInRegularAndScriptAsync(code, expected, 0); } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceParameter)] + public async Task TestIntroduceParameterOnParameter() + { + var code = +@" +using System; + +class Program +{ + public static void Main(string[] args) + { + Console.WriteLine([|args|]); + } +} +"; + + await TestMissingInRegularAndScriptAsync(code); + } } } diff --git a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs index 581c51d514e80..8cb91e67eabff 100644 --- a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs +++ b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs @@ -58,6 +58,12 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex return; } + var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; + if (expressionSymbol is IParameterSymbol) + { + return; + } + var syntaxFacts = document.GetRequiredLanguageService(); // Need to special case for expressions that are contained within a parameter From 7aa0d17f45c6a1f1f9d6fdd49881d4e494dfc90a Mon Sep 17 00:00:00 2001 From: akhera99 Date: Thu, 20 Jan 2022 12:58:30 -0800 Subject: [PATCH 2/5] local function case --- .../IntroduceParameterTests.cs | 38 +++++++++++++++++++ .../AbstractIntroduceParameterService.cs | 12 +++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs index f6fc38fd89bd1..72d4b945fd139 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs @@ -1951,5 +1951,43 @@ public static void Main(string[] args) await TestMissingInRegularAndScriptAsync(code); } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceParameter)] + public async Task TestIntroduceParameterOnExpressionContainingParameter() + { + var code = +@" +public class C +{ + public void M(string s) + { + localFunction(); + + void localFunction() + { + _ = [|s|].ToString(); + } + } +} +"; + + var expected = +@" +public class C +{ + public void M(string s) + { + localFunction(s); + + void localFunction(string s) + { + _ = s.ToString(); + } + } +} +"; + + await TestInRegularAndScriptAsync(code, expected, 0); + } } } diff --git a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs index 8cb91e67eabff..88cf1a8c1f601 100644 --- a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs +++ b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs @@ -58,12 +58,6 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex return; } - var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; - if (expressionSymbol is IParameterSymbol) - { - return; - } - var syntaxFacts = document.GetRequiredLanguageService(); // Need to special case for expressions that are contained within a parameter @@ -97,6 +91,12 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex return; } + var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; + if (expressionSymbol is IParameterSymbol && !syntaxFacts.IsLocalFunctionStatement(containingMethod)) + { + return; + } + var containingSymbol = semanticModel.GetDeclaredSymbol(containingMethod, cancellationToken); if (containingSymbol is not IMethodSymbol methodSymbol) { From d9655616474fbae96e97a2674d2263c760dfa6f9 Mon Sep 17 00:00:00 2001 From: akhera99 Date: Thu, 20 Jan 2022 14:20:33 -0800 Subject: [PATCH 3/5] fix --- .../IntroduceParameter/IntroduceParameterTests.cs | 2 +- .../AbstractIntroduceParameterService.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs index 72d4b945fd139..722a2489ab69d 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs @@ -1977,7 +1977,7 @@ public class C { public void M(string s) { - localFunction(s); + localFunction(""test""); void localFunction(string s) { diff --git a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs index 88cf1a8c1f601..f874b000e3696 100644 --- a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs +++ b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceParameterService.cs @@ -91,14 +91,14 @@ public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContex return; } - var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; - if (expressionSymbol is IParameterSymbol && !syntaxFacts.IsLocalFunctionStatement(containingMethod)) + var containingSymbol = semanticModel.GetDeclaredSymbol(containingMethod, cancellationToken); + if (containingSymbol is not IMethodSymbol methodSymbol) { return; } - var containingSymbol = semanticModel.GetDeclaredSymbol(containingMethod, cancellationToken); - if (containingSymbol is not IMethodSymbol methodSymbol) + var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol; + if (expressionSymbol is IParameterSymbol parameterSymbol && parameterSymbol.ContainingSymbol.Equals(containingSymbol)) { return; } From 6e9e5e11c16b422ad681141e26c20e40f0a77706 Mon Sep 17 00:00:00 2001 From: Ankita Khera <40616383+akhera99@users.noreply.github.com> Date: Thu, 20 Jan 2022 15:39:48 -0800 Subject: [PATCH 4/5] Update IntroduceParameterTests.cs --- .../CodeActions/IntroduceParameter/IntroduceParameterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs index 722a2489ab69d..72d4b945fd139 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs @@ -1977,7 +1977,7 @@ public class C { public void M(string s) { - localFunction(""test""); + localFunction(s); void localFunction(string s) { From b8253004ea85c7586ed6d1f747fdbcc23fa1d53f Mon Sep 17 00:00:00 2001 From: Ankita Khera <40616383+akhera99@users.noreply.github.com> Date: Fri, 21 Jan 2022 21:04:53 -0800 Subject: [PATCH 5/5] Update IntroduceParameterTests.cs --- .../CodeActions/IntroduceParameter/IntroduceParameterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs index 72d4b945fd139..05a41da7fe725 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/IntroduceParameter/IntroduceParameterTests.cs @@ -1981,7 +1981,7 @@ public void M(string s) void localFunction(string s) { - _ = s.ToString(); + _ = {|Rename:s|}.ToString(); } } }