public static void AddRegion(DTE dte, string[] predefinedRegions) { Document doc = dte.ActiveDocument; Language language = Utilities.GetLanguage(doc); if (IsSupportedLanguage(language) && predefinedRegions.Length > 0) { string regionName = GetRegionName(predefinedRegions); if (!string.IsNullOrEmpty(regionName)) { TextSelection sel = (TextSelection)doc.Selection; EditPoint startpoint = sel.TopPoint.CreateEditPoint(); EditPoint endpoint = sel.BottomPoint.CreateEditPoint(); bool hasSelection = true; if (startpoint.EqualTo(endpoint)) { hasSelection = false; } if (!startpoint.AtStartOfLine) { startpoint.StartOfLine(); } bool closeUndoContext = false; if (!dte.UndoContext.IsOpen) { closeUndoContext = true; dte.UndoContext.Open("Add Region" + TextDocumentHandler.UndoContextSuffix, false); } try { string padding = GetPadding(startpoint, hasSelection); MakeRegion(doc, regionName, startpoint, endpoint, padding, language, hasSelection); } finally { if (closeUndoContext) { dte.UndoContext.Close(); } } } } }