예제 #1
0
        private IConstructorDeclaration CreateConstructor(IClassDeclaration classDeclaration, IIdentifier fieldIdentifier, ITypeName typeName)
        {
            IConstructLanguage language = classDeclaration.Language;

            IParameter   parameter             = language.Parameter(typeName);
            NamingPolicy parameterNamingPolicy = parameter.PrimaryNamingPolicy(classDeclaration.FileModel.UserSettings);
            string       parameterName         = parameterNamingPolicy.ChangeNameAccordingToPolicy(classDeclaration.Identifier.Name, classDeclaration.SolutionModel);

            parameter.Identifier = language.Identifier(parameterName);
            Modifiers constructorModifiers = Modifiers.Public;
            Modifiers visibility           = classDeclaration.Modifiers.Modifiers.GetVisibility();

            if (visibility == Modifiers.None || visibility.IsInternal())
            {
                constructorModifiers = Modifiers.Internal;
            }

            IConstructorDeclaration constructor = language.Constructor(language.None <IDocComment>(),
                                                                       language.None <IAttributes>(),
                                                                       language.Modifiers(constructorModifiers),
                                                                       language.Parameters(parameter),
                                                                       language.None <IConstructorInitializer>(),
                                                                       language.Block(
                                                                           language.AssignmentStatement(
                                                                               language.MemberAccess(language.This(), fieldIdentifier),
                                                                               language.VariableAccess(parameter.Identifier))));

            return(constructor);
        }
예제 #2
0
 protected internal NewFormula(IConstructorDeclaration constructor, ReadOnlyCollection<Formula> arguments, ReadOnlyCollection<IMemberDeclaration> members)
     : base(NodeType.New, constructor.DeclaringType)
 {
     Constructor = constructor;
     Arguments = arguments;
     Members = members;
 }
예제 #3
0
        private static FixedList <ParameterIL> BuildConstructorParameters(IConstructorDeclaration constructorDeclaration)
        {
            var selfParameterSymbol = constructorDeclaration.ImplicitSelfParameter.Symbol;
            var selfParameter       = new SelfParameterIL(selfParameterSymbol);

            return(selfParameter.Yield().Concat(constructorDeclaration.Parameters.Select(BuildParameter)).ToFixedList());
        }
        public static IMethodName GetName([NotNull] this IConstructorDeclaration constructorDeclaration)
        {
            // TODO testing!
            var declaredElement = constructorDeclaration.DeclaredElement;

            Asserts.NotNull(declaredElement, "no declared element in declaration");
            return(declaredElement.GetName <IMethodName>());
        }
예제 #5
0
        private IEnumerable <IInvocationExpression> GetInvocationExpressions(IConstructorDeclaration constructorDeclaration)
        {
            var statementExpressions  = constructorDeclaration.Body.Children <IExpressionStatement>().Select(x => x.Expression);
            var invocationExpressions = statementExpressions.OfType <IInvocationExpression>()
                                        .SelectMany(z => z.DescendantsAndSelf(x => x.InvokedExpression.FirstChild as IInvocationExpression).Reverse());

            return(invocationExpressions);
        }
예제 #6
0
 internal InternalConstructorVisibilityHighlighting(
     [NotNull] string message,
     [NotNull] ITokenNode modifierTokenNode,
     [NotNull] IConstructorDeclaration constructorDeclaration,
     AccessRights visibility) : base(message)
 {
     this.modifierTokenNode = modifierTokenNode;
     ConstructorDeclaration = constructorDeclaration;
     Visibility             = visibility;
 }
예제 #7
0
        private IClassDeclaration CreateViewModel(IClassDeclaration classDeclaration)
        {
            ITypeName               typeName             = classDeclaration.Language.TypeName(classDeclaration.Type());
            IFieldDeclaration       fieldDeclaration     = CreateFieldToHoldWrappedClass(classDeclaration.Language, typeName, classDeclaration);
            IConstructorDeclaration viewModelConstructor = CreateConstructor(classDeclaration, fieldDeclaration.Identifier, typeName);

            IClassDeclaration viewModel = CreateInitialViewModel(classDeclaration, fieldDeclaration, viewModelConstructor);

            return(viewModel);
        }
