提交 60a6e4cc 编写于 作者: C chborl

Code Cleanup

上级 316327ef
......@@ -47,6 +47,12 @@ internal override SyntaxNode GetProperty(SyntaxToken token)
return containingProperty;
}
internal override async Task<string> GetFieldNameAsync(Document document, IPropertySymbol propertySymbol, CancellationToken cancellationToken)
{
var rules = await GetNamingRulesAsync(document, cancellationToken).ConfigureAwait(false);
return GenerateFieldName(propertySymbol, rules);
}
/// <summary>
/// Get the user-specified naming rules, then add standard default naming rules
/// for both static and non-static fields. The standard naming rules are added at the end
......@@ -62,12 +68,12 @@ internal override SyntaxNode GetProperty(SyntaxToken token)
var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var namingPreferencesOption = optionSet.GetOption(SimplificationOptions.NamingPreferences);
var rules = namingPreferencesOption.CreateRules().NamingRules
.AddRange(GetDefaultRule(ImmutableArray.Create(new ModifierKind(ModifierKindEnum.IsStatic)), defaultStaticFieldPrefix))
.AddRange(GetDefaultRule(ImmutableArray.Create<ModifierKind>(), defaultFieldPrefix));
.AddRange(CreateNewRule(ImmutableArray.Create(new ModifierKind(ModifierKindEnum.IsStatic)), defaultStaticFieldPrefix))
.AddRange(CreateNewRule(ImmutableArray.Create<ModifierKind>(), defaultFieldPrefix));
return rules;
}
private static ImmutableArray<NamingRule> GetDefaultRule(
private static ImmutableArray<NamingRule> CreateNewRule(
ImmutableArray<ModifierKind> modifiers,
string prefix)
{
......@@ -101,7 +107,7 @@ private string GenerateFieldName(IPropertySymbol property, ImmutableArray<Naming
}
}
return GetUniqueName(fieldName, property);
return NameGenerator.GenerateUniqueName(fieldName, n => !property.ContainingType.GetMembers(n).Any());
}
internal override (SyntaxNode newGetAccessor, SyntaxNode newSetAccessor) GetNewAccessors(
......@@ -207,8 +213,6 @@ internal ExpressionBodyPreference GetAccessorExpressionBodyPreference(DocumentOp
internal ExpressionBodyPreference GetPropertyExpressionBodyPreference(DocumentOptionSet options)
=> options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedProperties).Value;
internal string GetUniqueName(string fieldName, IPropertySymbol property)
=> NameGenerator.GenerateUniqueName(fieldName, n => !property.ContainingType.GetMembers(n).Any());
internal override SyntaxNode GetTypeBlock(SyntaxNode syntaxNode)
=> syntaxNode;
......@@ -218,12 +222,5 @@ internal override SyntaxNode GetInitializerValue(SyntaxNode property)
internal override SyntaxNode GetPropertyWithoutInitializer(SyntaxNode property)
=> ((PropertyDeclarationSyntax)property).WithInitializer(null);
internal override async Task<string> GetFieldNameAsync(Document document, IPropertySymbol propertySymbol, CancellationToken cancellationToken)
{
var rules = await GetNamingRulesAsync(document, cancellationToken).ConfigureAwait(false);
var fieldName = GenerateFieldName(propertySymbol, rules);
return fieldName;
}
}
}
......@@ -18,12 +18,12 @@ internal abstract class AbstractConvertAutoPropertyToFullPropertyCodeRefactoring
{
internal abstract SyntaxNode GetProperty(SyntaxToken token);
internal abstract Task<string> GetFieldNameAsync(Document document, IPropertySymbol propertySymbol, CancellationToken cancellationToken);
internal abstract SyntaxNode GetInitializerValue(SyntaxNode property);
internal abstract SyntaxNode GetPropertyWithoutInitializer(SyntaxNode property);
internal abstract (SyntaxNode newGetAccessor, SyntaxNode newSetAccessor) GetNewAccessors(
DocumentOptionSet options, SyntaxNode property, string fieldName, SyntaxGenerator generator);
internal abstract SyntaxNode GetTypeBlock(SyntaxNode syntaxNode);
internal abstract SyntaxNode GetPropertyWithoutInitializer(SyntaxNode property);
internal abstract SyntaxNode GetInitializerValue(SyntaxNode property);
internal abstract SyntaxNode ConvertPropertyToExpressionBodyIfDesired(DocumentOptionSet options, SyntaxNode fullProperty);
internal abstract SyntaxNode GetTypeBlock(SyntaxNode syntaxNode);
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
......@@ -72,29 +72,13 @@ internal bool IsValidAutoProperty(SyntaxNode property, IPropertySymbol propertyS
CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var newRoot = await ExpandPropertyAndAddFieldAsync(
document,
property,
root,
propertySymbol,
generator,
cancellationToken).ConfigureAwait(false);
return document.WithSyntaxRoot(newRoot);
}
private async Task<SyntaxNode> ExpandPropertyAndAddFieldAsync(
Document document, SyntaxNode property, SyntaxNode root,
IPropertySymbol propertySymbol, SyntaxGenerator generator,
CancellationToken cancellationToken)
{
var workspace = document.Project.Solution.Workspace;
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
// Create full property. If the auto property had an initial value
// we need to remove it and later add it to the backing field
var fieldName = await GetFieldNameAsync(document, propertySymbol, cancellationToken).ConfigureAwait(false);
var accessorTuple = GetNewAccessors(options, property,fieldName, generator);
var accessorTuple = GetNewAccessors(options, property, fieldName, generator);
var fullProperty = generator
.WithAccessorDeclarations(
GetPropertyWithoutInitializer(property),
......@@ -102,23 +86,23 @@ internal bool IsValidAutoProperty(SyntaxNode property, IPropertySymbol propertyS
? new SyntaxNode[] { accessorTuple.newGetAccessor }
: new SyntaxNode[] { accessorTuple.newGetAccessor, accessorTuple.newSetAccessor })
.WithLeadingTrivia(property.GetLeadingTrivia());
fullProperty = ConvertPropertyToExpressionBodyIfDesired(options,fullProperty);
fullProperty = ConvertPropertyToExpressionBodyIfDesired(options, fullProperty);
var editor = new SyntaxEditor(root, workspace);
editor.ReplaceNode(property, fullProperty.WithAdditionalAnnotations(Formatter.Annotation));
// add backing field, plus initializer if it exists
var newField = CodeGenerationSymbolFactory.CreateFieldSymbol(
default, Accessibility.Private,
DeclarationModifiers.From(propertySymbol),
propertySymbol.Type, fieldName,
default, Accessibility.Private,
DeclarationModifiers.From(propertySymbol),
propertySymbol.Type, fieldName,
initializer: GetInitializerValue(property));
var containingType = GetTypeBlock(
propertySymbol.ContainingType.DeclaringSyntaxReferences.FirstOrDefault().GetSyntax(cancellationToken));
editor.ReplaceNode(containingType, (currentTypeDecl, _)
editor.ReplaceNode(containingType, (currentTypeDecl, _)
=> CodeGenerator.AddFieldDeclaration(currentTypeDecl, newField, workspace)
.WithAdditionalAnnotations(Formatter.Annotation));
return editor.GetChangedRoot();
var newRoot = editor.GetChangedRoot();
return document.WithSyntaxRoot(newRoot);
}
private class ConvertAutoPropertyToFullPropertyCodeAction : CodeAction.DocumentChangeAction
......
......@@ -39,6 +39,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ConvertAutoPropertyToFullProperty
Return containingProperty
End Function
''' <summary>
''' In VB, auto properties have an implicit backing field that is named using the property
''' name preceded by an underscore. We will use this as the field name so we don't mess up
''' any existing references to this field.
''' </summary>
Friend Overrides Function GetFieldNameAsync(document As Document, propertySymbol As IPropertySymbol, cancellationToken As CancellationToken) As Task(Of String)
Return Task.FromResult(Underscore + propertySymbol.Name)
End Function
Friend Overrides Function GetNewAccessors(options As DocumentOptionSet, propertyNode As SyntaxNode,
fieldName As String, generator As SyntaxGenerator) _
As (newGetAccessor As SyntaxNode, newSetAccessor As SyntaxNode)
......@@ -77,29 +86,20 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ConvertAutoPropertyToFullProperty
Return False
End Function
''' <summary>
''' In VB, auto properties have an implicit backing field that is named using the property
''' name preceded by an underscore. We will use this as the field name so we don't mess up
''' any existing references to this field.
''' </summary>
Friend Overrides Function GetFieldNameAsync(document As Document, propertySymbol As IPropertySymbol, cancellationToken As CancellationToken) As Task(Of String)
Return Task.FromResult(Underscore + propertySymbol.Name)
End Function
Friend Overrides Function GetTypeBlock(syntaxNode As SyntaxNode) As SyntaxNode
Return DirectCast(syntaxNode, TypeStatementSyntax).Parent
Friend Overrides Function GetPropertyWithoutInitializer(propertyNode As SyntaxNode) As SyntaxNode
Return DirectCast(propertyNode, PropertyStatementSyntax).WithInitializer(Nothing)
End Function
Friend Overrides Function GetInitializerValue(propertyNode As SyntaxNode) As SyntaxNode
Return DirectCast(propertyNode, PropertyStatementSyntax).Initializer?.Value
End Function
Friend Overrides Function GetPropertyWithoutInitializer(propertyNode As SyntaxNode) As SyntaxNode
Return DirectCast(propertyNode, PropertyStatementSyntax).WithInitializer(Nothing)
End Function
Friend Overrides Function ConvertPropertyToExpressionBodyIfDesired(options As DocumentOptionSet, propertyNode As SyntaxNode) As SyntaxNode
Return propertyNode
End Function
Friend Overrides Function GetTypeBlock(syntaxNode As SyntaxNode) As SyntaxNode
Return DirectCast(syntaxNode, TypeStatementSyntax).Parent
End Function
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册