public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            SyntaxList <UsingDirectiveSyntax> list = new SyntaxList <UsingDirectiveSyntax>();

            list = list.AddRange(m_temp);
            return(node.WithUsings(list));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the proxy syntax tree
        /// </summary>
        /// <param name="interfaceType">Actor interface type</param>
        /// <param name="actorType">Actor type</param>
        /// <param name="actorMachineType">Actor machine type</param>
        /// <returns>SyntaxTree</returns>
        private SyntaxTree CreateProxySyntaxTree(Type interfaceType, Type actorType, Type actorMachineType)
        {
            ClassDeclarationSyntax proxyDecl = this.CreateProxyClassDeclaration(
                interfaceType, actorType, actorMachineType);

            NamespaceDeclarationSyntax namespaceDecl = SyntaxFactory.NamespaceDeclaration(
                SyntaxFactory.IdentifierName(actorType.Namespace + "_PSharpProxy"));

            var usingDirectives = new List <UsingDirectiveSyntax>
            {
                SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.PSharp.Actors")),
                SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.PSharp.Actors.Utilities"))
            };

            foreach (var requiredNamespace in this.RequiredNamespaces)
            {
                usingDirectives.Add(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName(requiredNamespace)));
            }

            namespaceDecl = namespaceDecl.WithUsings(SyntaxFactory.List(usingDirectives))
                            .WithMembers(SyntaxFactory.List(
                                             new List <MemberDeclarationSyntax>
            {
                proxyDecl
            }));

            namespaceDecl = namespaceDecl.NormalizeWhitespace();

            return(namespaceDecl.SyntaxTree);
        }
        public static NamespaceDeclarationSyntax AddUsingDirectives(
            this NamespaceDeclarationSyntax namespaceDeclaration,
            IList <UsingDirectiveSyntax> usingDirectives,
            bool placeSystemNamespaceFirst,
            params SyntaxAnnotation[] annotations)
        {
            if (!usingDirectives.Any())
            {
                return(namespaceDeclaration);
            }

            var specialCaseSystem = placeSystemNamespaceFirst;
            var comparer          = specialCaseSystem
                ? UsingsAndExternAliasesDirectiveComparer.SystemFirstInstance
                : UsingsAndExternAliasesDirectiveComparer.NormalInstance;

            var usings = new List <UsingDirectiveSyntax>();

            usings.AddRange(namespaceDeclaration.Usings);
            usings.AddRange(usingDirectives);

            if (namespaceDeclaration.Usings.IsSorted(comparer))
            {
                usings.Sort(comparer);
            }

            usings = usings.Select(u => u.WithAdditionalAnnotations(annotations)).ToList();
            var newNamespace = namespaceDeclaration.WithUsings(usings.ToSyntaxList());

            return(newNamespace);
        }
Ejemplo n.º 4
0
        private static CompilationUnitSyntax SortUsingDirectives(
            this CompilationUnitSyntax compilation,
            ref NamespaceDeclarationSyntax ns)
        {
            // TODO: Add removing of unused namespaces
            var usings = compilation
                         .GetAllUsingDirectives(ns)
                         .OrderBy(u => u, UsingsAndExternAliasesDirectiveComparer.SystemFirstInstance)
                         .ToSyntaxList();

            compilation = compilation.RemoveAllUsings(ref ns);

            if (ns != null)
            {
                var newNs = ns.WithUsings(usings);

                compilation = compilation.ReplaceNode(ns, newNs);
                ns          = newNs;
            }
            else
            {
                compilation = compilation.WithUsings(usings);
            }

            return(compilation);
        }
