public virtual SyntaxTokenList VisitList(SyntaxTokenList list) { SyntaxTokenListBuilder alternate = null; var count = list.Count; var index = -1; foreach (var item in list) { index++; var visited = this.VisitToken(item); if (item != visited && alternate == null) { alternate = new SyntaxTokenListBuilder(count); alternate.Add(list, 0, index); } if (alternate != null && visited.CSharpKind() != SyntaxKind.None) //skip the null check since SyntaxToken is a value type { alternate.Add(visited); } } if (alternate != null) { return(alternate.ToList()); } return(list); }
/// <summary> /// Insert one or more tokens in the list at the specified index. /// </summary> /// <returns>A new list with the tokens inserted.</returns> public static SyntaxTokenList Insert(this SyntaxTokenList list, int index, params SyntaxToken[] items) { if (index < 0 || index > list.Count) { throw new ArgumentOutOfRangeException(nameof(index)); } if (items == null) { throw new ArgumentNullException(nameof(items)); } if (items.Length == 0) { return(list); } if (list.Count == 0) { var first = (Syntax.InternalSyntax.InternalSyntaxToken)items[0].Node; return(first.Language.SyntaxFactory.TokenList(items)); } else { var builder = new SyntaxTokenListBuilder(list.Count + items.Length); if (index > 0) { builder.Add(list, 0, index); } builder.Add(items); if (index < list.Count) { builder.Add(list, index, list.Count - index); } return(builder.ToList()); } }
private static GreenNode CreateNode(SyntaxToken[] tokens) { if (tokens == null) { return(null); } // TODO: we could remove the unnecessary builder allocations here and go directly // from the array to the List nodes. var builder = new SyntaxTokenListBuilder(tokens.Length); for (int i = 0; i < tokens.Length; i++) { builder.Add(tokens[i].Node); } return(builder.ToList().Node); }