public static async Task <Document> RefactorAsync( Document document, UsingDirectiveSyntax usingDirective, CancellationToken cancellationToken = default(CancellationToken)) { return(await RemoveUsingAliasDirectiveSyntaxRewriter.VisitAsync(document, usingDirective, cancellationToken).ConfigureAwait(false)); }
public static async Task <Document> VisitAsync( Document document, UsingDirectiveSyntax usingDirective, CancellationToken cancellationToken = default(CancellationToken)) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (usingDirective == null) { throw new ArgumentNullException(nameof(usingDirective)); } SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxTree tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var aliasSymbol = semanticModel.GetDeclaredSymbol(usingDirective, cancellationToken) as IAliasSymbol; var identifierNames = new List <IdentifierNameSyntax>(); IEnumerable <ReferencedSymbol> referencedSymbols = await SymbolFinder.FindReferencesAsync(aliasSymbol, document.Project.Solution, cancellationToken).ConfigureAwait(false); foreach (TextSpan span in GetSymbolSpans(referencedSymbols, tree)) { IdentifierNameSyntax identifierName = root .FindNode(span, getInnermostNodeForTie: true) .FirstAncestorOrSelf <IdentifierNameSyntax>(); if (identifierName != null && aliasSymbol == semanticModel.GetAliasInfo(identifierName, cancellationToken)) { identifierNames.Add(identifierName); } } var rewriter = new RemoveUsingAliasDirectiveSyntaxRewriter(usingDirective, identifierNames.ToArray()); SyntaxNode newRoot = rewriter.Visit(root); usingDirective = (UsingDirectiveSyntax)newRoot.FindNode(usingDirective.Span); newRoot = newRoot.RemoveNode(usingDirective, SyntaxRemoveOptions.KeepUnbalancedDirectives); return(document.WithSyntaxRoot(newRoot)); }