diff --git a/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs b/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs index b216a35ff164c..c995776069858 100644 --- a/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs +++ b/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs @@ -102,6 +102,32 @@ record Class } """, new TestParameters(TestOptions.RegularPreview)); + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76813")] + public Task TestSingleGetterFromField_CommentBeforeField() + => TestInRegularAndScript1Async( + """ + class Class + { + // Comment to preserve + [|int i|]; + + int P + { + get + { + return i; + } + } + } + """, + """ + class Class + { + // Comment to preserve + int P { get; } + } + """); + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/28511")] public Task TestNullable1() => TestMissingInRegularAndScriptAsync( diff --git a/src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs b/src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs index 07ee9af1d1592..afd63a14d0831 100644 --- a/src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs +++ b/src/Features/Core/Portable/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs @@ -239,18 +239,18 @@ private async Task ProcessResultWorkerAsync( // it. As long as something is above it, we keep the separation. However, if the // property becomes the first member in the type, the separation is now inappropriate // because there's nothing to actually separate it from. + var fieldDocumentSyntaxFacts = fieldDocument.GetRequiredLanguageService(); if (fieldDocument == propertyDocument) { - var syntaxFacts = fieldDocument.GetRequiredLanguageService(); var bannerService = fieldDocument.GetRequiredLanguageService(); - if (WillRemoveFirstFieldInTypeDirectlyAboveProperty(syntaxFacts, propertyDeclaration, nodeToRemove) && + if (WillRemoveFirstFieldInTypeDirectlyAboveProperty(fieldDocumentSyntaxFacts, propertyDeclaration, nodeToRemove) && bannerService.GetLeadingBlankLines(nodeToRemove).Length == 0) { updatedProperty = bannerService.GetNodeWithoutLeadingBlankLines(updatedProperty); } } - var syntaxRemoveOptions = CreateSyntaxRemoveOptions(nodeToRemove); + var syntaxRemoveOptions = CreateSyntaxRemoveOptions(fieldDocumentSyntaxFacts, nodeToRemove); if (fieldDocument == propertyDocument) { // Same file. Have to do this in a slightly complicated fashion. @@ -326,15 +326,13 @@ private async Task ProcessResultWorkerAsync( return (fieldSymbol, propertySymbol); } - private static SyntaxRemoveOptions CreateSyntaxRemoveOptions(SyntaxNode nodeToRemove) + private static SyntaxRemoveOptions CreateSyntaxRemoveOptions( + ISyntaxFacts syntaxFacts, SyntaxNode nodeToRemove) { var syntaxRemoveOptions = SyntaxGenerator.DefaultRemoveOptions; - var hasDirective = nodeToRemove.GetLeadingTrivia().Any(t => t.IsDirective); - if (hasDirective) - { + if (nodeToRemove.GetLeadingTrivia().Any(t => t.IsDirective || syntaxFacts.IsRegularComment(t))) syntaxRemoveOptions |= SyntaxRemoveOptions.KeepLeadingTrivia; - } return syntaxRemoveOptions; }