private bool NeedEndregion(TextDocument document) { int regions = 0; int endregions = 0; for (int i = 1; i <= document.LineCount; i++) { string text = document.GetLineAsString(i).Trim(); if (text.StartsWith("#region")) { ++regions; } else if (text.StartsWith("#endregion")) { ++endregions; } } return regions > endregions; }