public static SeparatedSyntaxList <T> InsertRangeWithTrailingSeparator <T>( this SeparatedSyntaxList <T> separatedList, int index, IEnumerable <T> nodes, SyntaxKind separator ) where T : SyntaxNode { var newList = separatedList.InsertRange(index, nodes); if (index < separatedList.Count) { return(newList); } var nodesAndTokens = newList.GetWithSeparators(); if (!nodesAndTokens.Last().IsNode) { return(newList); } return(SyntaxFactory.SeparatedList <T>( nodesAndTokens.Add(SyntaxFactory.Token(separator)) )); }
private void DoTestAddInsertRemoveOnEmptyList(SeparatedSyntaxList <SyntaxNode> list) { Assert.Equal(0, list.Count); SyntaxNode nodeD = SyntaxFactory.ParseExpression("D"); SyntaxNode nodeE = SyntaxFactory.ParseExpression("E"); var newList = list.Add(nodeD); Assert.Equal(1, newList.Count); Assert.Equal("D", newList.ToFullString()); newList = list.AddRange(new[] { nodeD, nodeE }); Assert.Equal(2, newList.Count); Assert.Equal("D,E", newList.ToFullString()); newList = list.Insert(0, nodeD); Assert.Equal(1, newList.Count); Assert.Equal("D", newList.ToFullString()); newList = list.InsertRange(0, new[] { nodeD, nodeE }); Assert.Equal(2, newList.Count); Assert.Equal("D,E", newList.ToFullString()); newList = list.Remove(nodeD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(nodeD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(1, nodeD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(-1, nodeD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.InsertRange(1, new[] { nodeD })); Assert.Throws <ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { nodeD })); Assert.Throws <ArgumentNullException>(() => list.Add(null)); Assert.Throws <ArgumentNullException>( () => list.AddRange((IEnumerable <SyntaxNode>)null) ); Assert.Throws <ArgumentNullException>(() => list.Insert(0, null)); Assert.Throws <ArgumentNullException>( () => list.InsertRange(0, (IEnumerable <SyntaxNode>)null) ); }
public override SeparatedSyntaxList <TNode> VisitList <TNode>(SeparatedSyntaxList <TNode> list) { if (_originalNode is TNode node) { var index = list.IndexOf(node); if (index >= 0 && index < list.Count) { switch (editKind) { case ListEditKind.Replace: return(list.ReplaceRange(node, _newNodes.Cast <TNode>())); case ListEditKind.InsertAfter: return(list.InsertRange(index + 1, _newNodes.Cast <TNode>())); case ListEditKind.InsertBefore: return(list.InsertRange(index, _newNodes.Cast <TNode>())); } } } return(base.VisitList(list)); }
private static SeparatedSyntaxList <ParameterSyntax> RemoveDefaultValuesFromComplexTypeParameters(SeparatedSyntaxList <ParameterSyntax> parameters) { // asp.net web api does not support controller method parameters that are: // 1) reference types with default values set to 'null' // 2) enums with any default values // therefore remove all default values from complex type parameters // also the auto gen client files will include the default values so ok to remove them for enums // otherwise perhaps need to auto gen enum as a string (and parse it) or as a nullable type bool IsComplexTypeWithDefaultValue(ParameterSyntax parameter) { var param = new SeparatedSyntaxList <ParameterSyntax>().Add(parameter); var isComplexType = ComplexTypesGenerator.FindComplexTypes(param).Any(); var hasDefaultValue = parameter.Default != null; return(isComplexType && hasDefaultValue); } // split up complex type parameters into two lists: 1) has default values 2) all others var parametersGroupByDefaults = parameters.ToLookup(IsComplexTypeWithDefaultValue); var defaultValParameters = parametersGroupByDefaults[true].ToList(); var otherParameters = parametersGroupByDefaults[false]; if (!defaultValParameters.Any()) { return(parameters); } // remove default values var removedDefaultParameters = defaultValParameters .Select(parameter => { parameter = IsComplexTypeWithDefaultValue(parameter) ? parameter.WithDefault(null) : parameter; return(parameter); }); // combine modified parameters no longer with default values back with other params var newParameters = new SeparatedSyntaxList <ParameterSyntax>(); newParameters = newParameters.AddRange(otherParameters); var insertIndex = ParametersGenerator.FindIndexBeforeFirstOptionalParam(newParameters); newParameters = newParameters.InsertRange(insertIndex, removedDefaultParameters); return(newParameters); }
private void DoTestAddInsertRemoveOnEmptyList(SeparatedSyntaxList<SyntaxNode> list) { Assert.Equal(0, list.Count); SyntaxNode nodeD = SyntaxFactory.ParseExpression("D"); SyntaxNode nodeE = SyntaxFactory.ParseExpression("E"); var newList = list.Add(nodeD); Assert.Equal(1, newList.Count); Assert.Equal("D", newList.ToFullString()); newList = list.AddRange(new[] { nodeD, nodeE }); Assert.Equal(2, newList.Count); Assert.Equal("D,E", newList.ToFullString()); newList = list.Insert(0, nodeD); Assert.Equal(1, newList.Count); Assert.Equal("D", newList.ToFullString()); newList = list.InsertRange(0, new[] { nodeD, nodeE }); Assert.Equal(2, newList.Count); Assert.Equal("D,E", newList.ToFullString()); newList = list.Remove(nodeD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(nodeD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(1, nodeD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, nodeD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(1, new[] { nodeD })); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { nodeD })); Assert.Throws<ArgumentNullException>(() => list.Add(null)); Assert.Throws<ArgumentNullException>(() => list.AddRange((IEnumerable<SyntaxNode>)null)); Assert.Throws<ArgumentNullException>(() => list.Insert(0, null)); Assert.Throws<ArgumentNullException>(() => list.InsertRange(0, (IEnumerable<SyntaxNode>)null)); }
public override SeparatedSyntaxListWrapper <TNode> InsertRange(int index, IEnumerable <TNode> nodes) => new AutoWrapSeparatedSyntaxList <TSyntax>(syntaxList.InsertRange(index, nodes.Select(node => (TSyntax)SyntaxWrapper.Unwrap(node))));