public abstract bool ShouldUpdateAccessibilityModifier(
     ISyntaxFacts syntaxFacts,
     TMemberDeclarationSyntax member,
     AccessibilityModifiersRequired option,
     out SyntaxToken name);
Exemple #2
0
        public static async Task <Document> ConvertToProgramMainAsync(Document document, AccessibilityModifiersRequired accessibilityModifiersRequired, CancellationToken cancellationToken)
        {
            // While the analyze and refactoring check ensure we're in a well formed state for their needs, the 'new
            // template' code just calls directly into this if the user prefers Program.Main.  So check and make sure
            // this is actually something we can convert before proceeding.
            var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            if (root.IsTopLevelProgram())
            {
                var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);

                var programType = compilation.GetBestTypeByMetadataName(WellKnownMemberNames.TopLevelStatementsEntryPointTypeName);
                if (programType != null)
                {
                    if (programType.GetMembers(WellKnownMemberNames.TopLevelStatementsEntryPointMethodName).FirstOrDefault() is IMethodSymbol mainMethod)
                    {
                        var classDeclaration = await GenerateProgramClassAsync(
                            document, programType, mainMethod, accessibilityModifiersRequired, cancellationToken).ConfigureAwait(false);

                        var newRoot = root.RemoveNodes(root.Members.OfType <GlobalStatementSyntax>().Skip(1), SyntaxGenerator.DefaultRemoveOptions);
                        Contract.ThrowIfNull(newRoot);

                        var firstGlobalStatement = newRoot.Members.OfType <GlobalStatementSyntax>().Single();
                        newRoot = newRoot.ReplaceNode(firstGlobalStatement, classDeclaration);

                        return(document.WithSyntaxRoot(newRoot));
                    }
                }
            }

            return(document);
        }