Example #1
0
        private void AddMethodToClass(CodeElements codeElements, string className, string methodCode)
        {
            CodeClass featureReceiverClass = GetClassByName(codeElements, className);

            //add the method to the class
            if (featureReceiverClass != null)
            {
                EditPoint2 editPoint = (EditPoint2)featureReceiverClass.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

                StringReader reader = new StringReader(methodCode);
                string       line   = reader.ReadLine();
                while (line != null)
                {
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 2);
                    editPoint.Insert(line);
                    line = reader.ReadLine();
                }

                editPoint.InsertNewLine(1);

                Helpers.LogMessage(featureReceiverClass.DTE, featureReceiverClass.DTE, Helpers.GetFullPathOfProjectItem(featureReceiverClass.ProjectItem) + ": Added new method");
            }
            else
            {
                throw new Exception("Class " + className + " not found");
            }
        }
        // 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; //");
        }
Example #3
0
        private void AddUsingStatement(FileCodeModel model, CodeElements codeElements, string usingStatement)
        {
            bool       usingStatementFound = false;
            CodeImport lastCodeElement     = null;

            foreach (CodeElement codeElement in codeElements)
            {
                if (codeElement.Kind == vsCMElement.vsCMElementImportStmt)
                {
                    CodeImport codeImport = codeElement as CodeImport;
                    if (codeImport.Namespace == usingStatement)
                    {
                        usingStatementFound = true;
                    }
                    lastCodeElement = codeImport;
                }

                AddUsingStatement(model, codeElement.Children, usingStatement);
            }

            if (!usingStatementFound)
            {
                if (lastCodeElement != null)
                {
                    //FileCodeModel2 model2 = model as FileCodeModel2;
                    //model2.AddImport(usingStatement);

                    EditPoint2 editPoint = (EditPoint2)lastCodeElement.GetEndPoint().CreateEditPoint();
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 1);
                    editPoint.Insert("using " + usingStatement + ";");

                    Helpers.LogMessage(model.DTE, model.DTE, Helpers.GetFullPathOfProjectItem(lastCodeElement.ProjectItem) + ": Added using statement '" + usingStatement + "'");
                }
            }
        }
Example #4
0
        private bool AddMethodCall(CodeElements codeElements, string className, string targetMethodName, string methodCall)
        {
            CodeClass    featureReceiverClass = GetClassByName(codeElements, className);
            CodeFunction function             = null;
            bool         result = false;

            if (featureReceiverClass != null)
            {
                //try to find the targetMethodName and if found then add the methodCall at the end
                foreach (CodeElement codeElement in featureReceiverClass.Members)
                {
                    if (codeElement.Kind == vsCMElement.vsCMElementFunction)
                    {
                        if (codeElement.Name == targetMethodName)
                        {
                            function = codeElement as CodeFunction;
                        }
                    }
                }

                if (function == null)
                {
                    //method not found (SPFeatureReceiverProperties properties)
                    function = featureReceiverClass.AddFunction(targetMethodName, vsCMFunction.vsCMFunctionFunction, "void", 0, vsCMAccess.vsCMAccessPublic, null);
                    CodeFunction2 function2 = function as CodeFunction2;
                    function2.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
                    function.AddParameter("properties", "SPFeatureReceiverProperties", -1);
                    function.AddAttribute("SharePointPermission", "(SecurityAction.LinkDemand, ObjectModel = true)");
                    Helpers.LogMessage(function.DTE, function.DTE, Helpers.GetFullPathOfProjectItem(function.ProjectItem) + ": Added method '" + methodCall + "'");
                }

                if (function != null)
                {
                    EditPoint2 editPoint = (EditPoint2)function.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

                    //get indent of editpoint (at the end of the function
                    int charsBefore = editPoint.AbsoluteCharOffset;
                    int lineAdded   = editPoint.Line;

                    if (!methodCall.StartsWith("this."))
                    {
                        //add this. to be StyleCop conform
                        methodCall = "this." + methodCall;
                    }

                    editPoint.InsertNewLine(1);
                    editPoint.Insert("// Call to method " + methodCall);
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 2);
                    editPoint.Insert(methodCall);
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 2);

                    Helpers.LogMessage(function.DTE, function.DTE, Helpers.GetFullPathOfProjectItem(function.ProjectItem) + "(" + lineAdded.ToString() + ",0): Added code to method '" + methodCall + "'");

                    result = true;
                }
            }
            else
            {
                throw new Exception("Class " + className + " not found");
            }
            return(result);
        }