public AddConstructorParametersCodeAction(
     AddConstructorParametersFromMembersCodeRefactoringProvider service,
     Document document,
     State state,
     ImmutableArray <IParameterSymbol> missingParameters)
 {
     _service           = service;
     _document          = document;
     _state             = state;
     _missingParameters = missingParameters;
 }
Esempio n. 2
0
            public static State Generate(
                AddConstructorParametersFromMembersCodeRefactoringProvider service,
                ImmutableArray <ISymbol> selectedMembers)
            {
                var state = new State();

                if (!state.TryInitialize(service, selectedMembers))
                {
                    return(null);
                }

                return(state);
            }
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);
            }