Example #1
0
		private CompilationUnitSyntax UpdateUsings(CompilationUnitSyntax compilationUnit)
		{
			var namespaces = compilationUnit.Members.OfType<NamespaceDeclarationSyntax>().ToList();
			var usings = compilationUnit.Usings;
			
			usings = usings
				.AddRange(extraUsingDirectives)
				.AddRange(namespaces.SelectMany(c => GetNamespacesAndParents(c.Name.ToString()).Select(d => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(d)))))
				.Sort();

			usings = usings.Replace(usings[0], usings[0].WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.PragmaWarningDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.DisableKeyword), true))));
			
			return compilationUnit.WithUsings(usings);
		}
Example #2
0
        public static CompilationUnitSyntax AddUsingStatements(this CompilationUnitSyntax compilationUnit, string[] usingStatements)
        {
            if (compilationUnit == null)
            {
                throw new ArgumentNullException(nameof(compilationUnit));
            }

            if (usingStatements == null)
            {
                throw new ArgumentNullException(nameof(usingStatements));
            }

            if (usingStatements.Length == 0)
            {
                return(compilationUnit);
            }

            compilationUnit = compilationUnit.AddUsings(
                usingStatements
                .Select(s => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(s)))
                .ToArray());

            return(compilationUnit.WithUsings(compilationUnit.Usings.Sort()));
        }
Example #3
0
 public override SyntaxNode VisitCompilationUnit(Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax node)
 => base.VisitCompilationUnit(node.WithUsings(
                                  Microsoft.CodeAnalysis.CSharp.SyntaxFactory.List <UsingDirectiveSyntax>()));