Beispiel #1
0
        /// <remarks>These are tidied up so we can add as many GlobalImports as we want when building compilations</remarks>
        private CSharpSyntaxNode TidyUsings(CSharpSyntaxNode root)
        {
            var diagnostics  = _semanticModel.GetDiagnostics().ToList();
            var unusedUsings = diagnostics
                               .Where(d => d.Id == UnusedUsingDiagnosticId)
                               .Select(d => root.FindNode(d.Location.SourceSpan))
                               .OfType <UsingDirectiveSyntax>()
                               .ToList();

            var nodesWithUnresolvedTypes = diagnostics
                                           .Where(d => d.Id == UnresolvedTypeOrNamespaceDiagnosticId && d.Location.IsInSource)
                                           .Select(d => root.FindNode(d.Location.SourceSpan))
                                           .ToLookup(d => d.GetAncestor <UsingDirectiveSyntax>());

            unusedUsings = unusedUsings.Except(nodesWithUnresolvedTypes.Select(g => g.Key)).ToList();

            if (nodesWithUnresolvedTypes[null].Any() || !unusedUsings.Any())
            {
                return(root);
            }

            root = root.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepNoTrivia);
            var firstChild = root.ChildNodes().FirstOrDefault();

            if (firstChild != null)
            {
                root = root.ReplaceNode(firstChild,
                                        firstChild.WithLeadingTrivia(firstChild.GetLeadingTrivia()
                                                                     .SkipWhile(t => t.IsWhitespaceTrivia())));
            }

            return(root);
        }
        /// <remarks>These are tidied up so we can add as many GlobalImports as we want when building compilations</remarks>
        private CSharpSyntaxNode TidyUsings(CSharpSyntaxNode root)
        {
            var diagnostics  = _semanticModel.GetDiagnostics().ToList();
            var unusedUsings = diagnostics
                               .Where(d => d.Id == UnusedUsingDiagnosticId)
                               .Select(d => root.FindNode(d.Location.SourceSpan))
                               .OfType <UsingDirectiveSyntax>()
                               .ToList();

            var nodesWithUnresolvedTypes = diagnostics
                                           .Where(d => d.Id == UnresolvedTypeOrNamespaceDiagnosticId && d.Location.IsInSource)
                                           .Select(d => root.FindNode(d.Location.SourceSpan))
                                           .ToLookup(d => d.GetAncestor <UsingDirectiveSyntax>());

            unusedUsings = unusedUsings.Except(nodesWithUnresolvedTypes.Select(g => g.Key)).ToList();

            if (nodesWithUnresolvedTypes[null].Any() || !unusedUsings.Any())
            {
                return(root);
            }

            return(root.RemoveNodes(unusedUsings, SyntaxRemoveOptions.KeepNoTrivia));
        }