private void BindRegionToTreeView() { treeView1.Nodes.Clear(); VSCodeModel codeModel = new VSCodeModel(); List<RegionElement> regions = codeModel.GetRegionsInClass(classElement); foreach (RegionElement item in regions) { TreeNode node = new TreeNode(item.RegionName); node.Tag = item; RecursionAddChildren(item, node); treeView1.Nodes.Add(node); } }
public void RegionElement(List<CodeElement> needRegionElements, int classIndex, string regionName) { VSTextView textView = new VSTextView(VSTextView.ActiveTextView); VSCodeModel codeModel = new VSCodeModel(); List<TextSpan> methodsBody = new List<TextSpan>(); StringBuilder sb = new StringBuilder(); foreach (CodeElement item in needRegionElements) { //在这里获得的位置是最新的修改后的位置。 CodeElement ce = codeModel.GetPrevElementInCurrentFile(item); if (ce != null) { TextSpan es = new TextSpan(); //这里的行数全部以(0,0)开始 es.iEndLine = item.EndPoint.Line - 1; //当前元素的开始定位其上一个元素的结尾 es.iStartLine = ce.EndPoint.Line; //当前元素是第一个元素,所以他的开始应该是紧接着他父元素 if (ce == item) { CodeElement parentElement = codeModel.GetParentElementInCurrentFile(item); int parentElementStartLine = GetElementBodyStartLine(textView, parentElement); es.iStartLine = parentElementStartLine; } //检查es.iStartLine到item.StartLine之间有没有#region或者#endregion,这些 //东西不能为算在里面,否则粘贴后会破坏现有的region es.iStartLine = CutRegionFlag(textView, es.iStartLine, item.StartPoint.Line); methodsBody.Add(es); sb.Append(textView.GetText(es.iStartLine, 0, es.iEndLine + 1, 0)); } } //开始删除原来的节点 int filedCount = methodsBody.Count; for (int i = filedCount - 1; i >= 0; i--) { textView.DeleteText(methodsBody[i].iStartLine, 0, methodsBody[i].iEndLine + 1, 0); } //插入新的region包围的内容 //首先检查当前是否已经存在给定的regionName,如果存在则不用新建 if (filedCount > 0) { //需要重新获得删除节点后最新的class位置信息 List<CodeElement> classLists = GetClassAndStructInFile(codeModel); int line = GetElementBodyStartLine(textView, classLists[classIndex]); //检查有没有已经同名的region int r = CheckExistRegionName(regionName, classLists[classIndex]); if (r != -1) { line = r; } else { sb.Insert(0, "\t\t#region " + regionName + "\r\n\r\n"); sb.Append("\t\n\t\t#endregion\r\n\r\n"); } textView.InsertText(sb.ToString(), line, 0); } }
private void CleanBlankLine(VSCodeModel codeModel) { VSTextView textView = new VSTextView(VSTextView.ActiveTextView); List<TextSpan> blankLines = codeModel.GetBlankLines(textView, 3); //超过3行空白的一律合并 for (int i = blankLines.Count - 1; i >= 0; i--) { TextSpan emptyRegion = blankLines[i]; textView.DeleteText(emptyRegion.iStartLine, 0, emptyRegion.iEndLine, 0); } }
/// <summary> /// 检查当前文件中是否存在相同的region名字,如果不存在则返回-1,否则返回该region的末尾位置 /// </summary> /// <param name="regionName"></param> /// <param name="classElement"></param> /// <returns></returns> private int CheckExistRegionName(string regionName, CodeElement classElement) { VSCodeModel codeModel = new VSCodeModel(); List<RegionElement> regions = codeModel.GetAllRegionsInClass(classElement); RegionElement re = regions.Where(o => o.RegionName == regionName).FirstOrDefault(); if (re == null) { return -1; } else { return re.EndLine - 1; } }
private static List<CodeElement> GetClassAndStructInFile(VSCodeModel codeModel) { List<CodeElement> classLists = codeModel.GetClassInCurrentFile(); classLists.AddRange(codeModel.GetStructInCurrentFile()); return classLists; }
protected override void OnExecute(Microsoft.VisualStudio.Shell.OleMenuCommand command) { if (VSTextView.ActiveTextView == null) { return; } long getElement = 0; long regionTime = 0; Stopwatch sw = new Stopwatch(); sw.Start(); //开始之前先格式化 VSBase.ExecuteCommand((uint)VSConstants.VSStd2KCmdID.FORMATDOCUMENT); VSStatusBar.SetText("quick region......"); using (VSUndo.StartUndo()) { SettingModel model = SettingFrm.ReadSetting(); if (model != null) { QuickRegionSettingModel quickRegionModel = model.QuickRegionModel; VSCodeModel codeModel = new VSCodeModel(); List<CodeElement> classLists = GetClassAndStructInFile(codeModel); for (int i = 0; i < classLists.Count; i++) { sw.Stop(); getElement = sw.ElapsedMilliseconds; sw.Restart(); List<CodeElement> noneEventElements = codeModel.GetNotRegionNoneEventMethodInClass(classLists[i]); if (noneEventElements.Count != 0) { RegionElement(noneEventElements, i, model.QuickRegionModel.Method); } List<CodeElement> eventElements = codeModel.GetNotRegionEventInClass(classLists[i]); if (eventElements.Count != 0) { RegionElement(eventElements, i, model.QuickRegionModel.Event); } List<CodeElement> constructorElements = codeModel.GetNotRegionConstructorInClass(classLists[i]); if (constructorElements.Count != 0) { RegionElement(constructorElements, i, model.QuickRegionModel.Constructor); } List<CodeElement> propertyElements = codeModel.GetNotRegionPropertyInClass(classLists[i]); if (propertyElements.Count != 0) { RegionElement(propertyElements, i, model.QuickRegionModel.Property); } List<CodeElement> variablesElements = codeModel.GetNotRegionVariablesInClass(classLists[i]); if (variablesElements.Count != 0) { RegionElement(variablesElements, i, model.QuickRegionModel.Variable); } sw.Stop(); regionTime = sw.ElapsedMilliseconds; } //QuickRegionpNonEventMethod(textView, quickRegionModel.Method); //QuickRegionpEventMethod(textView, quickRegionModel.Event); //QuickRegionConstructor(textView, quickRegionModel.Constructor); //QuickRegionpProperty(textView, quickRegionModel.Property); //QuickRegionDelegates(textView, "- Delegate -"); //QuickRegionVariables(textView, quickRegionModel.Variable); //CleanEmptyRegion(textView); CleanBlankLine(codeModel); VSDocument.SaveActiveDocument(); VSBase.ExecuteCommand((uint)VSConstants.VSStd2KCmdID.FORMATDOCUMENT); VSBase.ExecuteCommand((uint)VSConstants.VSStd2KCmdID.OUTLN_COLLAPSE_TO_DEF); } } VsOutput.ShowDebugMessage("region complete, get element time total: " + getElement + " milliseconds\r\n"+" region time: "+regionTime); }