Ejemplo n.º 5
0
        private static CompilationUnitSyntax AddUsingDirective(
            this CompilationUnitSyntax compilation,
            string namespacesToAdd,
            ref NamespaceDeclarationSyntax ns)
        {
            var usingDirective = CreateUsingDirective(namespacesToAdd);

            // Namespace doesn't exists so we are adding using at the top of the file
            if (ns == null)
            {
                if (!compilation.Usings.Any(u => u.Name.ToFullString() == namespacesToAdd))
                {
                    var newUsings = compilation.Usings.Add(usingDirective);

                    compilation = compilation.WithUsings(newUsings);
                }
            }
            else if (!ns.Usings.Any(u => u.Name.ToFullString() == namespacesToAdd))
            {
                var newUsings = ns.Usings.Add(usingDirective);
                var newNs     = ns.WithUsings(newUsings);

                compilation = compilation.ReplaceNode(ns, newNs);
                ns          = newNs;
            }

            return(compilation);
        }
        public override SyntaxNode?VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            var usings  = OrganizeUsings(node.Usings);
            var members = OrganizeMembers(node.Members);

            return(node.WithUsings(usings).WithMembers(members));
        }
        /// <summary>Called when the visitor visits a NamespaceDeclarationSyntax node.</summary>
        public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            var u = _namespaces.Select(each => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($" {each}"))
                                       .WithLeadingTrivia(_leadingTrivia)
                                       .WithTrailingTrivia(_trailingTrivia)
                                       );


            if (node.Name != null && node.Name.GetType() == typeof(QualifiedNameSyntax))
            {
                var currUsings = new List <string>();
                var currNode   = ((QualifiedNameSyntax)node.Name);

                while (true)
                {
                    currUsings.Add(currNode.ToString());
                    if (currNode.Left == null || currNode.Left.GetType() != typeof(QualifiedNameSyntax))
                    {
                        break;
                    }
                    currNode = (QualifiedNameSyntax)currNode.Left;
                }
                u = u.Where(uName => !currUsings.Contains(uName.Name.ToString()));
            }
            node = node.AddUsings(u.ToArray());
            node = node.WithUsings(Sort(node.Usings));
            return(base.VisitNamespaceDeclaration(node));
        }
Ejemplo n.º 8
0
        internal static NamespaceDeclarationSyntax Namespace(string namespaceName)
        {
            NamespaceDeclarationSyntax @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(namespaceName));

            @namespace = @namespace.WithUsings(SyntaxFactory.List(usings.Select(n => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(n)))));

            return(@namespace);
        }
Ejemplo n.º 9
0
        /// <summary>Called when the visitor visits a NamespaceDeclarationSyntax node.</summary>
        public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            var u = _namespaces.Select(each => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($" {each}"))
                                       .WithLeadingTrivia(_leadingTrivia)
                                       .WithTrailingTrivia(_trailingTrivia)
                                       ).ToArray();

            node = node.AddUsings(u);
            node = node.WithUsings(Sort(node.Usings));
            return(base.VisitNamespaceDeclaration(node));
        }
        private async Task <Document> CleanupUsingDirectives(
            Document document,
            NamespaceDeclarationSyntax namespaceDeclaration)
        {
            var usingDirectives         = UsingDirectiveHelpers.CleanupUsingDirectives(namespaceDeclaration.Usings);
            var usings                  = new SyntaxList <UsingDirectiveSyntax>(usingDirectives);
            var newNamespaceDeclaration = namespaceDeclaration.WithUsings(usings);
            var root = await document.GetSyntaxRootAsync();

            var newRoot = root.ReplaceNode(namespaceDeclaration, newNamespaceDeclaration);

            return(document.WithSyntaxRoot(newRoot));
        }
Ejemplo n.º 11
0
        private static bool ReplaceInsideNamespace(
            NamespaceDeclarationSyntax namespaceRoot,
            UsingDirectiveSyntax newUsing,
            string existingContractNamespace,
            [NotNullWhen(true)] out NamespaceDeclarationSyntax?newNamespaceRoot)
        {
            var namespaceUsings = namespaceRoot.Usings;

            if (FindIndex(namespaceRoot.Usings, existingContractNamespace, out int namespaceIndex))
            {
                newNamespaceRoot = namespaceRoot.WithUsings(
                    namespaceUsings.Replace(namespaceUsings[namespaceIndex], newUsing));
                return(true);
            }

            newNamespaceRoot = null;
            return(false);
        }
Ejemplo n.º 12
0
        private static NamespaceDeclarationSyntax AmendUsings(NamespaceDeclarationSyntax nameSpace, SyntaxList <UsingDirectiveSyntax> usings)
        {
            var last = nameSpace.Name.ToString().Right(c => c != '.');

            foreach (var item in usings)
            {
                var name  = item.Name.ToString();
                var first = name.Left(c => c != '.');

                if (first == last)
                {
                    usings = usings.Remove(item);
                    usings = usings.Add(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("global::" + name)));
                }
            }

            return(nameSpace.WithUsings(usings));
        }
