提交 3069891d 编写于 作者: C CyrusNajmabadi

Indent code when converting a property to a method.

上级 f6adbb43
......@@ -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()
{
......
......@@ -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()
{
......
......@@ -27,6 +27,31 @@ end class",
end class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)>
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
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)>
Public Async Function TestPrivateProperty() As Task
Await TestAsync(
......
......@@ -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<SyntaxNode>();
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)
{
......
......@@ -392,4 +392,4 @@ public ReplaceParentArgs(ReferenceReplacer replacer, GetWriteValue getWriteValue
}
}
}
}
}
\ No newline at end of file
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(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册