private static async Task <Document> MoveDefaultLabelToLastPositionAsync( Document document, SwitchSectionSyntax switchSection, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels; SwitchLabelSyntax defaultLabel = labels.First(f => f.IsKind(SyntaxKind.DefaultSwitchLabel)); int index = labels.IndexOf(defaultLabel); SwitchLabelSyntax lastLabel = labels.Last(); labels = labels.Replace(lastLabel, defaultLabel.WithTriviaFrom(lastLabel)); labels = labels.Replace(labels[index], lastLabel.WithTriviaFrom(defaultLabel)); SwitchSectionSyntax newSwitchSection = switchSection.WithLabels(labels); SyntaxNode newRoot = root.ReplaceNode(switchSection, newSwitchSection); return(document.WithSyntaxRoot(newRoot)); }
SwitchStatementSyntax AddCasesToDefaultSection(SwitchSectionSyntax defaultSection) { return (SwitchStatement.ReplaceNode( defaultSection, defaultSection.WithLabels( SyntaxFactory.List( MissingEnumMembers.Select(CreateCaseSwitchLabel) .Concat(defaultSection.Labels) )))); }
public static SwitchSectionSyntax MoveDefaultlabelToLastInSwitchSection(this SwitchSectionSyntax switchSection) { var labels = switchSection.Labels; var defaultLabel = labels.GetDefaultSwitchLabel(); if (defaultLabel == null) { return(switchSection); } labels = labels.Remove(defaultLabel); labels = labels.Add(defaultLabel); var newDefaultSwitchSection = switchSection.WithLabels(labels); return(newDefaultSwitchSection); }
public static async Task <Document> RefactorAsync( Document document, SwitchSectionSyntax switchSection, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels; SwitchLabelSyntax defaultLabel = labels.First(f => f.IsKind(SyntaxKind.DefaultSwitchLabel)); int index = labels.IndexOf(defaultLabel); SwitchLabelSyntax lastLabel = labels.Last(); labels = labels.Replace(lastLabel, defaultLabel.WithTriviaFrom(lastLabel)); labels = labels.Replace(labels[index], lastLabel.WithTriviaFrom(defaultLabel)); SwitchSectionSyntax newSwitchSection = switchSection.WithLabels(labels); return(await document.ReplaceNodeAsync(switchSection, newSwitchSection, cancellationToken).ConfigureAwait(false)); }
private static Task <Document> CopySwitchSectionAsync( Document document, SwitchSectionSyntax switchSection, bool insertNewLine, CancellationToken cancellationToken) { var switchStatement = (SwitchStatementSyntax)switchSection.Parent; SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections; int index = sections.IndexOf(switchSection); SwitchLabelSyntax label = switchSection.Labels[0]; SyntaxToken firstToken = label.GetFirstToken(); SyntaxToken newFirstToken = firstToken.WithNavigationAnnotation(); if (insertNewLine && !firstToken.LeadingTrivia.Contains(SyntaxKind.EndOfLineTrivia)) { newFirstToken = newFirstToken.PrependToLeadingTrivia(CSharpFactory.NewLine()); } SwitchSectionSyntax newSection = switchSection .WithLabels(switchSection.Labels.ReplaceAt(0, label.ReplaceToken(firstToken, newFirstToken))) .WithFormatterAnnotation(); if (index == sections.Count - 1) { newSection = newSection.TrimTrailingTrivia(); } SyntaxList <SwitchSectionSyntax> newSections = sections.Insert(index + 1, newSection); SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections); return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken)); }
private static Task <Document> UsePatternMatchingAsync( Document document, SwitchStatementSyntax switchStatement, CancellationToken cancellationToken) { SyntaxList <SwitchSectionSyntax> newSections = switchStatement.Sections.Select(section => { if (!(section.Labels.Single() is CaseSwitchLabelSyntax label)) { return(section); } SyntaxList <StatementSyntax> statements = section.Statements; StatementSyntax statement = statements[0]; if (statement is BlockSyntax block) { statement = block.Statements.FirstOrDefault(); } SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo((LocalDeclarationStatementSyntax)statement); var castExpression = (CastExpressionSyntax)localInfo.Value; CasePatternSwitchLabelSyntax newLabel = CasePatternSwitchLabel( DeclarationPattern( castExpression.Type, SingleVariableDesignation(localInfo.Identifier)), label.ColonToken); SwitchSectionSyntax newSection = section.RemoveStatement(localInfo.Statement); newSection = newSection.WithLabels(newSection.Labels.ReplaceAt(0, newLabel)); return(newSection.WithFormatterAnnotation()); }) .ToSyntaxList(); ExpressionSyntax expression = switchStatement.Expression; ExpressionSyntax newExpression = expression; LocalDeclarationStatementSyntax localDeclaration = null; if (expression.IsKind(SyntaxKind.InvocationExpression)) { SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(expression); newExpression = invocationInfo.Expression; } else { localDeclaration = (LocalDeclarationStatementSyntax)switchStatement.PreviousStatement(); SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(localDeclaration); SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(localInfo.Value); newExpression = invocationInfo.Expression; } SwitchStatementSyntax newSwitchStatement = switchStatement .WithExpression(newExpression.WithTriviaFrom(expression)) .WithSections(newSections); if (localDeclaration != null) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(switchStatement); newSwitchStatement = newSwitchStatement.WithLeadingTrivia(localDeclaration.GetLeadingTrivia()); SyntaxList <StatementSyntax> newStatements = statementsInfo.Statements .Replace(switchStatement, newSwitchStatement) .RemoveAt(statementsInfo.IndexOf(localDeclaration)); return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); } else { return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken)); } }