Ejemplo n.º 13
0
        public static CompilationUnitSyntax RemoveAllUsings(
            this CompilationUnitSyntax compilation,
            ref NamespaceDeclarationSyntax ns)
        {
            if (compilation.Usings.Count > 0)
            {
                compilation = compilation.WithUsings(SyntaxFactory.List <UsingDirectiveSyntax>());
            }

            if (ns?.Usings.Count > 0)
            {
                var newNs = ns.WithUsings(SyntaxFactory.List <UsingDirectiveSyntax>());

                compilation = compilation.ReplaceNode(ns, newNs);
                ns          = newNs;
            }

            return(compilation);
        }
Ejemplo n.º 14
0
        public static NamespaceDeclarationSyntax AppendUsing(
            this NamespaceDeclarationSyntax node,
            params string[] usingDirectives)
        {
            List <string> list = node.DescendantNodes((Func <SyntaxNode, bool>)(p => !(p is ClassDeclarationSyntax)), false).OfType <UsingDirectiveSyntax>().Select <UsingDirectiveSyntax, string>((Func <UsingDirectiveSyntax, string>)(p => p.Name.ToString())).ToList <string>();

            foreach (string usingDirective in usingDirectives)
            {
                if (usingDirective != null && !list.Contains(usingDirective))
                {
                    list.Add(usingDirective);
                }
            }
            list.Sort();
            SyntaxList <UsingDirectiveSyntax> usings = node.Usings;

            while (usings.Count > 0)
            {
                usings = usings.RemoveAt(0);
            }
            usings = usings.AddRange(list.Select <string, UsingDirectiveSyntax>((Func <string, UsingDirectiveSyntax>)(u => u.ToUsing())));
            return(node.WithUsings(usings));
        }
Ejemplo n.º 15
0
        public static NamespaceDeclarationSyntax AddUsingDirectives(
            this NamespaceDeclarationSyntax namespaceDeclaration,
            IList <UsingDirectiveSyntax> usingDirectives,
            bool placeSystemNamespaceFirst,
            params SyntaxAnnotation[] annotations)
        {
            if (usingDirectives.Count == 0)
            {
                return(namespaceDeclaration);
            }

            var newUsings = new List <UsingDirectiveSyntax>();

            newUsings.AddRange(namespaceDeclaration.Usings);
            newUsings.AddRange(usingDirectives);

            newUsings.SortUsingDirectives(namespaceDeclaration.Usings, placeSystemNamespaceFirst);
            newUsings = newUsings.Select(u => u.WithAdditionalAnnotations(annotations)).ToList();

            var newNamespace = namespaceDeclaration.WithUsings(newUsings.ToSyntaxList());

            return(newNamespace);
        }
        private async Task <Solution> MoveUsingDirectiveAsync(Document document, UsingDirectiveSyntax usingDirectiveNode, NamespaceDeclarationSyntax namespaceNode, CancellationToken cancellationToken)
        {
            var usings         = namespaceNode.Usings;
            var insertPosition = GetInsertPositionFor(usingDirectiveNode, usings);
            var newUsings      = usings.Insert(insertPosition, usingDirectiveNode);

            var root = await document.GetSyntaxRootAsync();

            var trackedRoot = root.TrackNodes(namespaceNode, usingDirectiveNode);

            trackedRoot = trackedRoot.RemoveNode(trackedRoot.GetCurrentNode(usingDirectiveNode), SyntaxRemoveOptions.KeepNoTrivia);
            trackedRoot = trackedRoot.ReplaceNode(trackedRoot.GetCurrentNode(namespaceNode), namespaceNode.WithUsings(newUsings));

            var formattedRoot = Formatter.Format(trackedRoot, document.Project.Solution.Workspace);

            var newDocument = document.WithSyntaxRoot(formattedRoot);

            return(newDocument.Project.Solution);
        }
Ejemplo n.º 17
0
 public static NamespaceDeclarationSyntax WithUsings(this NamespaceDeclarationSyntax syntax, params UsingDirectiveSyntax[] tokens)
 {
     return(syntax.WithUsings(SyntaxFactory.List(tokens)));
 }
Ejemplo n.º 18
0
        public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
        {
            var ns = node.WithUsings(new SyntaxList <UsingDirectiveSyntax>(Usings));

            return(ns);
        }
Ejemplo n.º 19
0
 public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
 {
     return(base.VisitNamespaceDeclaration(
                node.WithUsings(this.SortUsings(node.Usings))
                ));
 }