예제 #8
0
        /// <summary>
        /// The execute transaction inner.
        /// </summary>
        /// <param name="solution">
        /// The solution.
        /// </param>
        /// <param name="textControl">
        /// The text control.
        /// </param>
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            ITreeNode element = Utils.GetElementAtCaret(solution, textControl);

            if (element != null)
            {
                IConstructorDeclaration constructorDeclaration = element.GetContainingNode <IConstructorDeclaration>(true);
                if (constructorDeclaration != null)
                {
                    new DocumentationRules().EnsureConstructorSummaryDocBeginsWithStandardText(constructorDeclaration);
                }
            }
        }
        /// <summary>Inspects the control graf.</summary>
        /// <param name="constructor">The constructor.</param>
        /// <returns>Returns the control graf.</returns>
        public CodeAnnotationAttribute InspectControlGraf([NotNull] IConstructorDeclaration constructor)
        {
            if (constructor == null)
            {
                throw new ArgumentNullException("constructor");
            }

            var function = constructor;

            if (!function.DeclaredElement.ReturnType.IsReferenceType())
            {
                return(CodeAnnotationAttribute.Undefined);
            }

            return(CodeAnnotationAttribute.NotNull);
        }
 public void AddAssignmentToBody(IConstructorDeclaration constructorDeclaration, IStatement anchorStatement,
     bool insertBefore, IParameter parameter, ITypeMember member)
 {
     var constructorDeclaration1 = constructorDeclaration;
     var instance = CSharpElementFactory.GetInstance(constructorDeclaration1);
     if (constructorDeclaration1.Body == null)
         constructorDeclaration1.SetBody(instance.CreateEmptyBlock());
     var shortName1 = parameter.ShortName;
     var shortName2 = member.ShortName;
     var statement = instance.CreateStatement("$0 = $1;", (object) shortName2, (object) shortName1);
     CodeStyleUtil.ApplyRecursive<ThisQualifierStyleSuggestion>(insertBefore
         ? constructorDeclaration1.Body.AddStatementBefore(statement,
             (ICSharpStatement) anchorStatement)
         : constructorDeclaration1.Body.AddStatementAfter(statement,
             (ICSharpStatement) anchorStatement));
 }
        /// <summary>Inspects the specified method.</summary>
        /// <param name="constructor">The method.</param>
        public void Inspect([NotNull] IConstructorDeclaration constructor)
        {
            this.CodeAnnotation = new CodeAnnotation(constructor);
            if (!this.CodeAnnotation.IsValid)
            {
                return;
            }

            var body = constructor.Body;

            if (body == null)
            {
                return;
            }

            this.TypeMember = constructor;
            this.Body       = body;

            this.AppliedReturn  = CodeAnnotationAttribute.Undefined;
            this.ExpectedReturn = CodeAnnotationAttribute.Undefined;

            this.BuildParameters(constructor, false);
            this.BuildAssertions(body);
        }
        /// <summary>
        /// Indicates whether the type of the constructor passed in is a struct.
        /// </summary>
        /// <param name="constructorDeclaration">
        /// The constructor of the type to check.
        /// </param>
        /// <returns>
        /// True if containing type is a struct.
        /// </returns>
        public static bool IsContainingTypeAStruct(IConstructorDeclaration constructorDeclaration)
        {
            ICSharpTypeDeclaration typeDeclaration = constructorDeclaration.GetContainingTypeDeclaration();

            return typeDeclaration is IStructDeclaration;
        }
        private void ProcessFunctionDeclaration(IConstructorDeclaration constructorDeclaration)
        {
            var constructorParams = constructorDeclaration.ParameterDeclarations;

            var interfaceCount = constructorParams.Count(regularParameterDeclaration => regularParameterDeclaration.DeclaredElement.Type.IsInterfaceType());

            if (interfaceCount > _maxParams)
            {
            string message = Stringtable.TooManyArgumentsWarning;
            var warning = new InjectionHappyDetectorWarning(message);
            _highlights.Add(new HighlightingInfo(constructorDeclaration.GetNameDocumentRange(), warning));
            }
        }
 public override void VisitConstructorDeclaration(IConstructorDeclaration constructorDeclaration, IHighlightingConsumer context)
 {
     tooManyDependenciesCheck.ExecuteIfEnabled(constructorDeclaration, context);
 }
        /// <summary>
        /// Creates a new summary string for this constructor.
        /// </summary>
        /// <param name="constructorDeclaration">
        /// The constructor to produce the summary for.
        /// </param>
        /// <returns>
        /// A string of the constructor summary text.
        /// </returns>
        public static string CreateSummaryForConstructorDeclaration(IConstructorDeclaration constructorDeclaration)
        {
            DeclarationHeader declarationHeader = new DeclarationHeader(constructorDeclaration);

            if (declarationHeader.IsInherited)
            {
                return declarationHeader.XmlNode.InnerXml;
            }

            IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, constructorDeclaration.GetSolution());
            if (!settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.InsertTextIntoDocumentation))
            {
                return string.Empty;
            }

            bool parentIsStruct = IsContainingTypeAStruct(constructorDeclaration);

            string structOrClass = parentIsStruct ? "struct" : "class";

            string xmlWeShouldInsert;

            if (constructorDeclaration.IsStatic)
            {
                xmlWeShouldInsert = string.Format(CultureInfo.InvariantCulture, HeaderSummaryForStaticConstructorXml, constructorDeclaration.DeclaredName, structOrClass);
            }
            else if (constructorDeclaration.GetAccessRights() == AccessRights.PRIVATE && constructorDeclaration.ParameterDeclarations.Count == 0)
            {
                xmlWeShouldInsert = string.Format(
                    CultureInfo.InvariantCulture, HeaderSummaryForPrivateInstanceConstructorXml, constructorDeclaration.DeclaredName, structOrClass);
            }
            else
            {
                string constructorDescriptionText = CreateConstructorDescriptionText(constructorDeclaration, true);

                xmlWeShouldInsert = string.Format(CultureInfo.InvariantCulture, HeaderSummaryForInstanceConstructorXml, constructorDescriptionText, structOrClass);
            }

            return xmlWeShouldInsert;
        }
