public static IEnumerable <AttributeListSyntax> SplitAttributes(AttributeListSyntax attributeList)
        {
            if (attributeList == null)
            {
                throw new ArgumentNullException(nameof(attributeList));
            }

            SeparatedSyntaxList <AttributeSyntax> attributes = attributeList.Attributes;

            for (int i = 0; i < attributes.Count; i++)
            {
                AttributeListSyntax list = AttributeList(attributes[i]);

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list.WithFormatterAnnotation());
            }
        }
        public static IEnumerable <AttributeListSyntax> SplitAttributes(AttributeListSyntax attributeList)
        {
            if (attributeList == null)
            {
                throw new ArgumentNullException(nameof(attributeList));
            }

            for (int i = 0; i < attributeList.Attributes.Count; i++)
            {
                AttributeListSyntax list = SyntaxFactory.AttributeList(
                    SyntaxFactory.SingletonSeparatedList(attributeList.Attributes[i]));

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributeList.Attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list);
            }
        }
예제 #3
0
            AttributeListSyntax FindInList(AttributeListSyntax originalAttributeList, bool add)
            {
                if (found)
                {
                    return(originalAttributeList);
                }

                List <AttributeSyntax> attributes = new List <AttributeSyntax>();

                foreach (var attribute in originalAttributeList.Attributes)
                {
                    if (found)
                    {
                        attributes.Add(attribute);
                        continue;
                    }

                    var p = semanticModel.GetSymbolInfo(attribute);
                    if (p.Symbol.ContainingType.Equals(mocklisSymbols.GeneratedCodeAttribute))
                    {
                        found = true;
                        attributes.Add(typesForSymbols.GeneratedCodeAttribute());
                    }
                    else if (add && p.Symbol.ContainingType.Equals(mocklisSymbols.MocklisClassAttribute))
                    {
                        found = true;
                        attributes.Add(attribute);
                        attributes.Add(typesForSymbols.GeneratedCodeAttribute());
                    }
                    else
                    {
                        attributes.Add(attribute);
                    }
                }

                if (found)
                {
                    var newAttributeList = F.AttributeList(F.SeparatedList(attributes));
                    if (originalAttributeList.HasLeadingTrivia)
                    {
                        newAttributeList = newAttributeList.WithLeadingTrivia(originalAttributeList.GetLeadingTrivia());
                    }

                    if (originalAttributeList.HasTrailingTrivia)
                    {
                        newAttributeList = newAttributeList.WithTrailingTrivia(originalAttributeList.GetTrailingTrivia());
                    }

                    return(newAttributeList);
                }

                return(found ? F.AttributeList(F.SeparatedList(attributes)) : originalAttributeList);
            }
예제 #4
0
        public static IEnumerable <AttributeListSyntax> SplitAttributeList(AttributeListSyntax attributeList)
        {
            SeparatedSyntaxList <AttributeSyntax> attributes = attributeList.Attributes;

            for (int i = 0; i < attributes.Count; i++)
            {
                AttributeListSyntax list = AttributeList(attributes[i]);

                if (i == 0)
                {
                    list = list.WithLeadingTrivia(attributeList.GetLeadingTrivia());
                }

                if (i == attributes.Count - 1)
                {
                    list = list.WithTrailingTrivia(attributeList.GetTrailingTrivia());
                }

                yield return(list);
            }
        }
예제 #5
0
 /// <summary>
 /// Return a "null attribute list" that only has trivia, no actual syntax.
 /// This is used when we want to remove an attribute list but keep nearby comments
 /// (e.g. the class documentation comments that can appear above an attribute).
 /// If we just used "null" we would lose those comments.
 /// </summary>
 private static AttributeListSyntax NullAttributeList(AttributeListSyntax node)
 {
     return(AttributeList(
                MissingToken(SyntaxKind.OpenBracketToken),
                null, new SeparatedSyntaxList <AttributeSyntax>(),
                MissingToken(SyntaxKind.CloseBracketToken)
                )
            // get rid of the indentation that would appear between the attribute list and the
            // doc comments, but we still want the indentation before the doc comments.
            .WithLeadingTrivia(
                node
                .GetLeadingTrivia()
                .Reverse()
                .SkipWhile(t => t.Kind() == SyntaxKind.WhitespaceTrivia)
                .Reverse()
                )
            .WithTrailingTrivia(
                node
                .GetTrailingTrivia()
                .Where(t => t.Kind() != SyntaxKind.EndOfLineTrivia)
                ));
 }
예제 #6
0
        private static List <AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia)
        {
            var newAttributeLists = new List <AttributeListSyntax>();

            for (var i = 0; i < attributeList.Attributes.Count; i++)
            {
                var newAttributes = SyntaxFactory.SingletonSeparatedList(
                    attributeList.Attributes[i].WithLeadingTrivia(
                        attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
                var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);

                newAttributeList = (i == 0)
                    ? newAttributeList.WithLeadingTrivia(attributeList.GetLeadingTrivia())
                    : newAttributeList.WithLeadingTrivia(indentationTrivia);

                newAttributeList = (i == (attributeList.Attributes.Count - 1))
                    ? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia())
                    : newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                newAttributeLists.Add(newAttributeList);
            }

            return(newAttributeLists);
        }
        private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia)
        {
            var newAttributeLists = new List<AttributeListSyntax>();

            for (var i = 0; i < attributeList.Attributes.Count; i++)
            {
                var newAttributes = SyntaxFactory.SingletonSeparatedList(
                    attributeList.Attributes[i].WithLeadingTrivia(
                        attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
                var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);

                newAttributeList = (i == 0)
                    ? newAttributeList.WithLeadingTrivia(attributeList.GetLeadingTrivia())
                    : newAttributeList.WithLeadingTrivia(indentationTrivia);

                newAttributeList = (i == (attributeList.Attributes.Count - 1))
                    ? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia())
                    : newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                newAttributeLists.Add(newAttributeList);
            }

            return newAttributeLists;
        }