static void InsertComment(ref AstNode insertionPoint, AstNode newNode, Role role, bool isDocumentationComment, AstNode rootNode) { TextLocation insertAt = newNode.StartLocation; // Advance insertionPoint to the first node that has a start location >= insertAt while (insertionPoint != null && insertionPoint.StartLocation < insertAt) { // Enter the current node if insertAt is within while (insertAt < insertionPoint.EndLocation && insertionPoint.FirstChild != null) { insertionPoint = insertionPoint.FirstChild; } // Go to next node (insertionPoint.NextSibling if it exists; otherwise the next sibling of the parent node etc.) insertionPoint = insertionPoint.GetNextNode(); } // As a special case, XmlDoc gets inserted at the beginning of the entity declaration if (isDocumentationComment && insertionPoint is EntityDeclaration && insertionPoint.FirstChild != null) { insertionPoint = insertionPoint.FirstChild; } if (insertionPoint == null) { // we're at the end of the compilation unit rootNode.AddChildUnsafe(newNode, role); } else { insertionPoint.Parent.InsertChildBeforeUnsafe(insertionPoint, newNode, role); } }