From 65092b444f116c0ec7af70b817d99c9f3a98a931 Mon Sep 17 00:00:00 2001 From: Brian Freeman Date: Tue, 29 Sep 2020 17:26:36 -0400 Subject: [PATCH] update to include comment on both get and set routine --- .../ReplaceMethodWithPropertyTests.cs | 33 ++++++++++++++++++- ...bstractReplaceMethodWithPropertyService.cs | 13 +++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs index b42e12d805b..ffae890a006 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs @@ -2361,7 +2361,7 @@ int Goo1 } [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)] [WorkItem(42698, "https://github.com/dotnet/roslyn/issues/42698")] - public async Task TestRefactorRetainsComments() + public async Task TestMethodWithTrivia_3() { await TestInRegularAndScript1Async( @"class C @@ -2376,6 +2376,37 @@ public async Task TestRefactorRetainsComments() //Vital Comment int FooBar => 1; }"); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)] + [WorkItem(42698, "https://github.com/dotnet/roslyn/issues/42698")] + public async Task TestMethodWithTrivia_4() + { + await TestWithAllCodeStyleOff( +@"class C +{ + int [||]GetGoo() // Goo + { + } + void SetGoo(int i) // SetGoo + { + } +}", +@"class C +{ + // Goo + // SetGoo + int Goo + { + get + { + } + + set + { + } + } +}", +index: 1); } private async Task TestWithAllCodeStyleOff( string initialMarkup, string expectedMarkup, diff --git a/src/Features/Core/Portable/ReplaceMethodWithProperty/AbstractReplaceMethodWithPropertyService.cs b/src/Features/Core/Portable/ReplaceMethodWithProperty/AbstractReplaceMethodWithPropertyService.cs index 79c5d20de78..9605cef376e 100644 --- a/src/Features/Core/Portable/ReplaceMethodWithProperty/AbstractReplaceMethodWithPropertyService.cs +++ b/src/Features/Core/Portable/ReplaceMethodWithProperty/AbstractReplaceMethodWithPropertyService.cs @@ -49,12 +49,13 @@ private static bool OverridesMetadataSymbol(IMethodSymbol method) var setMethodDeclaration = getAndSetMethods.SetMethodDeclaration; var finalLeadingTrivia = getAndSetMethods.GetMethodDeclaration.GetLeadingTrivia().ToList(); + if (getMethodDeclaration.ChildNodes().Any(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList && n.GetTrailingTrivia().Any(t => !syntaxFacts.IsWhitespaceOrEndOfLineTrivia(t)))) + { + finalLeadingTrivia.AddRange(getMethodDeclaration.ChildNodes().Where(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList).First().GetTrailingTrivia()); + } + if (setMethodDeclaration == null) { - if (getMethodDeclaration.ChildNodes().Any(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList && n.GetTrailingTrivia().Any(t => !syntaxFacts.IsWhitespaceOrEndOfLineTrivia(t)))) - { - finalLeadingTrivia.AddRange(getMethodDeclaration.ChildNodes().Where(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList).First().GetTrailingTrivia()); - } return property.WithLeadingTrivia(finalLeadingTrivia); } @@ -62,6 +63,10 @@ private static bool OverridesMetadataSymbol(IMethodSymbol method) setMethodDeclaration.GetLeadingTrivia() .SkipWhile(t => syntaxFacts.IsEndOfLineTrivia(t)) .Where(t => !t.IsDirective)); + if (setMethodDeclaration.ChildNodes().Any(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList && n.GetTrailingTrivia().Any(t => !syntaxFacts.IsWhitespaceOrEndOfLineTrivia(t)))) + { + finalLeadingTrivia.AddRange(setMethodDeclaration.ChildNodes().Where(n => n.RawKind == syntaxFacts.SyntaxKinds.ParameterList).First().GetTrailingTrivia()); + } return property.WithLeadingTrivia(finalLeadingTrivia); } -- GitLab