/// <summary> /// Adds the property. /// </summary> /// <param name="codeClass">The code class.</param> /// <param name="var">The var.</param> /// <returns></returns> public static CodeProperty AddProperty(CodeClass codeClass, CodeVariable var) { CodeProperty prop = null; try { prop = codeClass.AddProperty( FormatPropertyName(var.Name), FormatPropertyName(var.Name), var.Type.AsFullName, -1, vsCMAccess.vsCMAccessPublic, null); EditPoint editPoint = prop.Getter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); //Delete return default(int); added by codeClass.AddProperty editPoint.Delete(editPoint.LineLength); editPoint.Indent(null, 4); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "return {0};", var.Name)); editPoint = prop.Setter.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); editPoint.Indent(null, 1); editPoint.Insert(string.Format(CultureInfo.InvariantCulture, "{0} = value;", var.Name)); editPoint.SmartFormat(editPoint); return(prop); } catch { //Property already exists return(null); } }
/// <summary> /// Sorts all using statements in ascending order, with System using statements on top. /// </summary> /// <param name="usingStatementsItems">List of using Statement codeItems</param> /// <param name="namespaceItems">List of namespace codeItems</param> internal void MoveUsingStatementsWithinNamespace(List <CodeItemUsingStatement> usingStatementsItems, List <CodeItemNamespace> namespaceItems) { if (namespaceItems.Count != 1) { //We return back as is, if multiple namespaces are found. return; } CodeItemNamespace theOnlyNamespace = namespaceItems.First(); EditPoint namespaceInsertCursor = theOnlyNamespace.StartPoint; // Setting the start point where we will start inserting using statements. namespaceInsertCursor.LineDown(); namespaceInsertCursor.CharRight(); namespaceInsertCursor.Insert(Environment.NewLine); //Sort the using code items in ascending string order, with system usings on top. usingStatementsItems.Sort((usingStatement1Item, usingStatement2Item) => { string textOfUsingStatement1 = usingStatement1Item.StartPoint.GetText(usingStatement1Item.EndPoint); string textOfUsingStatement2 = usingStatement2Item.StartPoint.GetText(usingStatement2Item.EndPoint); var referenceNameOfStatement1 = ExtractUsingStatementReferenceName(textOfUsingStatement1); var referenceNameOfStatement2 = ExtractUsingStatementReferenceName(textOfUsingStatement2); if (IsSystemReference(referenceNameOfStatement1) && !IsSystemReference(referenceNameOfStatement2)) { return(-1); } else if (!IsSystemReference(referenceNameOfStatement1) && IsSystemReference(referenceNameOfStatement2)) { return(1); } else { return(string.Compare(referenceNameOfStatement1, referenceNameOfStatement2)); } }); foreach (var usingStatement in usingStatementsItems) { var startPoint = usingStatement.StartPoint; var endPoint = usingStatement.EndPoint; string text = startPoint.GetText(usingStatement.EndPoint); startPoint.Delete(usingStatement.EndPoint); namespaceInsertCursor.Insert(text); namespaceInsertCursor.Indent(Count: 1); namespaceInsertCursor.Insert(Environment.NewLine); } }
// add a class to the given namespace private void AddClassToNamespace(CodeNamespace ns) { // add a class CodeClass chess = (CodeClass)ns.AddClass("Chess", -1, null, null, vsCMAccess.vsCMAccessPublic); // add a function with a parameter and a comment CodeFunction move = (CodeFunction)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 EditPoint editPoint = (EditPoint)move.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint(); editPoint.Indent(null, 0); editPoint.Insert("int a = 1;"); editPoint.Insert(Environment.NewLine); editPoint.Indent(null, 3); editPoint.Insert("int b = 3;"); editPoint.Insert(Environment.NewLine); editPoint.Indent(null, 3); editPoint.Insert("return a + b; //"); }
/// <summary> /// Helper method to modify commands.xml. /// </summary> /// <param name="projectItem"></param> private void ModifyCommandTable(ProjectItem projectItem) { Document activeDoc = projectItem.Document; projectItem.Open(); TextDocument editDoc = (TextDocument)activeDoc.Object("TextDocument"); EditPoint objEditPt = editDoc.CreateEditPoint(); EditPoint objMovePt = editDoc.EndPoint.CreateEditPoint(); objEditPt.StartOfDocument(); activeDoc.ReadOnly = false; objEditPt.FindPattern("</KeyinTable>"); objEditPt.Insert("\n"); objEditPt.LineUp(1); objEditPt.Indent(objEditPt, 1); objEditPt.Insert("<Keyword CommandWord=\"" + FunctionName + "\"></Keyword>"); objEditPt.LineDown(1); objEditPt.Indent(objEditPt, 2); objEditPt.FindPattern("</KeyinHandlers>"); objEditPt.Insert("\n"); objEditPt.LineUp(1); objEditPt.Indent(objEditPt, 1); objEditPt.Insert("<KeyinHandler Keyin=\"" + RootNamespace + " " + FunctionName + "\"\n"); objEditPt.Indent(objEditPt, 4); if (!IsVBProject) { objEditPt.Insert("Function=\"" + RootNamespace + ".KeyinCommands." + FunctionName + "Keyin\"/>"); } else { objEditPt.Insert("Function=\"" + RootNamespace + ".KeyinCommands." + FunctionName + "Keyin\"/>"); } }
/// <summary> /// 处理缩进 /// </summary> /// <param name="Bracket">一对方括号内容</param> /// <param name="CurrentEditLintPoint">当前行的编辑点</param> private static void HandleIndent(IndentSquareBracket Bracket, EditPoint CurrentEditLintPoint) { //把处理点移动到一行的开始位置 CurrentEditLintPoint.StartOfLine(); //是不是最后的右中括号 ] bool IsEndRightBracket = CurrentEditLintPoint.Line == Bracket.End.Line; string LineText = CurrentEditLintPoint.GetText(CurrentEditLintPoint.LineLength); //整行从开头就被注释,则退出 if (LineText.StartsWith("//")) { return; } //如果这行是以#开头,则退出,针对#if之类的 if (LineText.StartsWith("#")) { return; } //剔除这行最前面的制表符 string CurrentLineText = LineText.TrimStart('\t'); //未处理该行的缩进个数 int OldIndentCount = LineText.Length - CurrentLineText.Length; //中括号之间的内容,应该缩进的个数 int ContentIndentCount = Bracket.IndentCount + (IsEndRightBracket ? 0 : 1); //待处理缩进个数 int IndentCountAbs = Math.Abs(OldIndentCount - ContentIndentCount); //如果小于应该缩进的个数,则添加 if (OldIndentCount < ContentIndentCount) { CurrentEditLintPoint.Indent(null, IndentCountAbs); } //否则取消相应个数 else if (OldIndentCount > ContentIndentCount) { CurrentEditLintPoint.Unindent(null, IndentCountAbs); } }