Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SyntaxListBuilder{TNode}"/> structure
 /// from the specified <see cref="SyntaxListBuilder"/> instance.
 /// </summary>
 /// <param name="builder">The <see cref="SyntaxListBuilder"/> which is encapsulated by this instance.</param>
 public SyntaxListBuilder(SyntaxListBuilder builder)
 {
     this.builder = builder;
 }
        /// <summary>
        /// Rewrites the specified trivia to include the specified number of trailing
        /// line breaks and spaces, as well as any other additional trivia which are
        /// deemed to be required.
        /// </summary>
        private static SyntaxNode RewriteTrivia(SyntaxNode trivia,
                                                Int32 neededLineBreaks, Int32 neededSpaces, Int32 indentation)
        {
            var needsLineBreakAfterExistingTrivia = false;
            var needsSpaceAfterExistingTrivia     = false;

            if (trivia != null)
            {
                if (trivia.IsList)
                {
                    for (int i = 0; i < trivia.SlotCount; i++)
                    {
                        var item       = trivia.GetSlot(i) as SyntaxTrivia;
                        var itemIsLast = (i + 1 == trivia.SlotCount);
                        EvaluateTriviaForRewrite(item,
                                                 ref needsLineBreakAfterExistingTrivia, ref needsSpaceAfterExistingTrivia, itemIsLast);
                    }
                }
                else
                {
                    EvaluateTriviaForRewrite(trivia as SyntaxTrivia,
                                             ref needsLineBreakAfterExistingTrivia, ref needsSpaceAfterExistingTrivia, true);
                }
            }

            var triviaList        = default(SyntaxList <SyntaxTrivia>);
            var triviaListBuilder = default(SyntaxListBuilder <SyntaxTrivia>);

            var triviaChanged =
                needsLineBreakAfterExistingTrivia ||
                needsSpaceAfterExistingTrivia ||
                neededLineBreaks > 0 ||
                neededSpaces > 0;

            if (triviaChanged)
            {
                triviaListBuilder = SyntaxListBuilder <SyntaxTrivia> .Create();

                if (trivia != null)
                {
                    triviaList = new SyntaxList <SyntaxTrivia>(trivia);
                    triviaListBuilder.AddRange(triviaList);
                }

                if (needsLineBreakAfterExistingTrivia)
                {
                    triviaListBuilder.Add(GetLineBreakTrivia(1, indentation));
                    neededLineBreaks--;
                    neededSpaces--;
                }
                else
                {
                    if (needsSpaceAfterExistingTrivia)
                    {
                        triviaListBuilder.Add(GetSpaceTrivia(1));
                        neededSpaces--;
                    }
                }

                if (neededLineBreaks > 0)
                {
                    triviaListBuilder.Add(GetLineBreakTrivia(neededLineBreaks, indentation));
                }
                else
                {
                    if (neededSpaces > 0)
                    {
                        triviaListBuilder.Add(GetSpaceTrivia(neededSpaces));
                    }
                }
            }

            return(triviaListBuilder.IsNull ? trivia : triviaListBuilder.ToListNode());
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SeparatedSyntaxListBuilder{TNode}"/> structure
 /// from the specified <see cref="SyntaxListBuilder"/> instance.
 /// </summary>
 /// <param name="builder">The <see cref="SyntaxListBuilder"/> which is encapsulated by this instance.</param>
 public SeparatedSyntaxListBuilder(SyntaxListBuilder builder)
 {
     this.builder = builder;
 }