Exemple #1
0
        internal static AccessorListSyntax WithAccessor(
            this AccessorListSyntax parentSyntax,
            AccessorDeclarationSyntax childSyntax,
            SyntaxKind kind)
        {
            AccessorDeclarationSyntax oldChildSyntax       = GetAccessorDeclaration(parentSyntax, kind);
            SyntaxList <AccessorDeclarationSyntax>?newList = null;

            if (childSyntax == null)
            {
                if (oldChildSyntax != null)
                {
                    newList = parentSyntax.Accessors.Remove(oldChildSyntax);
                }
            }
            else if (oldChildSyntax == null)
            {
                newList = parentSyntax.Accessors.Add(childSyntax);
            }
            else
            {
                newList = parentSyntax.Accessors.Replace(oldChildSyntax, childSyntax);
            }

            if (newList != null)
            {
                parentSyntax = parentSyntax.WithAccessors(newList.Value);
            }

            return(parentSyntax);
        }
        private static AccessorListSyntax CreateAccessorList(PropertyDeclarationSyntax property)
        {
            if (property.ExpressionBody != null)
            {
                return(AccessorList(AutoGetAccessorDeclaration())
                       .WithTriviaFrom(property.ExpressionBody));
            }
            else
            {
                AccessorListSyntax accessorList = property.AccessorList;

                IEnumerable <AccessorDeclarationSyntax> newAccessors = accessorList
                                                                       .Accessors
                                                                       .Select(accessor =>
                {
                    return(accessor
                           .WithBody(null)
                           .WithExpressionBody(null)
                           .WithSemicolonToken(SemicolonToken())
                           .WithTriviaFrom(accessor));
                });

                return(accessorList.WithAccessors(List(newAccessors)));
            }
        }
Exemple #3
0
        public static EventDeclarationSyntax WithAccessor(
            this EventDeclarationSyntax syntax,
            SyntaxKind kind,
            AccessorDeclarationSyntax accessor)
        {
            AccessorListSyntax accessorList = syntax.AccessorList;

            return(syntax.WithAccessorList(
                       accessorList.WithAccessors(accessorList.Accessors.Replace(GetAccessor(syntax, kind), accessor))));
        }
        private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList == null)
            {
                var getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                             .WithEosToken(SyntaxFactory.Token(SyntaxKind.EndOfLineTrivia));
                return(SyntaxFactory.AccessorList(SyntaxFactory.List(Enumerable.Repeat(getter, 1))));
            }

            return(accessorList.WithAccessors(SyntaxFactory.List(GetAccessors(accessorList.Accessors))));
        }
                public override SyntaxNode VisitAccessorList(AccessorListSyntax node)
                {
                    if (node != this.ContainerOfStatementsOrFieldToReplace)
                    {
                        return(base.VisitAccessorList(node));
                    }

                    var newAccessors = VisitList(ReplaceAccessors(node.Accessors));

                    return(node.WithAccessors(newAccessors));
                }
        public static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = property.AccessorList;

            if (accessorList != null)
            {
                SyntaxList <AccessorDeclarationSyntax> newAccessors = accessorList
                                                                      .Accessors
                                                                      .Select(accessor =>
                {
                    accessor = accessor.Update(
                        attributeLists: accessor.AttributeLists,
                        modifiers: accessor.Modifiers,
                        keyword: accessor.Keyword,
                        body: default(BlockSyntax),
                        expressionBody: default(ArrowExpressionClauseSyntax),
                        semicolonToken: SemicolonToken());

                    return(accessor.WithTriviaFrom(accessor));
                })
                                                                      .ToSyntaxList();

                accessorList = accessorList.WithAccessors(newAccessors);
            }
            else
            {
                accessorList = AccessorList(AutoGetAccessorDeclaration())
                               .WithTriviaFrom(property.ExpressionBody);
            }

            if (accessorList
                .DescendantTrivia()
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            PropertyDeclarationSyntax newProperty = property.Update(
                attributeLists: property.AttributeLists,
                modifiers: property.Modifiers,
                type: property.Type,
                explicitInterfaceSpecifier: property.ExplicitInterfaceSpecifier,
                identifier: property.Identifier.WithTrailingTrivia(Space),
                accessorList: accessorList,
                expressionBody: default(ArrowExpressionClauseSyntax),
                initializer: initializer,
                semicolonToken: (initializer != null) ? SemicolonToken() : default(SyntaxToken));

            return(newProperty
                   .WithTriviaFrom(property)
                   .WithFormatterAnnotation());
        }
        public override SyntaxNode VisitAccessorList(AccessorListSyntax node)
        {
            node = (AccessorListSyntax)base.VisitAccessorList(node);

            if (node.Accessors.Any(a => a.ExpressionBody != null))
            {
                var list = new List <AccessorDeclarationSyntax>();
                foreach (var accessor in node.Accessors)
                {
                    if (accessor != null && accessor.ExpressionBody != null)
                    {
                        list.Add(SyntaxHelper.ToStatementBody(accessor));
                    }
                    else
                    {
                        list.Add(accessor);
                    }
                }

                node = node.WithAccessors(SyntaxFactory.List(list)).NormalizeWhitespace();
            }

            return(node);
        }
 private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorList)
 {
     return(accessorList.WithAccessors(SyntaxFactory.List(GetAccessors(accessorList.Accessors))));
 }
        private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorListSyntax, TypeSyntax type)
        {
            var specialType = _typeTranslation.FirstOrDefault(tt => PrettyTypeName(tt.ActualType) == type.ToFullString().Replace("global::", ""));

            if (specialType != null)
            {
                return accessorListSyntax.WithAccessors(
                    SyntaxFactory.List<AccessorDeclarationSyntax>(
                        accessorListSyntax.Accessors.Select(a =>
                            a.WithBody(UpdateMethodBody(
                                a.Body,
                                type,
                                SyntaxFactory.ParameterList(
                                    SyntaxFactory.SeparatedList<ParameterSyntax>( new [] { SyntaxFactory.Parameter(SyntaxFactory.ParseToken("value")).WithType(type) }))))))
                    );
            }

            return accessorListSyntax;
        }