Exemple #1
0
        private async Task RemoveEmptyUsingStatementsAsync(
            )
        {
            foreach (var documentFilePath in _workspace.EnumerateAllDocumentFilePaths(Predicate.IsProjectInScope, Predicate.IsDocumentInScope))
            {
                if (documentFilePath == null)
                {
                    continue;
                }

                bool r = true;
                do
                {
                    var document = _workspace.GetDocument(documentFilePath);
                    if (document == null)
                    {
                        continue;
                    }

                    var syntaxRoot = await document.GetSyntaxRootAsync();

                    if (syntaxRoot == null)
                    {
                        continue;
                    }

                    var namespaces = syntaxRoot
                                     .DescendantNodes()
                                     .OfType <UsingDirectiveSyntax>()
                                     .ToList()
                    ;

                    if (namespaces.Count == 0)
                    {
                        continue;
                    }

                    var toRemove = new List <SyntaxNode>();

                    foreach (var n in namespaces)
                    {
                        var nname = n.Name.ToString();

                        if (!_namespaceCenter.NamespacesToRemove.Contains(nname))
                        {
                            //there is a types in this namespace
                            continue;
                        }

                        toRemove.Add(n);
                    }

                    if (toRemove.Count > 0)
                    {
                        syntaxRoot = syntaxRoot !.RemoveNodes(toRemove, SyntaxRemoveOptions.KeepNoTrivia);
                        if (syntaxRoot != null)
                        {
                            var changedDocument = document.WithSyntaxRoot(syntaxRoot);

                            r = _workspace.TryApplyChanges(changedDocument.Project.Solution);
                        }
                    }
                }while (!r);
            }
        }