提交 bf5fdb74 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #15077 from CyrusNajmabadi/replacePropertyIndentation

Indent code when converting a property to a method.
Fixes #14846
......@@ -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()
{
......
......@@ -10,9 +10,7 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions.ReplaceProp
public class ReplacePropertyWithMethodsTests : AbstractCSharpCodeActionTest
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace)
{
return new ReplacePropertyWithMethodsCodeRefactoringProvider();
}
=> new ReplacePropertyWithMethodsCodeRefactoringProvider();
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)]
public async Task TestGetWithBody()
......@@ -637,6 +635,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
{
private 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()
{
......
......@@ -607,5 +607,30 @@ public class Foo
End Property
end class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)>
Public Async Function TestIndentation() As Task
Await TestAsync(
"class C
Public Function [||]GetProp() As Integer
dim count = 0
for each x in y
count = count + z
next
return count
End Function
end class",
"class C
Public ReadOnly Property Prop As Integer
Get
dim count = 0
for each x in y
count = count + z
next
return count
End Get
End Property
end class", compareTokens:=False)
End Function
End Class
End Namespace
\ No newline at end of file
......@@ -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
return 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
return count
End Function
end class", compareTokens:=False)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplacePropertyWithMethods)>
Public Async Function TestPrivateProperty() As Task
Await TestAsync(
......
using System.Collections.Generic;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.ReplacePropertyWithMethods;
using Roslyn.Utilities;
......@@ -107,7 +111,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(WithFormattingAnnotation));
}
else if (propertyBackingField != null)
{
......@@ -120,6 +124,9 @@ public override SyntaxNode GetPropertyDeclaration(SyntaxToken token)
return generator.MethodDeclaration(setMethod, desiredSetMethodName, statements);
}
private static StatementSyntax WithFormattingAnnotation(StatementSyntax statement)
=> statement.WithAdditionalAnnotations(Formatter.Annotation);
private static SyntaxNode GetGetMethod(
SyntaxGenerator generator,
PropertyDeclarationSyntax propertyDeclaration,
......@@ -146,7 +153,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(WithFormattingAnnotation));
}
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 WithFormattingAnnotation))
ElseIf propertyBackingField IsNot Nothing Then
Dim fieldReference = GetFieldReference(generator, propertyBackingField)
statements.Add(generator.ReturnStatement(fieldReference))
......@@ -117,6 +120,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
Return generator.MethodDeclaration(getMethod, desiredGetMethodName, statements)
End Function
Private Shared Function WithFormattingAnnotation(statement As StatementSyntax) As StatementSyntax
Return statement.WithAdditionalAnnotations(Formatter.Annotation)
End Function
Private Function GetSetMethod(
generator As SyntaxGenerator,
propertyStatement As PropertyStatementSyntax,
......@@ -132,7 +139,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 WithFormattingAnnotation))
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.
先完成此消息的编辑!
想要评论请 注册