Exemple #1
0
        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);
        }
Exemple #2
0
            private void CreatePageMethod(CodeClass2 classModel, string name)
            {
                ParameterInfo[] parameters = _signature.GetParameters();
                CodeFunction2   method     = classModel.AddFunction(name, vsCMFunction.vsCMFunctionFunction, CreateCodeTypeRefName(_signature.ReturnType), -1, vsCMAccess.vsCMAccessPublic, null);

                method.IsShared = true;
                foreach (ParameterInfo param in parameters)
                {
                    method.AddParameter(param.Name, CreateCodeTypeRefName(param.ParameterType), -1);
                }
                method.AddAttribute(typeof(WebMethodAttribute).FullName, "", -1);
                method.AddAttribute(typeof(ScriptMethodAttribute).FullName, "", -1);
            }
Exemple #3
0
        public static void GenerateConstructor(CodeClass2 codeClass, CodeVariable2[] codeVariables, bool generateComments, vsCMAccess accessModifier)
        {
            CodeGenerator codeGenerator = CreateCodeGenerator(codeClass.Language);


            CodeFunction2 codeFunction = null;

            if (codeClass.Language == CodeModelLanguageConstants.vsCMLanguageCSharp)
            {
                codeFunction = (CodeFunction2)codeClass.AddFunction(codeClass.Name, vsCMFunction.vsCMFunctionConstructor, null, -1, accessModifier, null);
            }
            else if (codeClass.Language == CodeModelLanguageConstants.vsCMLanguageVB)
            {
                codeFunction = (CodeFunction2)codeClass.AddFunction("New", vsCMFunction.vsCMFunctionSub, vsCMTypeRef.vsCMTypeRefVoid, -1, accessModifier, null);
            }

            if (generateComments)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<doc>");
                sb.AppendLine("<summary>");
                sb.AppendLine("</summary>");
                foreach (CodeVariable2 codeVariable in codeVariables)
                {
                    sb.AppendLine(String.Format("<param name=\"{0}\"></param>", codeVariable.Name));
                }
                sb.Append("</doc>");

                codeFunction.DocComment = sb.ToString();
            }

            foreach (CodeVariable2 codeVariable in codeVariables)
            {
                codeFunction.AddParameter(codeVariable.Name, codeVariable.Type.AsString, -1);
            }

            EditPoint2 editPoint = (EditPoint2)codeFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            foreach (CodeVariable2 codeVariable in codeVariables)
            {
                editPoint.Insert(codeGenerator.GenerateAssignStatement(codeVariable.Name, codeVariable.Name));
                editPoint.SmartFormat(editPoint);

                if (Array.IndexOf(codeVariables, codeVariable) < codeVariables.Length - 1)
                {
                    editPoint.InsertNewLine(1);
                }
            }

            editPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, codeFunction.StartPoint);
        }
        // add a class to the given namespace
        private void AddClassToNamespace(CodeNamespace ns)
        {
            // add a class
            CodeClass2 chess = (CodeClass2)ns.AddClass("Chess", -1, null, null, vsCMAccess.vsCMAccessPublic);

            // add a function with a parameter and a comment
            CodeFunction2 move = (CodeFunction2)chess.AddFunction("Move", vsCMFunction.vsCMFunctionFunction, "int", -1, vsCMAccess.vsCMAccessPublic, null);

            move.AddParameter("IsOK", "bool", -1);
            move.Comment = "This is the move function";

            // add some text to the body of the function
            EditPoint2 editPoint = (EditPoint2)move.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            editPoint.Indent(null, 0);
            editPoint.Insert("int a = 1;");
            editPoint.InsertNewLine(1);
            editPoint.Indent(null, 3);
            editPoint.Insert("int b = 3;");
            editPoint.InsertNewLine(2);
            editPoint.Indent(null, 3);
            editPoint.Insert("return a + b; //");
        }
 public ShellCodeParameter AddParameter(string Name, object Type, object Position)
 {
     return(new ShellCodeParameter(_method.AddParameter(Name, Type, Position) as CodeParameter2));
 }
Exemple #6
0
 protected override object NewParameter(NewCodeElementItemParams newItemParams, string path, object value)
 {
     return(_element.AddParameter(path,
                                  newItemParams.MemberType,
                                  newItemParams.Position));
 }