private async Task <bool> TryInitializeAsync(
                SyntaxNode node, CancellationToken cancellationToken)
            {
                if (_service.IsConstructorInitializerGeneration(_document, node, cancellationToken))
                {
                    if (!await TryInitializeConstructorInitializerGenerationAsync(node, cancellationToken).ConfigureAwait(false))
                    {
                        return(false);
                    }
                }
                else if (_service.IsSimpleNameGeneration(_document, node, cancellationToken))
                {
                    if (!await TryInitializeSimpleNameGenerationAsync(node, cancellationToken).ConfigureAwait(false))
                    {
                        return(false);
                    }
                }
                else if (_service.IsImplicitObjectCreation(_document, node, cancellationToken))
                {
                    if (!await TryInitializeImplicitObjectCreationAsync(node, cancellationToken).ConfigureAwait(false))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                Contract.ThrowIfNull(TypeToGenerateIn);
                if (!CodeGenerator.CanAdd(_document.Project.Solution, TypeToGenerateIn, cancellationToken))
                {
                    return(false);
                }

                ParameterTypes     = ParameterTypes.IsDefault ? GetParameterTypes(cancellationToken) : ParameterTypes;
                _parameterRefKinds = _arguments.SelectAsArray(a => a.RefKind);

                if (ClashesWithExistingConstructor())
                {
                    return(false);
                }

                if (!TryInitializeDelegatedConstructor(cancellationToken))
                {
                    InitializeNonDelegatedConstructor(cancellationToken);
                }

                IsContainedInUnsafeType = _service.ContainingTypesOrSelfHasUnsafeKeyword(TypeToGenerateIn);

                return(true);
            }
Пример #2
0
            protected async Task <bool> TryFinishInitializingStateAsync(TService service, SemanticDocument document, CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                this.TypeToGenerateIn = await SymbolFinder.FindSourceDefinitionAsync(this.TypeToGenerateIn, document.Project.Solution, cancellationToken).ConfigureAwait(false) as INamedTypeSymbol;

                if (this.TypeToGenerateIn.IsErrorType())
                {
                    return(false);
                }

                if (!service.ValidateTypeToGenerateIn(document.Project.Solution, this.TypeToGenerateIn,
                                                      this.IsStatic, ClassInterfaceModuleStructTypes, cancellationToken))
                {
                    return(false);
                }

                if (!CodeGenerator.CanAdd(document.Project.Solution, this.TypeToGenerateIn, cancellationToken))
                {
                    return(false);
                }

                // Ok.  It either didn't bind to any symbols, or it bound to a symbol but with
                // errors.  In the former case we definitely want to offer to generate a method.  In
                // the latter case, we want to generate a method *unless* there's an existing method
                // with the same signature.
                var existingMethods = this.TypeToGenerateIn.GetMembers(this.IdentifierToken.ValueText)
                                      .OfType <IMethodSymbol>();

                var destinationProvider = document.Project.Solution.Workspace.Services.GetLanguageServices(this.TypeToGenerateIn.Language);
                var syntaxFacts         = destinationProvider.GetService <ISyntaxFactsService>();
                var syntaxFactory       = destinationProvider.GetService <SyntaxGenerator>();

                this.IsContainedInUnsafeType = service.ContainingTypesOrSelfHasUnsafeKeyword(this.TypeToGenerateIn);
                var generatedMethod = this.SignatureInfo.GenerateMethod(syntaxFactory, false, cancellationToken);

                return(!existingMethods.Any(m => SignatureComparer.Instance.HaveSameSignature(m, generatedMethod, caseSensitive: syntaxFacts.IsCaseSensitive, compareParameterName: true, isParameterCaseSensitive: syntaxFacts.IsCaseSensitive)));
            }
            private async Task <bool> TryInitializeAsync(
                TService service,
                SemanticDocument document,
                SyntaxNode node,
                CancellationToken cancellationToken)
            {
                if (service.IsIdentifierNameGeneration(node))
                {
                    // Cases that we deal with currently:
                    //
                    // 1) expr.Goo
                    // 2) expr->Goo
                    // 3) Goo
                    if (!TryInitializeSimpleName(service, document, (TSimpleNameSyntax)node, cancellationToken))
                    {
                        return(false);
                    }
                }
                else if (service.IsExplicitInterfaceGeneration(node))
                {
                    // 4)  bool IGoo.NewProp
                    if (!TryInitializeExplicitInterface(service, document, node, cancellationToken))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                // Ok.  It either didn't bind to any symbols, or it bound to a symbol but with
                // errors.  In the former case we definitely want to offer to generate a field.  In
                // the latter case, we want to generate a field *unless* there's an existing member
                // with the same name.  Note: it's ok if there's a  method with the same name.
                var existingMembers = this.TypeToGenerateIn.GetMembers(this.IdentifierToken.ValueText)
                                      .Where(m => m.Kind != SymbolKind.Method);

                if (existingMembers.Any())
                {
                    // TODO: Code coverage
                    // There was an existing method that the new method would clash with.
                    return(false);
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                this.TypeToGenerateIn = await SymbolFinder.FindSourceDefinitionAsync(this.TypeToGenerateIn, document.Project.Solution, cancellationToken).ConfigureAwait(false) as INamedTypeSymbol;

                if (!service.ValidateTypeToGenerateIn(
                        document.Project.Solution, this.TypeToGenerateIn, this.IsStatic, ClassInterfaceModuleStructTypes, cancellationToken))
                {
                    return(false);
                }

                this.IsContainedInUnsafeType = service.ContainingTypesOrSelfHasUnsafeKeyword(this.TypeToGenerateIn);

                return(CanGenerateLocal() || CodeGenerator.CanAdd(document.Project.Solution, this.TypeToGenerateIn, cancellationToken));
            }