Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
 public ShellCodeMethod AddFunction(string Name, vsCMFunction Kind, object Type, object Position,
                                    vsCMAccess Access, object Location)
 {
     return
         (new ShellCodeMethod(
              _codeClass.AddFunction(Name, Kind, Type, Position, Access, Location) as CodeFunction2));
 }
Exemplo n.º 3
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);
        }
 protected override object NewMethod(NewCodeElementItemParams newItemParams, string path)
 {
     return(_codeClass.AddFunction(path,
                                   newItemParams.FunctionKind,
                                   newItemParams.MemberType,
                                   newItemParams.Position,
                                   newItemParams.AccessKind,
                                   Type.Missing));
 }
Exemplo n.º 5
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);
            }
Exemplo n.º 6
0
        /// <summary>
        /// Add function to the specified class with the given body.
        /// </summary>
        /// <param name="p_securedClass"></param>
        /// <param name="p_functionName"></param>
        /// <param name="p_functionBody"></param>
        private static void AddFunction(CodeClass2 p_securedClass, string p_functionName, string p_functionBody)
        {
            // Add a method with a parameter to the class.
            CodeFunction2 testFunction = (CodeFunction2)p_securedClass.AddFunction(p_functionName, vsCMFunction.vsCMFunctionFunction, "void", -1, vsCMAccess.vsCMAccessPublic, null);

            testFunction.AddAttribute("TestMethod", string.Empty);

            var startPoint = testFunction.GetStartPoint(vsCMPart.vsCMPartBody);
            var endPoint   = testFunction.GetEndPoint(vsCMPart.vsCMPartBody);
            var editPoint  = startPoint.CreateEditPoint() as EditPoint2;

            // Clear the default generated body
            editPoint.Delete(endPoint);
            editPoint.Indent(null, 1);

            // Write the body of the function
            editPoint.Insert(p_functionBody);
            editPoint.InsertNewLine();

            endPoint = testFunction.GetEndPoint(vsCMPart.vsCMPartBody);
            editPoint.SmartFormat(endPoint);
        }
        // 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 static void Excute(CodeClass2 classElement, Dictionary<string, CodeClass2> dic, TyrantVSPackage package)
        {
            if (classElement == null)
                return;

            //var classCode = classElement.StartPoint.CreateEditPoint().GetText(classElement.GetEndPoint());
            var validBases = new List<CodeElement>();
            foreach (CodeElement baseClass in classElement.Bases)
            {
                if (!baseClass.FullName.StartsWith("Tyrant.GameCore.Message<") && baseClass.FullName != typeof(object).FullName)
                    validBases.Add(baseClass);
            }
            var validInterfaces = new List<CodeElement>();
            foreach (CodeElement implementedInterface in classElement.ImplementedInterfaces)
            {
                if (implementedInterface.FullName != "Tyrant.GameCore.ISerializable")
                {
                    validInterfaces.Add(implementedInterface);
                    break;
                }
            }

            var baseCollection = classElement.Collection;
            string name = classElement.Name;
            classElement.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint().ReplaceText(classElement.GetStartPoint(vsCMPart.vsCMPartBody), $"public partial class {name}{Environment.NewLine}{{{Environment.NewLine}", (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
            classElement = baseCollection.Item(name) as CodeClass2;
            classElement.AddImplementedInterface("Tyrant.GameCore.ISerializable");
            foreach (CodeElement baseClass in validBases)
            {
                try
                {
                    classElement.AddBase(baseClass, -1);
                }
                catch { }
            }
            foreach (CodeElement implementedInterface in validInterfaces)
            {
                try
                {
                    classElement.AddImplementedInterface(implementedInterface, -1);
                }
                catch { }
            }

            CommunicationCodeHelper.RemoveMembers(classElement);

            CodeFunction2 serializeMethod = null, deserializeMethod = null;
            try
            {
                classElement.AddFunction("Tyrant.GameCore.ISerializable.Serialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessDefault);
            }
            catch { }
            serializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Serialize") as CodeFunction2;
            try
            {
                serializeMethod.AddParameter("dest", typeof(ProtoBuf.ProtoWriter).FullName);
            }
            catch
            {
                serializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Serialize") as CodeFunction2;
            }
            try
            {
                classElement.AddFunction("Tyrant.GameCore.ISerializable.Deserialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessDefault);
            }
            catch { }
            deserializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Deserialize") as CodeFunction2;
            try
            {
                deserializeMethod.AddParameter("source", typeof(ProtoBuf.ProtoReader).FullName);
            }
            catch
            {
                deserializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Deserialize") as CodeFunction2;
            }
            CommunicationCodeHelper.GenerateSerializaCode(classElement, serializeMethod, deserializeMethod, dic, package/*, "dest", "source"*/);
        }
        public static void Excute(CodeClass2 classElement, Dictionary<string, CodeClass2> dic, TyrantVSPackage package)
        {
            if (classElement == null)
                return;

            string callBackGenericParamaName = null;
            foreach (CodeElement baseClass in classElement.Bases)
            {
                string baseFullName = baseClass.FullName;
                if (baseFullName.StartsWith("Tyrant.GameCore.Message<"))
                {
                    Stack<int> ind = new Stack<int>();
                    int thirdGenericParama = -1, find = 0;
                    int first = baseFullName.IndexOf('<');
                    for (int i = first + 1; i < baseFullName.Length; ++i)
                    {
                        char c = baseFullName[i];
                        if (c == '<')
                            ind.Push(i);
                        else if (c == ',')
                        {
                            if (ind.Count <= 0)
                            {
                                ++find;
                                if (find == 2)
                                    thirdGenericParama = i + 1;
                            }
                        }
                        else if (c == '>')
                        {
                            if (ind.Count > 0)
                                ind.Pop();
                            else
                            {
                                if (thirdGenericParama > 0 && thirdGenericParama < baseFullName.Length)
                                    callBackGenericParamaName = baseFullName.Substring(thirdGenericParama, i - 1 - thirdGenericParama).Trim();
                                break;
                            }
                        }
                    }
                }
            }

            var validInterfaces = new List<CodeElement>();
            foreach (CodeElement implementedInterface in classElement.ImplementedInterfaces)
            {
                if (implementedInterface.FullName != "Tyrant.GameCore.ISerializable")
                {
                    validInterfaces.Add(implementedInterface);
                    break;
                }
            }

            var name = classElement.Name;
            var fullName = classElement.FullName;
            var collection = classElement.Collection;
            if (!string.IsNullOrEmpty(callBackGenericParamaName))
            {
                int first = callBackGenericParamaName.IndexOf('<');
                if (first >= 0)
                    callBackGenericParamaName = callBackGenericParamaName.Substring(0, first);
                callBackGenericParamaName = $", {callBackGenericParamaName}<TUser>";
            }
            else
                callBackGenericParamaName = "";
            classElement.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint().ReplaceText(classElement.GetStartPoint(vsCMPart.vsCMPartBody), $"public partial class {name}<TUser> : Tyrant.GameCore.Message<TUser, {name}<TUser>{callBackGenericParamaName}> where TUser : Tyrant.GameCore.IUser{Environment.NewLine}{{{Environment.NewLine}", (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
            classElement = collection.Item(name) as CodeClass2;

            foreach (CodeElement implementedInterface in validInterfaces)
            {
                try
                {
                    classElement.AddImplementedInterface(implementedInterface, -1);
                }
                catch { }
            }

            CommunicationCodeHelper.RemoveMembers(classElement);

            CodeFunction2 serializeMethod = null, deserializeMethod = null;
            try
            {
                classElement.AddFunction("Serialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessProtected);
            }
            catch { }
            serializeMethod = classElement.Members.Item("Serialize") as CodeFunction2;
            if (serializeMethod == null)
            {
                return;
            }
            serializeMethod.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
            try
            {
                serializeMethod.AddParameter("dest", typeof(ProtoBuf.ProtoWriter).FullName);
            }
            catch
            {
                serializeMethod = classElement.Members.Item("Serialize") as CodeFunction2;
            }
            try
            {
                classElement.AddFunction("Deserialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessProtected);
            }
            catch { }
            deserializeMethod = classElement.Members.Item("Deserialize") as CodeFunction2;
            if (deserializeMethod == null)
            {
                return;
            }
            deserializeMethod.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
            try
            {
                deserializeMethod.AddParameter("source", typeof(ProtoBuf.ProtoReader).FullName);
            }
            catch
            {
                deserializeMethod = classElement.Members.Item("Deserialize") as CodeFunction2;
            }
            CommunicationCodeHelper.GenerateSerializaCode(classElement, serializeMethod, deserializeMethod, dic, package/*, "dest", "source"*/);
        }