예제 #16
0
 private static MethodRef CreateRef(IConstructorDeclaration cd)
 {
     return(MethodRef.CreateConstructorReference(cd.GetName(), cd.DeclaredElement, cd));
 }
        private static IParameterDeclaration AddContextParameter(IConstructorDeclaration constructorDeclaration, IDeclaration contextDeclaration)
        {
            var type = SerializationUtilities.StreamingContextTypeName.CreateTypeInContextOf(contextDeclaration);

            return(constructorDeclaration.AddParameterDeclarationBefore(ParameterKind.VALUE, type, "context", null));
        }
예제 #18
0
        public override void VisitConstructorDeclaration(IConstructorDeclaration decl, SST context)
        {
            _cancellationToken.ThrowIfCancellationRequested();

            var nameGen   = new UniqueVariableNameGenerator();
            var exprVisit = new ExpressionVisitor(nameGen, _marker);

            if (decl.DeclaredElement != null)
            {
                var methodName = decl.DeclaredElement.GetName <IMethodName>();

                var sstDecl = new MethodDeclaration
                {
                    Name         = methodName,
                    IsEntryPoint = _entryPoints.Contains(methodName)
                };
                context.Methods.Add(sstDecl);

                if (decl == _marker.AffectedNode)
                {
                    sstDecl.Body.Add(new ExpressionStatement {
                        Expression = new CompletionExpression()
                    });
                }

                if (decl.Initializer != null)
                {
                    var name = Names.UnknownMethod;

                    var substitution = decl.DeclaredElement.IdSubstitution;
                    var resolvedRef  = decl.Initializer.Reference.Resolve();
                    if (resolvedRef.DeclaredElement != null)
                    {
                        name = resolvedRef.DeclaredElement.GetName <IMethodName>(substitution);
                    }

                    var args = Lists.NewList <ISimpleExpression>();
                    foreach (var p in decl.Initializer.Arguments)
                    {
                        var expr = exprVisit.ToSimpleExpression(p.Value, sstDecl.Body);
                        args.Add(expr);
                    }

                    var varId = new VariableReference().Identifier; // default value
                    if (decl.Initializer.Instance != null)
                    {
                        var tokenType = decl.Initializer.Instance.GetTokenType();
                        var isThis    = CSharpTokenType.THIS_KEYWORD == tokenType;
                        var isBase    = CSharpTokenType.BASE_KEYWORD == tokenType;

                        varId = isThis ? "this" : isBase ? "base" : varId;
                    }

                    sstDecl.Body.Add(
                        new ExpressionStatement
                    {
                        Expression = new InvocationExpression
                        {
                            Reference = new VariableReference {
                                Identifier = varId
                            },
                            MethodName = name,
                            Parameters = args
                        }
                    });
                }

                if (!decl.IsAbstract)
                {
                    var bodyVisitor = new BodyVisitor(nameGen, _marker);

                    Execute.AndSupressExceptions(
                        delegate { decl.Accept(bodyVisitor, sstDecl.Body); });
                }
            }
        }
        private IClassDeclaration CreateInitialViewModel(IClassDeclaration classDeclaration, IFieldDeclaration fieldDeclaration, IConstructorDeclaration viewModelConstructor)
        {
            IConstructLanguage language = classDeclaration.Language;
            IClassDeclaration viewModel = language.Class(
                language.Modifiers(Modifiers.Public),
                language.None<IClassTypeParameters>(),
                language.None<ITypeName>(),
                new List<ITypeName>(),
                new List<IDeclaration>() { fieldDeclaration, viewModelConstructor });

            NamingPolicy classesPolicy = viewModel.PrimaryNamingPolicy(classDeclaration.FileModel.UserSettings);
            string viewModelName = classesPolicy.MakeTypeNameUniqueInNamespace(classDeclaration, classDeclaration.Identifier.Name + "ViewModel");

            viewModel.Identifier = language.Identifier(viewModelName);
            return viewModel;
        }
 public override void VisitConstructorDeclaration(IConstructorDeclaration constructorDeclaration, IHighlightingConsumer context)
 {
     tooManyDependenciesCheck.ExecuteIfEnabled(constructorDeclaration, context);
 }
        private void ProcessFunctionDeclaration(IConstructorDeclaration constructorDeclaration)
        {
            var constructorParams = constructorDeclaration.ParameterDeclarations;

            var interfaceCount = constructorParams.Count(regularParameterDeclaration => TypesUtil.IsInterfaceType(regularParameterDeclaration.DeclaredElement.Type));

            if (interfaceCount > _maxParams)
            {
            string message = Stringtable.Warning_TooManyDependencies;
            var warning = new TooManyDependenciesHighlighting(message);
            _highlights.Add(new HighlightingInfo(constructorDeclaration.GetNameDocumentRange(), warning));
            }
        }
 public virtual void AddInitializeOnLoadMethod(IHighlightingConsumer consumer, IConstructorDeclaration constructorDeclaration, string tooltip)
 {
     AddHighlightingWithConfigurableHighlighter(consumer, constructorDeclaration, tooltip);
 }
        /// <summary>
        /// Returns the text that the constructor should have from the containing type declaration with either with 'less than' and 'greater than' signs escaped or not.
        /// </summary>
        /// <param name="constructorDeclaration">
        /// The constructor to use.
        /// </param>
        /// <param name="encodeHtmlTags">
        /// If True then <see cref="ITypeParameterOfTypeDeclaration"/> will have {} instead of &lt; and &gt;.
        /// </param>
        /// <returns>
        /// A string of the text.
        /// </returns>
        public static string CreateConstructorDescriptionText(IConstructorDeclaration constructorDeclaration, bool encodeHtmlTags)
        {
            ICSharpTypeDeclaration containingTypeDeclaration = constructorDeclaration.GetContainingTypeDeclaration();
            string newName = constructorDeclaration.DeclaredName;
            if (containingTypeDeclaration.TypeParameters.Count > 0)
            {
                newName += encodeHtmlTags ? "{" : "<";
                for (int i = 0; i < containingTypeDeclaration.TypeParameters.Count; i++)
                {
                    ITypeParameterOfTypeDeclaration parameterDeclaration = containingTypeDeclaration.TypeParameters[i];
                    newName += parameterDeclaration.DeclaredName;
                    if (i < containingTypeDeclaration.TypeParameters.Count - 1)
                    {
                        newName += ",";
                    }
                }

                newName += encodeHtmlTags ? "}" : ">";
            }

            return newName;
        }
 /// <summary>
 /// Visits the constructor declaration.
 /// </summary>
 /// <param name="constructorDeclaration">The constructor declaration.</param>
 /// <param name="consumer">The consumer.</param>
 /// <returns></returns>
 public override object VisitConstructorDeclaration(IConstructorDeclaration constructorDeclaration, IHighlightingConsumer consumer)
 {
     this.VisitTypeMember(constructorDeclaration, consumer);
       return base.VisitConstructorDeclaration(constructorDeclaration, consumer);
 }
        /// <summary>
        /// Creates a new summary string for this constructor.
        /// </summary>
        /// <param name="constructorDeclaration">
        /// The constructor to produce the summary for.
        /// </param>
        /// <returns>
        /// A string of the constructor summary text.
        /// </returns>
        public static string CreateSummaryForConstructorDeclaration(IConstructorDeclaration constructorDeclaration)
        {
            DeclarationHeader declarationHeader = new DeclarationHeader(constructorDeclaration);

            if (declarationHeader.IsInherited)
            {
                return declarationHeader.XmlNode.InnerXml;
            }

            if (!StyleCopOptions.Instance.InsertTextIntoDocumentation)
            {
                return string.Empty;
            }

            bool parentIsStruct = Utils.IsContainingTypeAStruct(constructorDeclaration);

            string structOrClass = parentIsStruct ? "struct" : "class";

            string xmlWeShouldInsert;

            if (constructorDeclaration.IsStatic)
            {
                xmlWeShouldInsert = string.Format(CultureInfo.InvariantCulture, HeaderSummaryForStaticConstructorXml, constructorDeclaration.DeclaredName, structOrClass);
            }
            else if (constructorDeclaration.GetAccessRights() == AccessRights.PRIVATE && constructorDeclaration.ParameterDeclarations.Count == 0)
            {
                xmlWeShouldInsert = string.Format(
                    CultureInfo.InvariantCulture, HeaderSummaryForPrivateInstanceConstructorXml, constructorDeclaration.DeclaredName, structOrClass);
            }
            else
            {
                string constructorDescriptionText = Utils.CreateConstructorDescriptionText(constructorDeclaration, true);

                xmlWeShouldInsert = string.Format(CultureInfo.InvariantCulture, HeaderSummaryForInstanceConstructorXml, constructorDescriptionText, structOrClass);
            }

            return xmlWeShouldInsert;
        }
