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)); }