コード例 #1
0
        public static async Task <Document> RemoveDirectivesAsync(
            this Document document,
            DirectiveRemoveOptions removeOptions,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            SourceText newSourceText = RemoveDirectives(sourceText, GetDirectives(root, removeOptions));

            return(document.WithText(newSourceText));
        }
コード例 #2
0
        private static IEnumerable <DirectiveTriviaSyntax> GetDirectives(SyntaxNode root, DirectiveRemoveOptions removeOptions)
        {
            switch (removeOptions)
            {
            case DirectiveRemoveOptions.All:
            {
                return(root.DescendantDirectives());
            }

            case DirectiveRemoveOptions.AllExceptRegion:
            {
                return(root
                       .DescendantDirectives()
                       .Where(f => !f.IsKind(SyntaxKind.RegionDirectiveTrivia, SyntaxKind.EndRegionDirectiveTrivia)));
            }

            case DirectiveRemoveOptions.Region:
            {
                return(root.DescendantRegionDirectives());
            }

            default:
            {
                throw new ArgumentException("", nameof(removeOptions));
            }
            }
        }