private SyntaxNode ApplyDocComment(SyntaxNode node, string docCommentId) { if (docCommentId == null) return node; // Look up the comment text string docCommentText = GetDocCommentForId(docCommentId); // Get the SyntaxTrivia for the comment SyntaxTree newTree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(docCommentText); var newTrivia = newTree.GetRoot().GetLeadingTrivia(); if (node.HasLeadingTrivia) { SyntaxTriviaList triviaList = node.GetLeadingTrivia(); // Check to see if there are any existing doc comments var docComments = triviaList .Where(n => n.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia) || n.IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia)) .Select(t => t.GetStructure()) .OfType<DocumentationCommentTriviaSyntax>() .ToList(); // Append the doc comment (even if the API already has /// comments) node = node.InsertTriviaBefore(triviaList.First(), newTrivia); } else // no leading trivia { node = node.WithLeadingTrivia(newTrivia); } return node.WithAdditionalAnnotations(Simplifier.Annotation); }
public SyntaxNode TraverseAndConvert(SyntaxNode node, SyntaxNode newNode) { // Step 1: Handle current node // Find out if this node is a documentable API declaration // If not, skip to go to the child nodes. string docCommentId = GetAPIForNode(node); if (docCommentId != null) { // Look up the comment text string docCommentText = GetDocCommentForId(docCommentId); // Get the SyntaxTrivia for the comment SyntaxTree newTree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(docCommentText); var newTrivia = newTree.GetRoot().GetLeadingTrivia(); // Read a doc comment from a syntax tree. //var classNode = (ClassDeclarationSyntax)newTree.GetRoot().ChildNodes().First(); //var newTrivia = classNode.GetLeadingTrivia(); //var docCommentTrivia = newTrivia.Single(t => t.Kind() == SyntaxKind.SingleLineDocumentationCommentTrivia || // t.Kind() == SyntaxKind.MultiLineDocumentationCommentTrivia); // Find out if there is an existing comment or doc comment if (node.HasLeadingTrivia) { SyntaxTriviaList triviaList = node.GetLeadingTrivia(); SyntaxTrivia firstComment = triviaList.Last(); foreach (var trivia in triviaList.Reverse()) { SyntaxKind kind = trivia.Kind(); switch (kind) { case SyntaxKind.SingleLineCommentTrivia: case SyntaxKind.MultiLineCommentTrivia: // Found existing comment firstComment = trivia; break; case SyntaxKind.MultiLineDocumentationCommentTrivia: case SyntaxKind.SingleLineDocumentationCommentTrivia: // Found existing XML doc comment firstComment = trivia; break; default: break; } } // Append the doc comment newNode = node.InsertTriviaBefore(firstComment, newTrivia); } else // no leading trivia { newNode = node.WithLeadingTrivia(newTrivia); } } else // not an API node { newNode = node; } if (node.ChildNodes().Count() > 0) { newNode = newNode.ReplaceNodes(newNode.ChildNodes(), TraverseAndConvert); } return newNode; }