private static async Task <Document> AddBracesAsync(CodeFixContext context, CancellationToken cancellationToken)
        {
            var diagnostic = context.Diagnostics.First();
            var root       = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var @switch  = (SwitchStatementSyntax)root.FindNode(diagnostic.Location.SourceSpan);
            var sections = new List <SwitchSectionSyntax>();

            foreach (var section in @switch.Sections)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (!AddBracesToSwitchSectionsAnalyzer.HasBraces(section))
                {
                    var newSection = AddBraces(section);
                    sections.Add(newSection);
                }
                else
                {
                    sections.Add(section);
                }
            }
            var newSwitch   = @switch.WithSections(SyntaxFactory.List(sections)).WithAdditionalAnnotations(Formatter.Annotation);
            var newRoot     = root.ReplaceNode(@switch, newSwitch);
            var newDocument = context.Document.WithSyntaxRoot(newRoot);

            return(newDocument);
        }
示例#2
0
        private static Task <Document> AddBracesAsync(Document document, SyntaxNode root, SwitchStatementSyntax @switch)
        {
            var sections = new List <SwitchSectionSyntax>();

            foreach (var section in @switch.Sections)
            {
                if (!AddBracesToSwitchSectionsAnalyzer.HasBraces(section))
                {
                    var newSection = AddBraces(section);
                    sections.Add(newSection);
                }
                else
                {
                    sections.Add(section);
                }
            }
            var newSwitch   = @switch.WithSections(SyntaxFactory.List(sections)).WithAdditionalAnnotations(Formatter.Annotation);
            var newRoot     = root.ReplaceNode(@switch, newSwitch);
            var newDocument = document.WithSyntaxRoot(newRoot);

            return(Task.FromResult(newDocument));
        }