InsertRange() public method

Creates a new SyntaxNodeOrTokenList with the specified nodes or tokens inserted at the index.
public InsertRange ( int index, IEnumerable nodesAndTokens ) : SyntaxNodeOrTokenList
index int The index to insert at.
nodesAndTokens IEnumerable The nodes or tokens to insert.
return SyntaxNodeOrTokenList
Esempio n. 1
0
        /// <summary>
        /// Creates a new list with the specified nodes inserted at the index.
        /// </summary>
        /// <param name="index">The index to insert at.</param>
        /// <param name="nodes">The nodes to insert.</param>
        public SeparatedSyntaxList <TNode> InsertRange(int index, IEnumerable <TNode> nodes)
        {
            if (nodes == null)
            {
                throw new ArgumentNullException(nameof(nodes));
            }

            if (index < 0 || index > this.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            SyntaxNodeOrTokenList nodesWithSeps = this.GetWithSeparators();
            int insertionIndex = index < this.Count ? nodesWithSeps.IndexOf(this[index]) : nodesWithSeps.Count;

            // determine how to deal with separators (commas)
            if (insertionIndex > 0 && insertionIndex < nodesWithSeps.Count)
            {
                SyntaxNodeOrToken previous = nodesWithSeps[insertionIndex - 1];
                if (previous.IsToken && !KeepSeparatorWithPreviousNode(previous.AsToken()))
                {
                    // pull back so item in inserted before separator
                    insertionIndex--;
                }
            }

            List <SyntaxNodeOrToken> nodesToInsertWithSeparators = new List <SyntaxNodeOrToken>();

            foreach (TNode item in nodes)
            {
                if (item != null)
                {
                    // if item before insertion point is a node, add a separator
                    if (nodesToInsertWithSeparators.Count > 0 || (insertionIndex > 0 && nodesWithSeps[insertionIndex - 1].IsNode))
                    {
                        nodesToInsertWithSeparators.Add(item.Green.CreateSeparator <TNode>(item));
                    }

                    nodesToInsertWithSeparators.Add(item);
                }
            }

            // if item after last inserted node is a node, add separator
            if (insertionIndex < nodesWithSeps.Count && nodesWithSeps[insertionIndex].IsNode)
            {
                SyntaxNode node = nodesWithSeps[insertionIndex].AsNode();
                nodesToInsertWithSeparators.Add(node.Green.CreateSeparator <TNode>(node)); // separator
            }

            return(new SeparatedSyntaxList <TNode>(nodesWithSeps.InsertRange(insertionIndex, nodesToInsertWithSeparators)));
        }