From 3069891d2c74dccded7e89592a343de0fa8e5e3d Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Mon, 7 Nov 2016 18:37:57 -0800 Subject: [PATCH] Indent code when converting a property to a method. --- .../ReplaceMethodWithPropertyTests.cs | 34 +++++++++++++++++++ .../ReplacePropertyWithMethodsTests.cs | 34 +++++++++++++++++++ .../ReplacePropertyWithMethodsTests.vb | 25 ++++++++++++++ ...CSharpReplacePropertyWithMethodsService.cs | 10 ++++-- ...stractReplacePropertyWithMethodsService.cs | 2 +- .../VisualBasicReplacePropertyWithMethods.vb | 14 ++++++-- 6 files changed, 113 insertions(+), 6 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs index 92899b6a2c0..fdfdfde8049 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs @@ -156,6 +156,40 @@ int Foo compareTokens: false); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)] + public async Task TestIndentation() + { + await TestAsync( +@"class C +{ + int [||]GetFoo() + { + int count; + foreach (var x in y) + { + count += bar; + } + return count; + } +}", +@"class C +{ + int Foo + { + get + { + int count; + foreach (var x in y) + { + count += bar; + } + return count; + } + } +}", +compareTokens: false); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)] public async Task TestIfDefMethod() { diff --git a/src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs index 77563d41a5d..c2366eb597f 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs @@ -637,6 +637,40 @@ private int GetProp() }", compareTokens: false); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)] + public async Task TestIndentation() + { + await TestAsync( +@"class C +{ + int [||]Foo + { + get + { + int count; + foreach (var x in y) + { + count += bar; + } + return count; + } + } +}", +@"class C +{ + int GetFoo() + { + int count; + foreach (var x in y) + { + count += bar; + } + return count; + } +}", +compareTokens: false); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)] public async Task TestComputedPropWithTrailingTriviaAfterArrow() { diff --git a/src/EditorFeatures/VisualBasicTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.vb b/src/EditorFeatures/VisualBasicTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.vb index aff6d650bc5..19edfbf1348 100644 --- a/src/EditorFeatures/VisualBasicTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.vb +++ b/src/EditorFeatures/VisualBasicTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.vb @@ -27,6 +27,31 @@ end class", end class") End Function + + Public Async Function TestIndentation() As Task + Await TestAsync( +"class C + readonly property [||]Prop as integer + get + dim count = 0 + for each x in y + count = count + z + next + reutrn count + end get + end property +end class", +"class C + Public Function GetProp() As Integer + dim count = 0 + for each x in y + count = count + z + next + reutrn count + End Function +end class", compareTokens:=False) + End Function + Public Async Function TestPrivateProperty() As Task Await TestAsync( diff --git a/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs b/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs index 1088def4ced..6da6294615f 100644 --- a/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs +++ b/src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs @@ -7,6 +7,8 @@ using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.ReplacePropertyWithMethods; using Roslyn.Utilities; +using System.Linq; +using Microsoft.CodeAnalysis.Formatting; namespace Microsoft.CodeAnalysis.CSharp.ReplacePropertyWithMethods { @@ -107,7 +109,7 @@ public override SyntaxNode GetPropertyDeclaration(SyntaxToken token) var statements = new List(); if (setAccessorDeclaration?.Body != null) { - statements.AddRange(setAccessorDeclaration?.Body?.Statements); + statements.AddRange(setAccessorDeclaration.Body.Statements.Select(WithMarkerAndAnnotation)); } else if (propertyBackingField != null) { @@ -120,6 +122,10 @@ public override SyntaxNode GetPropertyDeclaration(SyntaxToken token) return generator.MethodDeclaration(setMethod, desiredSetMethodName, statements); } + private static StatementSyntax WithMarkerAndAnnotation(StatementSyntax statement) + => statement.WithPrependedLeadingTrivia(SyntaxFactory.ElasticMarker) + .WithAdditionalAnnotations(Formatter.Annotation); + private static SyntaxNode GetGetMethod( SyntaxGenerator generator, PropertyDeclarationSyntax propertyDeclaration, @@ -146,7 +152,7 @@ public override SyntaxNode GetPropertyDeclaration(SyntaxToken token) var getAccessorDeclaration = (AccessorDeclarationSyntax)getMethod.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken); if (getAccessorDeclaration?.Body != null) { - statements.AddRange(getAccessorDeclaration.Body.Statements); + statements.AddRange(getAccessorDeclaration.Body.Statements.Select(WithMarkerAndAnnotation)); } else if (propertyBackingField != null) { diff --git a/src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs b/src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs index 595b97eab08..faa31857c73 100644 --- a/src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs +++ b/src/Features/Core/Portable/ReplacePropertyWithMethods/AbstractReplacePropertyWithMethodsService.cs @@ -392,4 +392,4 @@ public ReplaceParentArgs(ReferenceReplacer replacer, GetWriteValue getWriteValue } } } -} +} \ No newline at end of file diff --git a/src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb b/src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb index f80fb41ce17..01effc2dd97 100644 --- a/src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb +++ b/src/Features/VisualBasic/Portable/ReplacePropertyWithMethods/VisualBasicReplacePropertyWithMethods.vb @@ -1,6 +1,9 @@ -Imports System.Composition +' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +Imports System.Composition Imports System.Threading Imports Microsoft.CodeAnalysis.Editing +Imports Microsoft.CodeAnalysis.Formatting Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.ReplacePropertyWithMethods Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -108,7 +111,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP If TypeOf getAccessorDeclaration?.Parent Is AccessorBlockSyntax Then Dim block = DirectCast(getAccessorDeclaration.Parent, AccessorBlockSyntax) - statements.AddRange(block.Statements) + statements.AddRange(block.Statements.Select(AddressOf WithMarkerAndAnnotation)) ElseIf propertyBackingField IsNot Nothing Then Dim fieldReference = GetFieldReference(generator, propertyBackingField) statements.Add(generator.ReturnStatement(fieldReference)) @@ -117,6 +120,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP Return generator.MethodDeclaration(getMethod, desiredGetMethodName, statements) End Function + Private Shared Function WithMarkerAndAnnotation(statement As StatementSyntax) As StatementSyntax + Return statement.WithPrependedLeadingTrivia(SyntaxFactory.ElasticMarker). + WithAdditionalAnnotations(Formatter.Annotation) + End Function + Private Function GetSetMethod( generator As SyntaxGenerator, propertyStatement As PropertyStatementSyntax, @@ -132,7 +140,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP If TypeOf setAccessorDeclaration?.Parent Is AccessorBlockSyntax Then Dim block = DirectCast(setAccessorDeclaration.Parent, AccessorBlockSyntax) - statements.AddRange(block.Statements) + statements.AddRange(block.Statements.Select(AddressOf WithMarkerAndAnnotation)) ElseIf propertyBackingField IsNot Nothing Then Dim fieldReference = GetFieldReference(generator, propertyBackingField) statements.Add(generator.AssignmentStatement( -- GitLab