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"); } }
public static void Run(DTE dte, VsError selectedError) { selectedError.Navigate(); EditPoint2 ep = ErrorUtilities.GetEditPoint(dte); ep.LineDown(); ep.StartOfLine(); ep.InsertNewLine(); }
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 static void Run(DTE dte, VsError selectedError) { selectedError.Navigate(); EditPoint2 editPoint = ErrorUtilities.GetEditPoint(dte); editPoint.StartOfDocument(); string fileName = selectedError.FileName.Substring( selectedError.FileName.LastIndexOf(@"\") + 1, selectedError.FileName.Length - (selectedError.FileName.LastIndexOf(@"\") + 1)); editPoint.InsertNewLine(); editPoint.StartOfDocument(); editPoint.Insert( string.Format( @"//----------------------------------------------------------------------- // <copyright file=""{0}"" company=""{1}""> // {2} // </copyright> //-----------------------------------------------------------------------", fileName, StyleRepair.Properties.StyleRepair.Default.CompanyName, StyleRepair.Properties.StyleRepair.Default.CopyrightMessage)); }
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 + "'"); } } }
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); }