예제 #26
0
 protected override void InitializeForCodeGeneration()
 {
     base.InitializeForCodeGeneration();
     NodeType = NodeType.New;
     Constructor = default(IConstructorDeclaration);
     Arguments = new FormulaCollection<Formula>();
 }
        /// <summary>
        /// Ensures that the constructor documentation starts with the standard text summary. 
        /// </summary>
        /// <remarks>
        /// Keeps the existing comment, but prepends the standard text.
        /// </remarks>
        /// <param name="constructorDeclaration">
        /// The destructor <see cref="IDeclaration"/>.
        /// </param>
        public void EnsureConstructorSummaryDocBeginsWithStandardText(IConstructorDeclaration constructorDeclaration)
        {
            if (constructorDeclaration == null)
            {
                return;
            }

            IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, constructorDeclaration.GetSolution());
            if (!settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.InsertTextIntoDocumentation))
            {
                return;
            }

            DeclarationHeader declarationHeader = new DeclarationHeader(constructorDeclaration);

            if (declarationHeader.IsMissing || declarationHeader.IsInherited || !declarationHeader.HasSummary)
            {
                return;
            }

            string existingSummaryText = declarationHeader.SummaryXmlNode.InnerXml;

            bool parentIsStruct = Utils.IsContainingTypeAStruct(constructorDeclaration);

            int constructorParameterCount = constructorDeclaration.ParameterDeclarations.Count;

            string xmlComment = Utils.GetTextFromDeclarationHeader(declarationHeader.XmlNode);
            string structOrClass = parentIsStruct ? CachedCodeStrings.StructText : CachedCodeStrings.ClassText;
            string textWeShouldStartWith;

            if (constructorDeclaration.IsStatic)
            {
                textWeShouldStartWith = string.Format(
                    CultureInfo.InvariantCulture, CachedCodeStrings.HeaderSummaryForStaticConstructor, constructorDeclaration.DeclaredName, structOrClass);
            }
            else if (constructorDeclaration.GetAccessRights() == AccessRights.PRIVATE && constructorParameterCount == 0)
            {
                textWeShouldStartWith = string.Format(
                    CultureInfo.InvariantCulture, CachedCodeStrings.HeaderSummaryForPrivateInstanceConstructor, constructorDeclaration.DeclaredName, structOrClass);
            }
            else
            {
                string constructorDescriptionText = Utils.CreateConstructorDescriptionText(constructorDeclaration, true);
                textWeShouldStartWith = string.Format(
                    CultureInfo.InvariantCulture, CachedCodeStrings.HeaderSummaryForInstanceConstructor, constructorDescriptionText, structOrClass);
            }

            if (constructorDeclaration.IsStatic)
            {
                string docStd = string.Format("Initializes the {0} class.", constructorDeclaration.DeclaredName);
                if (xmlComment == docStd)
                {
                    existingSummaryText = string.Empty;
                }
            }

            if (!xmlComment.StartsWith(textWeShouldStartWith, StringComparison.Ordinal))
            {
                string newSummaryText = Utils.CreateSummaryForConstructorDeclaration(constructorDeclaration);

                declarationHeader.SummaryXmlNode.InnerXml = newSummaryText + " " + existingSummaryText;

                declarationHeader.Update();
            }
        }
