コード例 #1
0
        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));
        }