Example #1
0
        private static Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax property,
            string methodName,
            CancellationToken cancellationToken = default)
        {
            AccessorDeclarationSyntax setter = property.Setter();

            string propertyName = property.Identifier.ValueText;

            ExpressionSyntax argumentExpression;

            if (document.SupportsLanguageFeature(CSharpLanguageFeature.NameOf))
            {
                argumentExpression = NameOfExpression(propertyName);
            }
            else
            {
                argumentExpression = StringLiteralExpression(propertyName);
            }

            IdentifierNameSyntax backingFieldName = GetBackingFieldIdentifierName(setter).WithoutTrivia();

            AccessorDeclarationSyntax newSetter = SetAccessorDeclaration(
                Block(
                    IfStatement(
                        NotEqualsExpression(
                            backingFieldName,
                            IdentifierName("value")),
                        Block(
                            SimpleAssignmentStatement(
                                backingFieldName,
                                IdentifierName("value")),
                            ExpressionStatement(
                                InvocationExpression(
                                    IdentifierName(methodName),
                                    ArgumentList(Argument(argumentExpression))))))));

            newSetter = newSetter
                        .WithTriviaFrom(property)
                        .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(setter, newSetter, cancellationToken));
        }
Example #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax property,
            bool supportsCSharp6,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            AccessorDeclarationSyntax setter = property.Setter();

            AccessorDeclarationSyntax newSetter = CreateSetter(
                GetBackingFieldIdentifierName(setter).WithoutTrivia(),
                property.Identifier.ValueText,
                supportsCSharp6);

            newSetter = newSetter
                        .WithTriviaFrom(property)
                        .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(setter, newSetter, cancellationToken).ConfigureAwait(false));
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax property,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            AccessorDeclarationSyntax setter = property.Setter();

            AccessorDeclarationSyntax newSetter = CreateSetter(
                GetBackingFieldIdentifierName(setter).WithoutTrivia(),
                property.Identifier.ValueText);

            newSetter = newSetter
                        .WithTriviaFrom(property)
                        .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(setter, newSetter);

            return(document.WithSyntaxRoot(newRoot));
        }