private static SyntaxNode ToMultiLine(SyntaxNode node, SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses) { TypeParameterConstraintClauseSyntax first = constraintClauses.First(); SyntaxToken previousToken = node.FindToken(first.FullSpan.Start - 1); node = node.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(TriviaList(NewLine()))); SyntaxTriviaList leadingTrivia = node .FindToken(node.SpanStart) .LeadingTrivia; SyntaxTriviaList trivia = IncreaseIndentation(leadingTrivia.LastOrDefault()); int count = constraintClauses.Count; for (int i = 0; i < count; i++) { TypeParameterConstraintClauseSyntax newNode = constraintClauses[i].WithLeadingTrivia(trivia); if (i < count - 1) { newNode = newNode.WithTrailingTrivia(NewLine()); } constraintClauses = constraintClauses.ReplaceAt(i, newNode); } return(WithConstraintClauses(node, constraintClauses)); }
private static GenericInfo ToMultiLine(GenericInfo info) { SyntaxNode declaration = info.Node; SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses = info.ConstraintClauses; TypeParameterConstraintClauseSyntax first = constraintClauses.First(); SyntaxToken previousToken = declaration.FindToken(first.FullSpan.Start - 1); declaration = declaration.ReplaceToken(previousToken, previousToken.WithTrailingTrivia(TriviaList(NewLine()))); SyntaxTriviaList leadingTrivia = declaration .FindToken(declaration.SpanStart) .LeadingTrivia; SyntaxTriviaList trivia = IncreaseIndentation(leadingTrivia.LastOrDefault()); int count = constraintClauses.Count; for (int i = 0; i < count; i++) { TypeParameterConstraintClauseSyntax newNode = constraintClauses[i].WithLeadingTrivia(trivia); if (i < count - 1) { newNode = newNode.WithTrailingTrivia(NewLine()); } constraintClauses = constraintClauses.ReplaceAt(i, newNode); } return(SyntaxInfo.GenericInfo(declaration).WithConstraintClauses(constraintClauses)); }
/// <summary> /// Returns a <see cref="SyntaxList{TNode}"/> of <see cref="TypeParameterConstraintClauseSyntax"/> build from the given <paramref name="constraints"/>. /// </summary> /// <param name="constraints">A collection of <see cref="TypeParameterConstraintClauseSyntax"/> to build the <see cref="SyntaxList{TNode}"/> from.</param> /// <param name="numOriginalConstraints">Number of type constraints in the original declaration.</param> public static SyntaxList <TypeParameterConstraintClauseSyntax> ApplyConstraints(IEnumerable <TypeParameterConstraintClauseSyntax> constraints, int numOriginalConstraints) { SyntaxList <TypeParameterConstraintClauseSyntax> clauses = SyntaxFactory.List(constraints); if (numOriginalConstraints > 0) { int count = clauses.Count; if (count > 0 && count < numOriginalConstraints) { TypeParameterConstraintClauseSyntax last = clauses.Last(); TypeParameterConstraintClauseSyntax newLast = last.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); clauses = clauses.Replace(last, newLast); } } return(clauses); }