Exemplo n.º 1
0
        private SyntaxNode AddFlagAttributeVisualBasic(Document document, SyntaxNode root, SyntaxNode statement)
        {
            var generator = SyntaxGenerator.GetGenerator(document);

            var flagsAttribute = VisualBasicSyntaxFactory.Attribute(VisualBasicSyntaxFactory.ParseName("Flags"));
            var newStatement   = generator.AddAttributes(statement, flagsAttribute);

            var newRoot = root.ReplaceNode(statement, newStatement).WithAdditionalAnnotations(Formatter.Annotation);

            var compilationUnit = (VisualBasicCompilationUnitSyntax)newRoot;

            var importSystemClause =
                VisualBasicSyntaxFactory.SimpleImportsClause(
                    VisualBasicSyntaxFactory.ParseName("System"))
                .WithTrailingTrivia(
                    VisualBasicSyntaxFactory.SyntaxTrivia(
                        Microsoft.CodeAnalysis.VisualBasic.SyntaxKind.EndOfLineTrivia, Environment.NewLine));
            var importsList = VisualBasicSyntaxFactory.SeparatedList(new List <ImportsClauseSyntax> {
                importSystemClause
            });
            var importStatement = VisualBasicSyntaxFactory.ImportsStatement(importsList);

            var imports =
                compilationUnit.Imports.SelectMany(
                    c => c.ImportsClauses.OfType <SimpleImportsClauseSyntax>().Select(i => i.Name.GetText().ToString()));

            if (imports.All(u => u != importSystemClause.Name.GetText().ToString()))
            {
                newRoot = generator.AddNamespaceImports(compilationUnit, importStatement);
            }

            return(newRoot);
        }