public IEnumerable<ICodeRegion> GetRegions(string src) { var ret = new List<ICodeRegion>(); var lines = src.Split(new char[] {'\n'}); CodeRegion currentCodeRegion = null; var regionStack = new Stack<CodeRegion>(); int lineNumber = 0; foreach (var line in lines) { if (line.Contains("#region")) { // start a new region var start = line.IndexOf("#region") + "#region".Length; var name = line.Substring(start).Trim(); currentCodeRegion = new CodeRegion(); currentCodeRegion.Name = name; currentCodeRegion.LineStart = lineNumber; // add as child of the last item in the stack if (regionStack.Count>0 && regionStack.Peek()!=null) { regionStack.Peek().Add(currentCodeRegion); } else { ret.Add(currentCodeRegion); } regionStack.Push(currentCodeRegion); } if (line.Contains("#endregion")) { if (regionStack.Peek() != null) { var c = regionStack.Pop(); c.LineEnd = lineNumber; } } lineNumber++; } return ret; }
public void Add(CodeRegion codeRegion) { _nestedRegions.Add(codeRegion); }