/// <summary> /// Returns the root nodes of all subtrees that are fully contained between <paramref name="start"/> and <paramref name="end"/> (inclusive). /// </summary> public IEnumerable <AstNode> GetNodesBetween(TextLocation start, TextLocation end) { AstNode node = this; while (node != null) { AstNode next; if (start <= node.StartLocation && node.EndLocation <= end) { // Remember next before yielding node. // This allows iteration to continue when the caller removes/replaces the node. next = node.GetNextNode(); yield return(node); } else { if (node.EndLocation <= start) { next = node.GetNextNode(); } else { next = node.FirstChild; } } if (next != null && next.StartLocation > end) { yield break; } node = next; } }
void FixAttributesAndDocComment(EntityDeclaration entity) { var node = entity.FirstChild; while (node != null && node.Role == Roles.Comment) { node = node.GetNextSibling(NoWhitespacePredicate); FixIndentation(node); } if (entity.Attributes.Count > 0) { AstNode n = null; entity.Attributes.First().AcceptVisitor(this); foreach (var attr in entity.Attributes.Skip(1)) { FixIndentation(attr); attr.AcceptVisitor(this); n = attr; } if (n != null) { FixIndentation(n.GetNextNode(NoWhitespacePredicate)); } else { FixIndentation(entity.Attributes.First().GetNextNode(NoWhitespacePredicate)); } } }
void FixOpenBrace(BraceStyle braceStyle, AstNode lbrace) { if (lbrace.IsNull) { return; } switch (braceStyle) { case BraceStyle.DoNotChange: return; case BraceStyle.BannerStyle: case BraceStyle.EndOfLine: var prev = lbrace.GetPrevNode(NoWhitespacePredicate); if (prev is PreProcessorDirective) { return; } int prevOffset = document.GetOffset(prev.EndLocation); if (prev is Comment || prev is PreProcessorDirective || prev is InlineAsmNode) { int next = document.GetOffset(lbrace.GetNextNode().StartLocation); EnsureText(prevOffset, next, ""); while (prev is Comment || prev is PreProcessorDirective || prev is InlineAsmNode) { prev = prev.GetPrevNode(); } prevOffset = document.GetOffset(prev.EndLocation); AddChange(prevOffset, 0, " {"); } else { int braceOffset2 = document.GetOffset(lbrace.StartLocation); EnsureText(prevOffset, braceOffset2, " "); } break; case BraceStyle.EndOfLineWithoutSpace: prev = lbrace.GetPrevNode(NoWhitespacePredicate); if (prev is PreProcessorDirective) { return; } prevOffset = document.GetOffset(prev.EndLocation); int braceOffset = document.GetOffset(lbrace.StartLocation); EnsureText(prevOffset, braceOffset, ""); break; case BraceStyle.NextLine: prev = lbrace.GetPrevNode(NoWhitespacePredicate); if (prev is PreProcessorDirective) { return; } prevOffset = document.GetOffset(prev.EndLocation); braceOffset = document.GetOffset(lbrace.StartLocation); EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString); break; case BraceStyle.NextLineShifted: prev = lbrace.GetPrevNode(NoWhitespacePredicate); if (prev is PreProcessorDirective) { return; } prevOffset = document.GetOffset(prev.EndLocation); braceOffset = document.GetOffset(lbrace.StartLocation); curIndent.Push(IndentType.Block); EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString); curIndent.Pop(); break; case BraceStyle.NextLineShifted2: prev = lbrace.GetPrevNode(NoWhitespacePredicate); if (prev is PreProcessorDirective) { return; } prevOffset = document.GetOffset(prev.EndLocation); braceOffset = document.GetOffset(lbrace.StartLocation); curIndent.Push(IndentType.Block); EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString); curIndent.Pop(); break; } }