public static async Task <Document> RefactorAsync(
            Document document,
            UsingDirectiveSyntax usingDirective,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var classSymbol = (INamedTypeSymbol)semanticModel.GetSymbol(usingDirective.Name, cancellationToken);

            SyntaxNode parent = usingDirective.Parent;

            Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration));

            int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective);

            List <SimpleNameSyntax> names = CollectNames(parent, classSymbol, semanticModel, cancellationToken);

            TypeSyntax type = classSymbol.ToTypeSyntax();

            SyntaxNode newNode = parent.ReplaceNodes(names, (node, _) =>
            {
                return(SimpleMemberAccessExpression(type, node.WithoutTrivia())
                       .WithTriviaFrom(node)
                       .WithSimplifierAnnotation());
            });

            newNode = RemoveUsingDirective(newNode, index);

            return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false));
        }
Beispiel #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            UsingDirectiveSyntax usingDirective,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            IAliasSymbol aliasSymbol = semanticModel.GetDeclaredSymbol(usingDirective, cancellationToken);

            SyntaxNode parent = usingDirective.Parent;

            Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), "");

            int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective);

            List <IdentifierNameSyntax> names = CollectNames(parent, aliasSymbol, semanticModel, cancellationToken);

            NameSyntax name = usingDirective.Name;

            ISymbol symbol = semanticModel.GetSymbol(name, cancellationToken);

            SyntaxNode newNode = parent.ReplaceNodes(names, (f, _) =>
            {
                if (symbol != null &&
                    semanticModel
                    .GetSpeculativeSymbolInfo(f.SpanStart, name, SpeculativeBindingOption.BindAsTypeOrNamespace)
                    .Symbol?
                    .Equals(symbol) == true)
                {
                    return(name.WithTriviaFrom(f));
                }
                else
                {
                    return(aliasSymbol.Target.ToMinimalTypeSyntax(semanticModel, f.SpanStart).WithTriviaFrom(f));
                }
            });

            newNode = RemoveUsingDirective(newNode, index);

            return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false));
        }
Beispiel #3
0
        public static async Task <Document> RefactorAsync(
            Document document,
            UsingDirectiveSyntax usingDirective,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            IAliasSymbol aliasSymbol = semanticModel.GetDeclaredSymbol(usingDirective, cancellationToken);

            SyntaxNode parent = usingDirective.Parent;

            Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), "");

            int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective);

            var rewriter = new Rewriter(aliasSymbol, aliasSymbol.Target.ToTypeSyntax(), semanticModel, cancellationToken);

            SyntaxNode newNode = rewriter.Visit(parent);

            newNode = RemoveUsingDirective(newNode, index);

            return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false));
        }