Esempio n. 1
0
 public static Task <Document> RefactorAsync(
     Document document,
     UsingDirectiveSyntax usingDirective,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(InlineAliasExpressionSyntaxRewriter.VisitAsync(document, usingDirective, cancellationToken));
 }
Esempio n. 2
0
            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 InlineAliasExpressionSyntaxRewriter(usingDirective, identifierNames.ToArray());

                SyntaxNode newRoot = rewriter.Visit(root);

                usingDirective = (UsingDirectiveSyntax)newRoot.FindNode(usingDirective.Span);

                newRoot = newRoot.RemoveNode(usingDirective, GetRemoveOptions(usingDirective));

                return(document.WithSyntaxRoot(newRoot));
            }