private static async Task <Document> GenerateComparisonOperatorsAsync(
            Document document,
            SyntaxNode typeDeclaration,
            INamedTypeSymbol comparableType,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var containingType = (INamedTypeSymbol)semanticModel.GetRequiredDeclaredSymbol(typeDeclaration, cancellationToken);
            var compareMethod  = TryGetCompareMethodImpl(containingType, comparableType) !;

            var generator = document.GetRequiredLanguageService <SyntaxGenerator>();

            var codeGenService = document.GetRequiredLanguageService <ICodeGenerationService>();
            var operators      = GenerateComparisonOperators(
                generator, semanticModel.Compilation, containingType, comparableType,
                GenerateLeftExpression(generator, comparableType, compareMethod));

            var solutionContext = new CodeGenerationSolutionContext(
                document.Project.Solution,
                new CodeGenerationContext(contextLocation: typeDeclaration.GetLocation()),
                fallbackOptions);

            return(await codeGenService.AddMembersAsync(solutionContext, containingType, operators, cancellationToken).ConfigureAwait(false));
        }
Пример #2
0
        public override async Task <Document> AddEventAsync(
            CodeGenerationSolutionContext context, INamedTypeSymbol destination, IEventSymbol @event, CancellationToken cancellationToken)
        {
            var newDocument = await base.AddEventAsync(
                context, destination, @event, cancellationToken).ConfigureAwait(false);

            var namedType = @event.Type as INamedTypeSymbol;

            if (namedType?.AssociatedSymbol != null)
            {
                // This is a VB event that declares its own type.  i.e. "Public Event E(x As Object)"
                // We also have to generate "public void delegate EEventHandler(object x)"
                var compilation = await newDocument.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);

                var newDestinationSymbol = destination.GetSymbolKey(cancellationToken).Resolve(compilation, cancellationToken: cancellationToken).Symbol;
                var newContext           = context with {
                    Solution = newDocument.Project.Solution
                };

                if (newDestinationSymbol?.ContainingType != null)
                {
                    return(await AddNamedTypeAsync(newContext, newDestinationSymbol.ContainingType, namedType, cancellationToken).ConfigureAwait(false));
                }
                else if (newDestinationSymbol?.ContainingNamespace != null)
                {
                    return(await AddNamedTypeAsync(newContext, newDestinationSymbol.ContainingNamespace, namedType, cancellationToken).ConfigureAwait(false));
                }
            }

            return(newDocument);
        }
        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));
        }
            protected override async Task <Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                var generateUnsafe = _state.TypeMemberType.RequiresUnsafeModifier() &&
                                     !_state.IsContainedInUnsafeType;

                var context = new CodeGenerationSolutionContext(
                    _semanticDocument.Project.Solution,
                    new CodeGenerationContext(
                        afterThisLocation: _state.AfterThisLocation,
                        beforeThisLocation: _state.BeforeThisLocation,
                        contextLocation: _state.IdentifierToken.GetLocation()),
                    _fallbackOptions);

                if (_generateProperty)
                {
                    var getAccessor = CreateAccessor(_state.DetermineMaximalAccessibility());
                    var setAccessor = _isReadonly || _refKind != RefKind.None
                        ? null
                        : CreateAccessor(DetermineMinimalAccessibility(_state));

                    var propertySymbol = CodeGenerationSymbolFactory.CreatePropertySymbol(
                        attributes: default,
Пример #5
0
        protected static async Task <Document> AddPropertyAsync(
            Document document,
            Solution destinationSolution,
            IFieldSymbol field,
            IPropertySymbol property,
            CodeAndImportGenerationOptionsProvider fallbackOptions,
            CancellationToken cancellationToken)
        {
            var codeGenerationService = document.GetLanguageService <ICodeGenerationService>();

            var fieldDeclaration = field.DeclaringSyntaxReferences.First();

            var context = new CodeGenerationSolutionContext(
                destinationSolution,
                new CodeGenerationContext(
                    contextLocation: fieldDeclaration.SyntaxTree.GetLocation(fieldDeclaration.Span)),
                fallbackOptions);

            var destination = field.ContainingType;

            return(await codeGenerationService.AddPropertyAsync(
                       context, destination, property, cancellationToken).ConfigureAwait(false));
        }
        private async Task <Document> GetEditAsync(
            CodeGenerationSolutionContext context,
            INamespaceOrTypeSymbol destination,
            Func <SyntaxNode, TCodeGenerationContextInfo, IList <bool>?, CancellationToken, SyntaxNode> declarationTransform,
            CancellationToken cancellationToken)
        {
            var(destinationDeclaration, availableIndices) =
                await FindMostRelevantDeclarationAsync(context.Solution, destination, context.Context.BestLocation, cancellationToken).ConfigureAwait(false);

            if (destinationDeclaration == null)
            {
                throw new ArgumentException(WorkspacesResources.Could_not_find_location_to_generation_symbol_into);
            }

            var destinationTree = destinationDeclaration.SyntaxTree;
            var oldDocument     = context.Solution.GetRequiredDocument(destinationTree);
            var codeGenOptions  = await oldDocument.GetCodeGenerationOptionsAsync(context.FallbackOptions, cancellationToken).ConfigureAwait(false);

            var info = (TCodeGenerationContextInfo)codeGenOptions.GetInfo(context.Context, destinationDeclaration.SyntaxTree.Options);
            var transformedDeclaration = declarationTransform(destinationDeclaration, info, availableIndices, cancellationToken);

            var root = await destinationTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var currentRoot = root.ReplaceNode(destinationDeclaration, transformedDeclaration);

            var newDocument = oldDocument.WithSyntaxRoot(currentRoot);

            if (context.Context.AddImports)
            {
                var addImportsOptions = await newDocument.GetAddImportPlacementOptionsAsync(context.FallbackOptions, cancellationToken).ConfigureAwait(false);

                newDocument = await ImportAdder.AddImportsFromSymbolAnnotationAsync(newDocument, addImportsOptions, cancellationToken).ConfigureAwait(false);
            }

            return(newDocument);
        }