예제 #28
0
 public static NewFormula New(IConstructorDeclaration constructor, ReadOnlyCollection<Formula> arguments)
 {
     return new NewFormula(constructor, arguments, null);
 }
예제 #29
0
        private IClassDeclaration CreateInitialViewModel(IClassDeclaration classDeclaration, IFieldDeclaration fieldDeclaration, IConstructorDeclaration viewModelConstructor)
        {
            IConstructLanguage language  = classDeclaration.Language;
            IClassDeclaration  viewModel = language.Class(
                language.Modifiers(Modifiers.Public),
                language.None <IClassTypeParameters>(),
                language.None <ITypeName>(),
                new List <ITypeName>(),
                new List <IDeclaration>()
            {
                fieldDeclaration, viewModelConstructor
            });

            NamingPolicy classesPolicy = viewModel.PrimaryNamingPolicy(classDeclaration.FileModel.UserSettings);
            string       viewModelName = classesPolicy.MakeTypeNameUniqueInNamespace(classDeclaration, classDeclaration.Identifier.Name + "ViewModel");

            viewModel.Identifier = language.Identifier(viewModelName);
            return(viewModel);
        }
예제 #30
0
 public static MethodRef CreateConstructorReference(IMethodName methodName, IConstructor ctor, IConstructorDeclaration ctorDecl)
 {
     return(new MethodRef
     {
         Name = methodName,
         Constructor = ctor,
         ConstructorDeclaration = ctorDecl,
         IsAssemblyReference = false
     });
 }
예제 #31
0
 public ConstructorDeclarationCompiler(IConstructorDeclaration constructorDeclaration,
                                       AbstractILCompilerParams @params) : base(@params)
 {
     myConstructorDeclaration = constructorDeclaration;
     MyParams.CreateMethod(constructorDeclaration.DeclaredElement);
 }
예제 #32
0
 public virtual void AddInitializeOnLoadMethod(IHighlightingConsumer consumer,
                                               IConstructorDeclaration constructorDeclaration, string tooltip, string displayName, DaemonProcessKind kind)
 {
     AddHighlightingWithConfigurableHighlighter(consumer, constructorDeclaration, tooltip, displayName, kind);
 }