private ImmutableArray <Edit> GetUnwrapAllEdits(WrappingStyle wrappingStyle) { var result = ArrayBuilder <Edit> .GetInstance(); AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result); foreach (var comma in _listItems.GetSeparators()) { result.Add(Edit.DeleteBetween(comma.GetPreviousToken(), comma)); result.Add(Edit.DeleteBetween(comma, comma.GetNextToken())); } result.Add(Edit.DeleteBetween(_listItems.Last(), _listSyntax.GetLastToken())); return(result.ToImmutableAndFree()); }
public void SeparatedSyntaxList_TwoItems_WithTrailingComma() { var expression1 = SyntaxFacts.ParseExpression("test1"); var comma1 = SyntaxFacts.ParseToken(","); var expression2 = SyntaxFacts.ParseExpression("test2"); var comma2 = SyntaxFacts.ParseToken(","); var nodeOrTokens = new SyntaxNodeOrToken[] { expression1, comma1, expression2, comma2 }; var list = new SeparatedSyntaxList <NameExpressionSyntax>(nodeOrTokens); Assert.Equal(2, list.Count); Assert.Equal(new[] { expression1, expression2 }, list); Assert.Equal(new[] { comma1, comma2 }, list.GetSeparators()); Assert.Equal(comma1, list.GetSeparator(0)); Assert.Equal(comma2, list.GetSeparator(1)); }
public static string ConvertSeparatedSyntaxList <T>( this SeparatedSyntaxList <T> source, Func <T, string> itemConverter = null, string separatorForced = null ) where T : SyntaxNode { var separator = separatorForced ?? BuilderStatic.SyntaxTokenConvert(source.GetSeparators().FirstOrDefault()); if (itemConverter == null) { itemConverter = BuilderStatic.SyntaxNode; } return(string.Join(separator, source.Select(itemConverter))); }
public static void Analyze(SyntaxNodeAnalysisContext context, InitializerExpressionSyntax initializer) { SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions; if (expressions.Any() && expressions.Count == expressions.SeparatorCount) { SyntaxToken token = expressions.GetSeparators().Last(); if (!token.IsMissing) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveRedundantCommaInInitializer, token); } } }
public override IEnumerable <SyntaxToken> GetSeparators() => SyntaxList.GetSeparators();
private static SeparatedSyntaxList <ArgumentSyntax> ConvertInitializers(SeparatedSyntaxList <AnonymousObjectMemberDeclaratorSyntax> initializers) => SyntaxFactory.SeparatedList(initializers.Select(ConvertInitializer), initializers.GetSeparators());
private static Task <Document> ConvertSwitchExpressionToSwitchStatement( Document document, SwitchExpressionSyntax switchExpression, CancellationToken cancellationToken) { SeparatedSyntaxList <SwitchExpressionArmSyntax> arms = switchExpression.Arms; SyntaxToken[] separators = arms.GetSeparators().ToArray(); IEnumerable <SwitchSectionSyntax> sections = arms.Select((arm, i) => { PatternSyntax pattern = arm.Pattern; ExpressionSyntax expression = arm.Expression; SyntaxToken semicolon = Token(SyntaxKind.SemicolonToken); SyntaxToken separator = default; if (i < separators.Length) { separator = separators[i]; } if (separator.IsKind(SyntaxKind.None) || separator.IsMissing) { semicolon = semicolon.WithTrailingTrivia(arm.GetTrailingTrivia()); expression = expression.WithoutTrailingTrivia(); } else { semicolon = semicolon.WithTriviaFrom(separator); } SyntaxKind kind = pattern.Kind(); SwitchLabelSyntax label = default; if (kind == SyntaxKind.ConstantPattern) { label = CaseSwitchLabel( Token(SyntaxKind.CaseKeyword).WithLeadingTrivia(pattern.GetLeadingTrivia()), ((ConstantPatternSyntax)pattern).Expression.WithoutLeadingTrivia(), Token(SyntaxKind.ColonToken).WithTriviaFrom(arm.EqualsGreaterThanToken)); } else if (kind == SyntaxKind.DiscardPattern) { label = DefaultSwitchLabel(Token(SyntaxKind.DefaultKeyword), Token(SyntaxKind.ColonToken)); } else { throw new InvalidOperationException(); } StatementSyntax statement = CreateStatement(expression, semicolon); return(SwitchSection(label, statement)); }); var returnStatement = (ReturnStatementSyntax)switchExpression.Parent; SwitchStatementSyntax switchStatement = SwitchStatement( switchExpression.SwitchKeyword.WithTriviaFrom(returnStatement.ReturnKeyword), OpenParenToken(), switchExpression.GoverningExpression, CloseParenToken().WithTrailingTrivia(switchExpression.SwitchKeyword.TrailingTrivia), switchExpression.OpenBraceToken, sections.ToSyntaxList(), switchExpression.CloseBraceToken); switchStatement = switchStatement.WithFormatterAnnotation(); return(document.ReplaceNodeAsync(returnStatement, switchStatement, cancellationToken));