public override SyntaxNode VisitParameterList(ParameterListSyntax node) { ParameterListSyntax originalNode = node; //node = (ParameterListSyntax)base.VisitParameterList(node); can't - see below int i = 0; foreach (ParameterSyntax parameter in originalNode.Parameters) { if (RemoveParameter(parameter)) { node = node.Update( node.OpenParenToken, node.Parameters.RemoveAt(i), node.CloseParenToken); continue; } // Can't use base.VisitParameterList(node) to inner-process all parameters because some that are destined // to be stripped may cause processing errors (e.g. because they contain a generic type reference to a type // parameter that is being eliminated). Therefore, do the work ourselves using base.VisitParameter(parameter) // on each. bool hasTrailingSeparator = i < node.Parameters.Count - 1; bool hasLeadingSeparator = i > 0; SyntaxToken originalSeparator = hasTrailingSeparator ? node.Parameters.GetSeparator(i) : (hasLeadingSeparator ? node.Parameters.GetSeparator(i - 1) : SyntaxFactory.MissingToken(SyntaxKind.CommaToken)); SeparatedSyntaxList <ParameterSyntax> parameters = node.Parameters .RemoveAt(i) .Insert(i, (ParameterSyntax)base.VisitParameter(parameter).WithTriviaFrom(node.Parameters[i])); if (hasTrailingSeparator) { parameters = parameters.ReplaceSeparator(parameters.GetSeparator(i), originalSeparator); } else if (hasLeadingSeparator) { parameters = parameters.ReplaceSeparator(parameters.GetSeparator(i - 1), originalSeparator); } node = node.Update( node.OpenParenToken, parameters, node.CloseParenToken); i++; } return(node); }
private TypeArgumentListSyntax ParameterToArgumentListSyntax(TypeParameterListSyntax typeParameters) { var list = new SeparatedSyntaxList <TypeSyntax>(); list = list.AddRange(typeParameters.Parameters.Select(p => SyntaxFactory.ParseName(p.ToString()).WithTriviaFrom(p))); for (int i = 0; i < list.SeparatorCount; i++) { var separator = list.GetSeparator(i); // Make sure the parameter list looks nice list = list.ReplaceSeparator(separator, separator.WithTrailingTrivia(SyntaxFactory.Whitespace(" "))); } return(SyntaxFactory.TypeArgumentList(list)); }
private static async Task <Document> SortEnumMembersAsync( Document document, EnumDeclarationSyntax enumDeclaration, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration).EnumUnderlyingType.SpecialType; SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members; SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members .OrderBy(f => GetConstantValue(f, semanticModel, cancellationToken), EnumValueComparer.GetInstance(enumSpecialType)) .ToSeparatedSyntaxList(); if (AreSeparatedWithEmptyLine(members)) { for (int i = 0; i < newMembers.Count; i++) { newMembers = newMembers.ReplaceAt(i, newMembers[i].TrimLeadingTrivia()); } for (int i = 0; i < newMembers.Count - 1; i++) { SyntaxToken separator = newMembers.GetSeparator(i); newMembers = newMembers.ReplaceSeparator( separator, separator.TrimTrailingTrivia().AppendToTrailingTrivia(new SyntaxTrivia[] { NewLine(), NewLine() })); } } if (newMembers.SeparatorCount == members.SeparatorCount - 1) { SyntaxNodeOrTokenList newMembersWithSeparators = newMembers.GetWithSeparators(); newMembersWithSeparators = newMembersWithSeparators.Add(CommaToken()); newMembers = newMembersWithSeparators.ToSeparatedSyntaxList <EnumMemberDeclarationSyntax>(); } MemberDeclarationSyntax newNode = enumDeclaration .WithMembers(newMembers) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false)); }
private static InitializerExpressionSyntax RemoveTrailingComma(InitializerExpressionSyntax initializer) { SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; SyntaxToken trailingComma = expressions.GetTrailingSeparator(); SeparatedSyntaxList <ExpressionSyntax> newExpressions = expressions.ReplaceSeparator( trailingComma, SyntaxFactory.MissingToken(SyntaxKind.CommaToken)); int lastIndex = expressions.Count - 1; SyntaxTriviaList newTrailingTrivia = expressions[lastIndex] .GetTrailingTrivia() .AddRange(trailingComma.LeadingTrivia) .AddRange(trailingComma.TrailingTrivia); ExpressionSyntax newExpression = newExpressions[lastIndex].WithTrailingTrivia(newTrailingTrivia); newExpressions = newExpressions.ReplaceAt(lastIndex, newExpression); return(initializer.WithExpressions(newExpressions)); }
private static Task <Document> FormatInitializerOnSingleLineAsync( Document document, InitializerExpressionSyntax initializer, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); SyntaxToken trailingComma = initializer.Expressions.GetTrailingSeparator(); if (trailingComma == default) { return(SyntaxFormatter.ToSingleLineAsync(document, initializer, cancellationToken)); } SyntaxNode parent = initializer.Parent; initializer = initializer .ReplaceWhitespace(SyntaxFactory.ElasticMarker, TextSpan.FromBounds(initializer.FullSpan.Start, initializer.Span.End)) .WithFormatterAnnotation(); SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; expressions = expressions.ReplaceAt(0, expressions.First().WithTrailingTrivia(SyntaxFactory.Space)); expressions = expressions.ReplaceSeparator(expressions.GetSeparator(0), SyntaxFactory.MissingToken(SyntaxKind.CommaToken)); initializer = initializer.WithExpressions(expressions); SyntaxNode newParent = GetNewParent(); return(document.ReplaceNodeAsync(parent, newParent, cancellationToken)); SyntaxNode GetNewParent() { switch (parent) { case ObjectCreationExpressionSyntax objectCreation: { objectCreation = objectCreation.WithInitializer(initializer); ArgumentListSyntax argumentList = objectCreation.ArgumentList; if (argumentList != null) { return(objectCreation.WithArgumentList(argumentList.WithoutTrailingTrivia())); } else { return(objectCreation.WithType(objectCreation.Type.WithoutTrailingTrivia())); } } case ArrayCreationExpressionSyntax arrayCreation: { return(arrayCreation .WithInitializer(initializer) .WithType(arrayCreation.Type.WithoutTrailingTrivia())); } case ImplicitArrayCreationExpressionSyntax implicitArrayCreation: { return(implicitArrayCreation .WithInitializer(initializer) .WithCloseBracketToken(implicitArrayCreation.CloseBracketToken.WithoutTrailingTrivia())); } } throw new InvalidOperationException(); } }
private static TypeArgumentListSyntax ParameterToArgumentListSyntax(TypeParameterListSyntax typeParameters) { var list = new SeparatedSyntaxList<TypeSyntax>(); list = list.AddRange(typeParameters.Parameters.Select(p => SyntaxFactory.ParseName(p.ToString()).WithTriviaFrom(p))); for (int i = 0; i < list.SeparatorCount; i++) { // Make sure the parameter list looks nice var separator = list.GetSeparator(i); list = list.ReplaceSeparator(separator, separator.WithTrailingTrivia(SyntaxFactory.Space)); } return SyntaxFactory.TypeArgumentList(list); }
public static Task <Document> ToSingleLineAsync( Document document, InitializerExpressionSyntax initializer, bool removeTrailingComma = false, CancellationToken cancellationToken = default(CancellationToken)) { InitializerExpressionSyntax newInitializer = initializer .ReplaceWhitespace(ElasticSpace, TextSpan.FromBounds(initializer.Span.Start, initializer.Span.End)) .WithFormatterAnnotation(); newInitializer = newInitializer.WithOpenBraceToken(newInitializer.OpenBraceToken.WithoutLeadingTrivia().WithTrailingTrivia(Space)); newInitializer = newInitializer.WithCloseBraceToken(newInitializer.CloseBraceToken.WithoutLeadingTrivia()); SeparatedSyntaxList <ExpressionSyntax> expressions = newInitializer.Expressions; if (expressions.Any()) { ExpressionSyntax firstExpression = expressions.First(); newInitializer = newInitializer.WithExpressions(expressions.Replace(firstExpression, firstExpression.WithoutLeadingTrivia())); expressions = newInitializer.Expressions; SyntaxToken trailingComma = expressions.GetTrailingSeparator(); if (trailingComma.IsKind(SyntaxKind.CommaToken)) { if (removeTrailingComma) { expressions = expressions.ReplaceSeparator(trailingComma, MissingToken(SyntaxKind.CommaToken)); ExpressionSyntax lastExpression = expressions.Last(); expressions = expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space)); newInitializer = newInitializer.WithExpressions(expressions); } else { newInitializer = newInitializer.WithExpressions(expressions.ReplaceSeparator(trailingComma, trailingComma.WithTrailingTrivia(Space))); } } else { ExpressionSyntax lastExpression = expressions.Last(); newInitializer = newInitializer.WithExpressions(expressions.Replace(lastExpression, lastExpression.WithTrailingTrivia(Space))); } } SyntaxNode parent = initializer.Parent; SyntaxNode newParent; switch (parent.Kind()) { case SyntaxKind.ObjectCreationExpression: { var expression = (ObjectCreationExpressionSyntax)parent; expression = expression.WithInitializer(newInitializer); ArgumentListSyntax argumentList = expression.ArgumentList; if (argumentList != null) { newParent = expression.WithArgumentList(argumentList.WithTrailingTrivia(Space)); } else { newParent = expression.WithType(expression.Type.WithTrailingTrivia(Space)); } break; } case SyntaxKind.ArrayCreationExpression: { var expression = (ArrayCreationExpressionSyntax)parent; newParent = expression .WithInitializer(newInitializer) .WithType(expression.Type.WithTrailingTrivia(Space)); break; } case SyntaxKind.ImplicitArrayCreationExpression: { var expression = (ImplicitArrayCreationExpressionSyntax)parent; newParent = expression .WithInitializer(newInitializer) .WithCloseBracketToken(expression.CloseBracketToken.WithTrailingTrivia(Space)); break; } case SyntaxKind.EqualsValueClause: { var equalsValueClause = (EqualsValueClauseSyntax)parent; newParent = equalsValueClause .WithValue(newInitializer) .WithEqualsToken(equalsValueClause.EqualsToken.WithTrailingTrivia(Space)); break; } case SyntaxKind.SimpleAssignmentExpression: { var simpleAssignment = (AssignmentExpressionSyntax)parent; newParent = simpleAssignment .WithRight(newInitializer) .WithOperatorToken(simpleAssignment.OperatorToken.WithTrailingTrivia(Space)); break; } default: { Debug.Fail(parent.Kind().ToString()); return(document.ReplaceNodeAsync(initializer, newInitializer, cancellationToken)); } } return(document.ReplaceNodeAsync(parent, newParent, cancellationToken)); }
public override SeparatedSyntaxListWrapper <TNode> ReplaceSeparator(SyntaxToken separatorToken, SyntaxToken newSeparator) => new AutoWrapSeparatedSyntaxList <TSyntax>(syntaxList.ReplaceSeparator(separatorToken, newSeparator));