/// <summary> /// Creates a constructor from a method element. /// </summary> /// <param name="methodElement">The method element.</param> /// <returns>A constructor element.</returns> private static ConstructorElement CreateConstructor(MethodElement methodElement) { ConstructorElement constructor = new ConstructorElement(); constructor.Name = methodElement.Name; constructor.Access = methodElement.Access; constructor.MemberModifiers = methodElement.MemberModifiers; constructor.Parameters = methodElement.Parameters; constructor.BodyText = methodElement.BodyText; return constructor; }
/// <summary> /// Processes a constructor element. /// </summary> /// <param name="element">Constructor code element.</param> public abstract void VisitConstructorElement(ConstructorElement element);
/// <summary> /// Processes a constructor element. /// </summary> /// <param name="element">Constructor code element.</param> public override void VisitConstructorElement(ConstructorElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); WriteAccess(element.Access); WriteMemberAttributes( element.MemberModifiers, element[VBExtendedProperties.Overloads] is bool && (bool)element[VBExtendedProperties.Overloads]); Writer.Write(VBKeyword.Sub); Writer.Write(' '); Writer.Write(element.Name); WriteParameterList(element.Parameters); if (!string.IsNullOrEmpty(element.Reference)) { TabCount++; Writer.WriteLine(); WriteIndented(element.Reference); TabCount--; } WriteBody(element); }
public void InsertByElementTypeTest() { SortBy sortBy = new SortBy(); sortBy.By = ElementAttributeType.ElementType; sortBy.Direction = SortDirection.Ascending; SortedInserter sortedInserter = new SortedInserter(ElementType.NotSpecified, sortBy); // // Create a parent element // RegionElement regionElement = new RegionElement(); Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children."); // // Insert an element with a middle access. // ConstructorElement constructor = new ConstructorElement(); constructor.Name = "SomeClass"; constructor.Access = CodeAccess.Public; sortedInserter.InsertElement(regionElement, constructor); Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent."); Assert.AreEqual(0, regionElement.Children.IndexOf(constructor), "Element was not inserted at the correct index."); // // Insert an element that should be sorted toward the end // MethodElement methodElement = new MethodElement(); methodElement.Name = "SomeMethod"; methodElement.Access = CodeAccess.Public; sortedInserter.InsertElement(regionElement, methodElement); Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent."); Assert.AreEqual(0, regionElement.Children.IndexOf(constructor), "Element is not at the correct index."); Assert.AreEqual(1, regionElement.Children.IndexOf(methodElement), "Element is not at the correct index."); // // Insert an element that should be sorted toward the beginning // FieldElement fieldElement = new FieldElement(); fieldElement.Name = "someField"; fieldElement.Access = CodeAccess.Private; sortedInserter.InsertElement(regionElement, fieldElement); Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent."); Assert.AreEqual(0, regionElement.Children.IndexOf(fieldElement), "Element is not at the correct index."); Assert.AreEqual(1, regionElement.Children.IndexOf(constructor), "Element is not at the correct index."); Assert.AreEqual(2, regionElement.Children.IndexOf(methodElement), "Element is not at the correct index."); }
/// <summary> /// Processes a constructor element. /// </summary> /// <param name="element">Constructor code element.</param> public override void VisitConstructorElement(ConstructorElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); WriteAccess(element.Access); WriteMemberAttributes(element.MemberModifiers); Writer.Write(element.Name); WriteParameterList(element.Parameters); Writer.WriteLine(); if (element.Reference != null) { TabCount++; WriteIndentedLine( string.Format( CultureInfo.InvariantCulture, "{0} {1}", CSharpSymbol.TypeImplements, element.Reference)); TabCount--; } WriteBody(element); }
/// <summary> /// Parses a constructor. /// </summary> /// <param name="memberName">Member name.</param> /// <param name="access">Member accessibility.</param> /// <param name="memberAttributes">Member attributes.</param> /// <returns>Constructor code element.</returns> private ConstructorElement ParseConstructor(string memberName, CodeAccess access, MemberModifiers memberAttributes) { ConstructorElement constructor = new ConstructorElement(); constructor.Name = memberName; constructor.Access = access; constructor.MemberModifiers = memberAttributes; constructor.Parameters = this.ParseParameters(); EatWhiteSpace(); List<ICommentElement> extraComments = new List<ICommentElement>(); extraComments.AddRange(ParseComments()); EatWhiteSpace(); bool hasReference = TryReadChar(CSharpSymbol.TypeImplements); if (hasReference) { EatWhiteSpace(); extraComments.AddRange(ParseComments()); StringBuilder referenceBuilder = new StringBuilder(DefaultWordLength); EatWhiteSpace(); referenceBuilder.Append(CaptureWord()); EatWhiteSpace(); string referenceParams = ParseNestedText(CSharpSymbol.BeginParameterList, CSharpSymbol.EndParameterList, true, true); referenceBuilder.Append(CSharpSymbol.BeginParameterList); referenceBuilder.Append(referenceParams); referenceBuilder.Append(CSharpSymbol.EndParameterList); constructor.Reference = referenceBuilder.ToString(); } constructor.BodyText = this.ParseBlock(true, constructor); foreach (ICommentElement comment in extraComments) { constructor.AddHeaderComment(comment); } return constructor; }