public void initialize_constructor() { TextSelection selection = studio.ActiveDocument.Selection as TextSelection; CodeClass2 class_object = (CodeClass2)selection.ActivePoint.get_CodeElement(vsCMElement.vsCMElementClass); CodeFunction2 constructor = class_object.AddFunction(class_object.Name, vsCMFunction.vsCMFunctionConstructor, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessPublic, 0) as CodeFunction2; string text = ""; foreach (CodeElement2 member in class_object.Members) { if (member.Kind == vsCMElement.vsCMElementVariable) { CodeVariable2 variable = member as CodeVariable2; CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2; text += "\r\n" + variable.Name + " = " + parameter.Name + ";"; } else if (member.Kind == vsCMElement.vsCMElementProperty) { var variable = member as CodeProperty; // CodeTypeRef new_type = CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2; text += "\r\n" + variable.Name + " = " + parameter.Name + ";"; } } EditPoint2 point = constructor.EndPoint.CreateEditPoint() as EditPoint2; point.LineUp(1); point.Insert(text); selection.MoveToPoint(constructor.StartPoint, false); selection.MoveToPoint(constructor.EndPoint, true); selection.SmartFormat(); selection.MoveToPoint(point, false); }
/// <summary> /// Create the text for a property using the property name and the accessors /// </summary> /// <param name="elem">Property we are updating</param> /// <param name="ep">The point we are inserting the text</param> private static void ProcessPropertyUpdate(CodeProperty elem, EditPoint2 ep) { string descriptionText = string.Empty; if (elem.Getter != null) { switch (elem.Getter.Access) { case vsCMAccess.vsCMAccessPrivate: break; default: descriptionText = "Gets"; break; } } if (elem.Setter != null) { switch (elem.Setter.Access) { case vsCMAccess.vsCMAccessPrivate: break; default: if (descriptionText.Length > 0) { descriptionText += " or sets"; } else { descriptionText = "Sets"; } break; } } descriptionText += " " + elem.Name; TextPoint tp = elem.GetStartPoint(); ep.MoveToPoint(tp); string spacer = string.Empty; for (int i = 0; i < tp.LineCharOffset - 1; i++) { spacer += " "; } ep.LineUp(); ep.Insert(Environment.NewLine); ep.Insert(spacer + "/// <summary>"); ep.Insert(Environment.NewLine + spacer + string.Format("/// {0}", descriptionText)); ep.Insert(Environment.NewLine + spacer + "/// </summary>"); ep.Insert(spacer); }