/// <summary> /// Indent according to bracket hierarchy /// </summary> /// <param name="textDocument">The text document to cleanup.</param> internal void InsertIndent(TextDocument textDocument) { TextPoint cursor = textDocument.StartPoint.CreateEditPoint(); IndentSquareBracket bracket = new IndentSquareBracket(cursor.Parent.StartPoint, cursor.Parent.EndPoint, null); bracket.FindBracket(); bracket.Execute(); }
public void FindBracket() { EditPoint EndPoint = null; TextRanges dummy = null; EditPoint StartPoint = Start.CreateEditPoint(); StartPoint.LineDown(); //FindPattern 不能指定结束位置 while (StartPoint.FindPattern(MaximumBracketPattern, StandardFindOptions, ref EndPoint, ref dummy)) { //所以,需要 判读匹配的开始位置是否在,被包含的区间外 if (StartPoint.Line >= End.Line) { break;//在外面,则跳出,转入数组的下一个 } if (StartPoint != null && EndPoint != null) { IndentSquareBracket Child = new IndentSquareBracket(StartPoint, EndPoint, this); StartPoint.StartOfLine(); int LineIndentCount; //当父节区间为空时,取父区间的缩进,否则在父区间的基础上+1,逐级实现缩进 if (Parent == null) { string LineText = StartPoint.GetText(StartPoint.LineLength); string textTemp = LineText.TrimStart('\t'); LineIndentCount = LineText.Length - textTemp.Length;//获取当前的缩进深度 } else { LineIndentCount = IndentCount + 1; } Child.IndentCount = LineIndentCount; Children.Add(Child); //子Bracket递归查找 Child.FindBracket(); } //再从区间的结束位置向后在下一个最大的包含区间 StartPoint = EndPoint; } }