Ejemplo n.º 1
0
        /// <summary>
        /// Generates formatted source code containing general information about the symbol's
        /// containing assembly, and the public, protected, and protected-or-internal interface of
        /// which the given ISymbol is or is a part of into the given document
        /// </summary>
        /// <param name="document">The document to generate source into</param>
        /// <param name="symbolCompilation">The <see cref="Compilation"/> in which <paramref name="symbol"/> is resolved.</param>
        /// <param name="symbol">The symbol to generate source for</param>
        /// <param name="formattingOptions">Options to use to format the document.</param>
        /// <param name="cancellationToken">To cancel document operations</param>
        /// <returns>The updated document</returns>
        public static Task <Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, OmniSharpSyntaxFormattingOptionsWrapper formattingOptions, CancellationToken cancellationToken)
        {
            var service = document.GetRequiredLanguageService <IMetadataAsSourceService>();

            var options = new CleanCodeGenerationOptions(
                GenerationOptions: CodeGenerationOptions.GetDefault(document.Project.LanguageServices),
                CleanupOptions: formattingOptions.CleanupOptions);

            return(service.AddSourceToAsync(document, symbolCompilation, symbol, options, cancellationToken));
        }
        public async Task <Document> AddSourceToAsync(
            Document document,
            Compilation symbolCompilation,
            ISymbol symbol,
            CleanCodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var newSemanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var rootNamespace = newSemanticModel.GetEnclosingNamespace(position: 0, cancellationToken);

            Contract.ThrowIfNull(rootNamespace);

            var context = new CodeGenerationSolutionContext(
                document.Project.Solution,
                new CodeGenerationContext(
                    contextLocation: newSemanticModel.SyntaxTree.GetLocation(new TextSpan()),
                    generateMethodBodies: false,
                    generateDocumentationComments: true,
                    mergeAttributes: false,
                    autoInsertionLocation: false),
                new CodeAndImportGenerationOptions(options.GenerationOptions, options.CleanupOptions.AddImportOptions).CreateProvider());

            // Add the interface of the symbol to the top of the root namespace
            document = await CodeGenerator.AddNamespaceOrTypeDeclarationAsync(
                context,
                rootNamespace,
                CreateCodeGenerationSymbol(document, symbol),
                cancellationToken).ConfigureAwait(false);

            document = await AddNullableRegionsAsync(document, cancellationToken).ConfigureAwait(false);

            var docCommentFormattingService = document.GetRequiredLanguageService <IDocumentationCommentFormattingService>();
            var docWithDocComments          = await ConvertDocCommentsToRegularCommentsAsync(document, docCommentFormattingService, cancellationToken).ConfigureAwait(false);

            var docWithAssemblyInfo = await AddAssemblyInfoRegionAsync(docWithDocComments, symbolCompilation, symbol.GetOriginalUnreducedDefinition(), cancellationToken).ConfigureAwait(false);

            var node = await docWithAssemblyInfo.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var formattedDoc = await Formatter.FormatAsync(
                docWithAssemblyInfo,
                SpecializedCollections.SingletonEnumerable(node.FullSpan),
                options.CleanupOptions.FormattingOptions,
                GetFormattingRules(docWithAssemblyInfo),
                cancellationToken).ConfigureAwait(false);

            var reducers = GetReducers();

            return(await Simplifier.ReduceAsync(formattedDoc, reducers, options.CleanupOptions.SimplifierOptions, cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        public static async Task <Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken)
        {
            var service = document.GetRequiredLanguageService <IMetadataAsSourceService>();

            var cleanupOptions = await document.GetCodeCleanupOptionsAsync(fallbackOptions : null, cancellationToken).ConfigureAwait(false);

            var options = new CleanCodeGenerationOptions(
                GenerationOptions: CodeGenerationOptions.GetDefault(document.Project.LanguageServices),
                CleanupOptions: cleanupOptions);

            return(await service.AddSourceToAsync(document, symbolCompilation, symbol, options, cancellationToken).ConfigureAwait(false));
        }
            public override ValueTask <CleanCodeGenerationOptions> GetCleanCodeGenerationOptionsAsync(HostLanguageServices languageServices, CancellationToken cancellationToken)
            {
                var lineFormattingOptions = _lineFormattingOptionsProvider.GetLineFormattingOptions();
                var codeGenerationOptions = CleanCodeGenerationOptions.GetDefault(languageServices) with
                {
                    CleanupOptions = CodeCleanupOptions.GetDefault(languageServices) with
                    {
                        FormattingOptions = SyntaxFormattingOptions.GetDefault(languageServices).With(new LineFormattingOptions
                        {
                            IndentationSize = lineFormattingOptions.IndentationSize,
                            TabSize         = lineFormattingOptions.TabSize,
                            UseTabs         = lineFormattingOptions.UseTabs,
                            NewLine         = lineFormattingOptions.NewLine,
                        })
                    }
                };

                return(new ValueTask <CleanCodeGenerationOptions>(codeGenerationOptions));
            }
 public static async ValueTask <CleanCodeGenerationOptions> GetCleanCodeGenerationOptionsAsync(this Document document, CleanCodeGenerationOptions fallbackOptions, CancellationToken cancellationToken)
 => new(