public static void windowEvents_WindowActivated(Window gotfocus, Window lostfocus) { if (BaseUCSetting.SettingModel.LessTabModel.OpenLessTab) { Document activeDocument = gotfocus.Document; if (activeDocument != null) { //当列表中的文件个数不足已经打开的文件个数时,则为启动项目的时候 //此时需要将所有已经打开的加入文件列表 //GetAllOpenedDocument的数量为已经打开新的窗口的数量,所以collection要加1 //比如现在有5个窗口已经打开,当我再次打开新的窗口的时候GetAllOpenedDocument此时已经为6了 //而ValidCount=5,所以这边就会永远成立。这会影响到后面isNewOpen的判断。 if (collection.ValidCount() + 1 < VSDocument.GetAllOpenedDocument().Count) { VSDocument.GetAllOpenedDocument().ForEach(doc => collection.Add(doc)); } //在加入到列表之前,先判断是否存在 bool isNewOpen = !collection.Contains(activeDocument); //不管是新打开的还是激活的,都重新排列列表 collection.Add(activeDocument); //只有新开了文档才调用关闭方法 if (isNewOpen) { //关闭超过数量的tab CloseTabNotInCollection(); } } } }
//private static string GetHeaderInfo(string text) //{ // if (text.StartsWith(prefix)) // { // int last = text.IndexOf(suffix); // string info = text.Substring(prefix.Length, last); // return info; // } // return string.Empty; //} public static void DocumentOpenedEvent(Document document) { //是新打开的文件 if (newOpenDocNameList.Contains(document.Name)) { //清除新打开的记录 newOpenDocNameList.Remove(document.Name); //只对.cs结尾的文件添加 if (document.FullName.ToLower().EndsWith(".cs") || document.FullName.ToLower().EndsWith(".js") || document.FullName.ToLower().EndsWith(".css") ) { //添加文件头信息 StringBuilder templete = new StringBuilder(BaseUCSetting.SettingModel.AutoHeaderModel.AutoHeaderTemplete); if (!string.IsNullOrEmpty(templete.ToString())) { templete = templete.Replace("[CurrentTime]", DateTime.Now.ToString()); templete = templete.Replace("[User]", Environment.UserName); //templete.Insert(0, prefix + "\n"); //templete.Append("\n " + suffix + "\n"); VSDocument.Insert(document, 1, 1, templete.ToString()); } } } // VsOutput.ShowGeneralMessage(document.Name + " 打开了"); }
public static void CloseTabNotInCollection() { List <Document> openedDocument = VSDocument.GetAllOpenedDocument(); foreach (Document item in openedDocument) { if (!collection.Contains(item) && !string.IsNullOrEmpty(item.FullName) && item.Windows.Count != 0 /*一定要有窗口才能关闭,针对打开Winform的bug*/) { VSDocument.CloseDocument(item.FullName); } } }
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); }