Esempio n. 1
0
            private async Task <bool> TryInitializeAsync(
                AddConstructorParametersFromMembersCodeRefactoringProvider service,
                ImmutableArray <ISymbol> selectedMembers,
                Document document,
                CancellationToken cancellationToken)
            {
                ContainingType = selectedMembers[0].ContainingType;

                var rules = await document.GetNamingRulesAsync(FallbackNamingRules.RefactoringMatchLookupRules, cancellationToken).ConfigureAwait(false);

                var parametersForSelectedMembers = service.DetermineParameters(selectedMembers, rules);

                if (!selectedMembers.All(IsWritableInstanceFieldOrProperty) ||
                    ContainingType == null ||
                    ContainingType.TypeKind == TypeKind.Interface ||
                    parametersForSelectedMembers.IsEmpty)
                {
                    return(false);
                }

                ConstructorCandidates = await GetConstructorCandidatesInfoAsync(
                    ContainingType, service, selectedMembers, document, parametersForSelectedMembers, cancellationToken).ConfigureAwait(false);

                return(!ConstructorCandidates.IsEmpty);
            }
Esempio n. 2
0
            private bool TryInitialize(
                AddConstructorParametersFromMembersCodeRefactoringProvider service,
                Document document,
                TextSpan textSpan,
                ImmutableArray <ISymbol> selectedMembers,
                CancellationToken cancellationToken)
            {
                if (!selectedMembers.All(IsWritableInstanceFieldOrProperty))
                {
                    return(false);
                }

                this.TextSpan        = textSpan;
                this.SelectedMembers = selectedMembers;
                this.ContainingType  = this.SelectedMembers[0].ContainingType;
                if (this.ContainingType == null || this.ContainingType.TypeKind == TypeKind.Interface)
                {
                    return(false);
                }

                this.Parameters = service.DetermineParameters(selectedMembers);

                this.MatchingConstructor  = service.GetMatchingConstructor(this.ContainingType, this.Parameters);
                this.DelegatedConstructor = service.GetDelegatedConstructor(this.ContainingType, this.Parameters);
                return(this.DelegatedConstructor != null);
            }
Esempio n. 3
0
            private bool TryInitialize(
                AddConstructorParametersFromMembersCodeRefactoringProvider service,
                ImmutableArray <ISymbol> selectedMembers)
            {
                if (!selectedMembers.All(IsWritableInstanceFieldOrProperty))
                {
                    return(false);
                }

                this.ContainingType = selectedMembers[0].ContainingType;
                if (this.ContainingType == null || this.ContainingType.TypeKind == TypeKind.Interface)
                {
                    return(false);
                }

                var parameters = service.DetermineParameters(selectedMembers);

                // We are trying to add these parameters into an existing constructor's parameter list.
                // Comparing parameters based on names to make sure parameter list won't contains duplicate parameters after we
                // append the new parameters
                this.ConstructorToAddTo = GetDelegatedConstructorBasedOnParameterNames(this.ContainingType, parameters);

                if (this.ConstructorToAddTo == null)
                {
                    return(false);
                }

                var zippedParametersAndSelectedMembers = parameters.Zip(selectedMembers, (parameter, selectedMember) => (parameter, selectedMember));
                var missingParametersBuilder           = ArrayBuilder <IParameterSymbol> .GetInstance();

                var missingMembersBuilder = ArrayBuilder <ISymbol> .GetInstance();

                var constructorParamNames = this.ConstructorToAddTo.Parameters.SelectAsArray(p => p.Name);

                foreach ((var parameter, var selectedMember) in zippedParametersAndSelectedMembers)
                {
                    if (!constructorParamNames.Contains(parameter.Name))
                    {
                        missingParametersBuilder.Add(parameter);
                        missingMembersBuilder.Add(selectedMember);
                    }
                }

                this.MissingParameters = missingParametersBuilder.ToImmutableAndFree();
                this.MissingMembers    = missingMembersBuilder.ToImmutableAndFree();

                return(MissingParameters.Length != 0);
            }
Esempio n. 4
0
            /// <summary>
            /// Try to find all constructors in <paramref name="containingType"/> whose parameters
            /// are a subset of the selected members by comparing name.
            /// These constructors will not be considered as potential candidates:
            ///  - if the constructor's parameter list contains 'ref' or 'params'
            ///  - any constructor that has a params[] parameter
            ///  - deserialization constructor
            ///  - implicit default constructor
            /// </summary>
            private async Task <ImmutableArray <ConstructorCandidate> > GetConstructorCandidatesInfoAsync(
                INamedTypeSymbol containingType,
                AddConstructorParametersFromMembersCodeRefactoringProvider service,
                ImmutableArray <ISymbol> selectedMembers,
                Document document,
                CancellationToken cancellationToken)
            {
                var parametersForSelectedMembers = service.DetermineParameters(selectedMembers);
                var applicableConstructors       = ArrayBuilder <ConstructorCandidate> .GetInstance();

                foreach (var constructor in containingType.InstanceConstructors)
                {
                    if (await IsApplicableConstructorAsync(
                            constructor, document, parametersForSelectedMembers.SelectAsArray(p => p.Name), cancellationToken).ConfigureAwait(false))
                    {
                        applicableConstructors.Add(CreateConstructorCandidate(parametersForSelectedMembers, selectedMembers, constructor));
                    }
                }

                return(applicableConstructors.ToImmutableAndFree());
            }