private static AccessorDeclarationSyntax CreateGetAccessor(GetAndSetMethods getAndSetMethods) { var getMethodDeclaration = getAndSetMethods.GetMethodDeclaration as MethodDeclarationSyntax; var accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration); if (getMethodDeclaration.ExpressionBody != null && getAndSetMethods.SetMethodDeclaration != null) { var statement = SyntaxFactory.ReturnStatement(getMethodDeclaration.ExpressionBody.Expression) .WithSemicolonToken(getMethodDeclaration.SemicolonToken); return(accessor.WithBody(SyntaxFactory.Block(statement))); } if (getMethodDeclaration.SemicolonToken.Kind() != SyntaxKind.None) { return(accessor.WithSemicolonToken(getMethodDeclaration.SemicolonToken)); } if (getMethodDeclaration.Body != null) { return(accessor.WithBody(getMethodDeclaration.Body.WithAdditionalAnnotations(Formatter.Annotation))); } return(accessor); }
public PropertyDeclarationSyntax ConvertMethodsToPropertyWorker( DocumentOptionSet documentOptions, ParseOptions parseOptions, SemanticModel semanticModel, SyntaxGenerator generator, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged) { var getMethodDeclaration = getAndSetMethods.GetMethodDeclaration as MethodDeclarationSyntax; var getAccessor = CreateGetAccessor(getAndSetMethods, documentOptions, parseOptions); var setAccessor = CreateSetAccessor(semanticModel, generator, getAndSetMethods, documentOptions, parseOptions); var property = SyntaxFactory.PropertyDeclaration( getMethodDeclaration.AttributeLists, getMethodDeclaration.Modifiers, getMethodDeclaration.ReturnType, getMethodDeclaration.ExplicitInterfaceSpecifier, GetPropertyName(getMethodDeclaration.Identifier, propertyName, nameChanged), accessorList: null); IEnumerable <SyntaxTrivia> trivia = getMethodDeclaration.GetLeadingTrivia(); var setMethodDeclaration = getAndSetMethods.SetMethodDeclaration; if (setMethodDeclaration != null) { trivia = trivia.Concat(setMethodDeclaration.GetLeadingTrivia()); } property = property.WithLeadingTrivia(trivia.Where(t => !t.IsDirective)); var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(getAccessor)); if (setAccessor != null) { accessorList = accessorList.AddAccessors(setAccessor); } property = property.WithAccessorList(accessorList); return(property.WithAdditionalAnnotations(Formatter.Annotation)); }
public void ReplaceGetMethodWithProperty( DocumentOptionSet documentOptions, ParseOptions parseOptions, SyntaxEditor editor, SemanticModel semanticModel, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged ) { if ( !( getAndSetMethods.GetMethodDeclaration is MethodDeclarationSyntax getMethodDeclaration ) ) { return; } var newProperty = ConvertMethodsToProperty( documentOptions, parseOptions, semanticModel, editor.Generator, getAndSetMethods, propertyName, nameChanged ); editor.ReplaceNode(getMethodDeclaration, newProperty); }
private static AccessorDeclarationSyntax CreateSetAccessor( SemanticModel semanticModel, SyntaxGenerator generator, GetAndSetMethods getAndSetMethods, DocumentOptionSet documentOptions, ParseOptions parseOptions) { var accessorDeclaration = CreateSetAccessorWorker(semanticModel, generator, getAndSetMethods); return(UseExpressionOrBlockBodyIfDesired(documentOptions, parseOptions, accessorDeclaration)); }
private static AccessorDeclarationSyntax CreateGetAccessor( GetAndSetMethods getAndSetMethods, DocumentOptionSet documentOptions, ParseOptions parseOptions) { var accessorDeclaration = CreateGetAccessorWorker(getAndSetMethods); return(UseExpressionOrBlockBodyIfDesired( documentOptions, parseOptions, accessorDeclaration)); }
public SyntaxNode ConvertMethodsToProperty( DocumentOptionSet documentOptions, ParseOptions parseOptions, SemanticModel semanticModel, SyntaxGenerator generator, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged) { var propertyDeclaration = ConvertMethodsToPropertyWorker( documentOptions, parseOptions, semanticModel, generator, getAndSetMethods, propertyName, nameChanged); var expressionBodyPreference = documentOptions.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedProperties).Value; if (expressionBodyPreference != ExpressionBodyPreference.Never) { if (propertyDeclaration.AccessorList?.Accessors.Count == 1 && propertyDeclaration.AccessorList?.Accessors[0].Kind() == SyntaxKind.GetAccessorDeclaration) { var getAccessor = propertyDeclaration.AccessorList.Accessors[0]; if (getAccessor.ExpressionBody != null) { return(propertyDeclaration.WithExpressionBody(getAccessor.ExpressionBody) .WithSemicolonToken(getAccessor.SemicolonToken) .WithAccessorList(null)); } else if (getAccessor.Body != null && getAccessor.Body.TryConvertToExpressionBody( parseOptions, expressionBodyPreference, out var arrowExpression, out var semicolonToken)) { return(propertyDeclaration.WithExpressionBody(arrowExpression) .WithSemicolonToken(semicolonToken) .WithAccessorList(null)); } } } else { if (propertyDeclaration.ExpressionBody != null && propertyDeclaration.ExpressionBody.TryConvertToBlock( propertyDeclaration.SemicolonToken, createReturnStatementForExpression: true, block: out var block)) { var accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithBody(block); var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(accessor)); return(propertyDeclaration.WithAccessorList(accessorList) .WithExpressionBody(null) .WithSemicolonToken(default(SyntaxToken))); } } return(propertyDeclaration); }
public void ReplaceGetMethodWithProperty( SyntaxEditor editor, SemanticModel semanticModel, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged) { var getMethodDeclaration = getAndSetMethods.GetMethodDeclaration as MethodDeclarationSyntax; if (getMethodDeclaration == null) { return; } editor.ReplaceNode(getMethodDeclaration, ConvertMethodsToProperty(semanticModel, editor.Generator, getAndSetMethods, propertyName, nameChanged)); }
private static AccessorDeclarationSyntax CreateSetAccessorWorker( SemanticModel semanticModel, SyntaxGenerator generator, GetAndSetMethods getAndSetMethods) { var setMethodDeclaration = getAndSetMethods.SetMethodDeclaration as MethodDeclarationSyntax; var setMethod = getAndSetMethods.SetMethod; if (setMethodDeclaration == null || setMethod?.Parameters.Length != 1) { return(null); } var getMethod = getAndSetMethods.GetMethod; var accessor = SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration); if (getMethod.DeclaredAccessibility != setMethod.DeclaredAccessibility) { accessor = (AccessorDeclarationSyntax)generator.WithAccessibility(accessor, setMethod.DeclaredAccessibility); } if (setMethodDeclaration.ExpressionBody != null) { var oldExpressionBody = setMethodDeclaration.ExpressionBody; var expression = ReplaceReferencesToParameterWithValue( semanticModel, setMethod.Parameters[0], oldExpressionBody.Expression); return(accessor.WithExpressionBody(oldExpressionBody.WithExpression(expression)) .WithSemicolonToken(setMethodDeclaration.SemicolonToken)); } if (setMethodDeclaration.SemicolonToken.Kind() != SyntaxKind.None) { return(accessor.WithSemicolonToken(setMethodDeclaration.SemicolonToken)); } if (setMethodDeclaration.Body != null) { var body = ReplaceReferencesToParameterWithValue(semanticModel, setMethod.Parameters[0], setMethodDeclaration.Body); return(accessor.WithBody(body.WithAdditionalAnnotations(Formatter.Annotation))); } return(accessor); }
public SyntaxNode ConvertMethodsToProperty( SemanticModel semanticModel, SyntaxGenerator generator, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged) { var getMethodDeclaration = getAndSetMethods.GetMethodDeclaration as MethodDeclarationSyntax; var getAccessor = CreateGetAccessor(getAndSetMethods); var setAccessor = CreateSetAccessor(semanticModel, generator, getAndSetMethods); var property = SyntaxFactory.PropertyDeclaration( getMethodDeclaration.AttributeLists, getMethodDeclaration.Modifiers, getMethodDeclaration.ReturnType, getMethodDeclaration.ExplicitInterfaceSpecifier, GetPropertyName(getMethodDeclaration.Identifier, propertyName, nameChanged), accessorList: null); IEnumerable <SyntaxTrivia> trivia = getMethodDeclaration.GetLeadingTrivia(); var setMethodDeclaration = getAndSetMethods.SetMethodDeclaration; if (setMethodDeclaration != null) { trivia = trivia.Concat(setMethodDeclaration.GetLeadingTrivia()); } property = property.WithLeadingTrivia(trivia.Where(t => !t.IsDirective)); if (getMethodDeclaration.ExpressionBody != null && setMethodDeclaration == null) { property = property.WithExpressionBody(getMethodDeclaration.ExpressionBody); property = property.WithSemicolonToken(getMethodDeclaration.SemicolonToken); } else { var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(getAccessor)); if (setAccessor != null) { accessorList = accessorList.AddAccessors(new[] { setAccessor }); } property = property.WithAccessorList(accessorList); } return(property.WithAdditionalAnnotations(Formatter.Annotation)); }
public void ReplaceGetMethodWithProperty( CodeGenerationOptions options, ParseOptions parseOptions, SyntaxEditor editor, SemanticModel semanticModel, GetAndSetMethods getAndSetMethods, string propertyName, bool nameChanged) { if (getAndSetMethods.GetMethodDeclaration is not MethodDeclarationSyntax getMethodDeclaration) { return; } var languageVersion = parseOptions.LanguageVersion(); var newProperty = ConvertMethodsToProperty( (CSharpCodeGenerationOptions)options, languageVersion, semanticModel, editor.Generator, getAndSetMethods, propertyName, nameChanged); editor.ReplaceNode(getMethodDeclaration, newProperty); }