private static SyntaxTokenList InsertImpl(SyntaxTokenList modifiers, SyntaxToken modifier, int index) { if (index == 0) { SyntaxToken firstModifier = modifiers[index]; SyntaxTriviaList trivia = firstModifier.LeadingTrivia; if (trivia.Any()) { SyntaxTriviaList leadingTrivia = modifier.LeadingTrivia; if (!leadingTrivia.IsSingleElasticMarker()) { trivia = trivia.AddRange(leadingTrivia); } modifier = modifier.WithLeadingTrivia(trivia); modifiers = modifiers.ReplaceAt(index, firstModifier.WithoutLeadingTrivia()); } } return(modifiers.Insert(index, modifier)); }
public TNode InsertModifier(TNode node, SyntaxToken modifier, IModifierComparer comparer = null) { if (node == null) { throw new ArgumentNullException(nameof(node)); } SyntaxTokenList modifiers = GetModifiers(node); Debug.Assert(modifiers.Any() || modifiers == default(SyntaxTokenList), node.ToString()); if (!modifiers.Any()) { SyntaxNodeOrToken nodeOrToken = FindNodeOrTokenAfterModifiers(node); if (!nodeOrToken.IsKind(SyntaxKind.None)) { SyntaxTriviaList trivia = nodeOrToken.GetLeadingTrivia(); if (trivia.Any()) { SyntaxTriviaList leadingTrivia = modifier.LeadingTrivia; if (!leadingTrivia.IsSingleElasticMarker()) { trivia = trivia.AddRange(leadingTrivia); } if (nodeOrToken.IsNode) { SyntaxNode node2 = nodeOrToken.AsNode(); node = node.ReplaceNode(node2, node2.WithoutLeadingTrivia()); } else { SyntaxToken token = nodeOrToken.AsToken(); node = node.ReplaceToken(token, token.WithoutLeadingTrivia()); } return(WithModifiers(node, TokenList(modifier.WithLeadingTrivia(trivia)))); } } } return(WithModifiers(node, modifiers.InsertModifier(modifier, comparer ?? ModifierComparer.Instance))); }
private TNode InsertModifier(TNode node, SyntaxTokenList modifiers, SyntaxToken modifier, int index) { SyntaxToken token; if (!modifiers.Any() || index == modifiers.Count) { if (modifiers.Any()) { token = modifiers.Last().GetNextToken(); } else { AttributeListSyntax attributeList = GetAttributeLists(node).LastOrDefault(); if (attributeList != null) { token = attributeList.GetLastToken().GetNextToken(); } else { token = node.GetFirstToken(); } } } else { token = modifiers[index]; } if (token != default(SyntaxToken)) { SyntaxTriviaList newLeadingTrivia = token.LeadingTrivia; if (newLeadingTrivia.Any()) { SyntaxTriviaList leadingTrivia = modifier.LeadingTrivia; if (!leadingTrivia.IsSingleElasticMarker()) { newLeadingTrivia = newLeadingTrivia.AddRange(leadingTrivia); } modifier = modifier.WithLeadingTrivia(newLeadingTrivia); SyntaxToken newToken = token.WithoutLeadingTrivia(); if (!modifiers.Any() || index == modifiers.Count) { node = node.ReplaceToken(token, newToken); } else { modifiers = modifiers.ReplaceAt(index, newToken); } } if (modifier.TrailingTrivia.IsSingleElasticMarker()) { modifier = modifier.WithTrailingTrivia(TriviaList(Space)); } } modifiers = modifiers.Insert(index, modifier); return(WithModifiers(node, modifiers)); }
public TNode InsertModifier(TNode node, SyntaxToken modifier, IModifierComparer comparer = null) { if (node == null) { throw new ArgumentNullException(nameof(node)); } SyntaxTokenList modifiers = GetModifiers(node); int insertIndex = (comparer ?? ModifierComparer.Instance).GetInsertIndex(modifiers, modifier); var token = default(SyntaxToken); if (!modifiers.Any() || insertIndex == modifiers.Count) { SyntaxNodeOrToken nodeOrToken = FindNodeOrTokenAfterModifiers(node); if (nodeOrToken.IsToken) { token = nodeOrToken.AsToken(); } else if (nodeOrToken.IsNode) { token = nodeOrToken.AsNode().GetFirstToken(); } } else { token = modifiers[insertIndex]; } if (token != default(SyntaxToken)) { SyntaxTriviaList newLeadingTrivia = token.LeadingTrivia; if (newLeadingTrivia.Any()) { SyntaxTriviaList leadingTrivia = modifier.LeadingTrivia; if (!leadingTrivia.IsSingleElasticMarker()) { newLeadingTrivia = newLeadingTrivia.AddRange(leadingTrivia); } modifier = modifier.WithLeadingTrivia(newLeadingTrivia); SyntaxToken newToken = token.WithoutLeadingTrivia(); if (!modifiers.Any() || insertIndex == modifiers.Count) { node = node.ReplaceToken(token, newToken); } else { modifiers = modifiers.ReplaceAt(insertIndex, newToken); } } if (modifier.TrailingTrivia.IsSingleElasticMarker()) { modifier = modifier.WithTrailingTrivia(TriviaList(Space)); } } modifiers = modifiers.Insert(insertIndex, modifier); return(WithModifiers(node, modifiers)); }