Ejemplo n.º 1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax member,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.Assert(member.IsParentKind(SyntaxKind.CompilationUnit), member.Parent?.Kind().ToString());

            if (member.IsParentKind(SyntaxKind.CompilationUnit))
            {
                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                IEnumerable <string> reservedNames = semanticModel
                                                     .LookupNamespacesAndTypes(member.SpanStart)
                                                     .Select(f => f.Name);

                string name = Identifier.EnsureUniqueName(Identifier.DefaultNamespaceName, reservedNames);

                NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration(
                    IdentifierName(Identifier(name).WithRenameAnnotation()),
                    default(SyntaxList <ExternAliasDirectiveSyntax>),
                    default(SyntaxList <UsingDirectiveSyntax>),
                    SingletonList(member));

                return(await document.ReplaceNodeAsync(member, namespaceDeclaration.WithFormatterAnnotation(), cancellationToken).ConfigureAwait(false));
            }

            return(document);
        }
        private async Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax member,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (member.IsParentKind(SyntaxKind.CompilationUnit))
            {
                SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                string name = SyntaxUtility.GetUniqueName("Namespace", semanticModel, member.Span.Start);

                NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration(
                    IdentifierName(Identifier(name).WithRenameAnnotation()),
                    default(SyntaxList <ExternAliasDirectiveSyntax>),
                    default(SyntaxList <UsingDirectiveSyntax>),
                    SingletonList(member));

                SyntaxNode newRoot = root.ReplaceNode(member, namespaceDeclaration.WithFormatterAnnotation());

                return(document.WithSyntaxRoot(newRoot));
            }

            return(document);
        }
Ejemplo n.º 3
0
        private static async Task <Document> DeclareTypeInsideNamespaceAsync(
            Document document,
            MemberDeclarationSyntax member,
            CancellationToken cancellationToken)
        {
            Debug.Assert(member.IsParentKind(SyntaxKind.CompilationUnit), member.Parent?.Kind().ToString());

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            string name = NameGenerator.Default.EnsureUniqueName(
                DefaultNames.Namespace,
                semanticModel.LookupNamespacesAndTypes(member.SpanStart));

            NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration(
                IdentifierName(Identifier(name).WithRenameAnnotation()),
                default(SyntaxList <ExternAliasDirectiveSyntax>),
                default(SyntaxList <UsingDirectiveSyntax>),
                SingletonList(member));

            return(await document.ReplaceNodeAsync(member, namespaceDeclaration.WithFormatterAnnotation(), cancellationToken).ConfigureAwait(false));
        }