本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.InsertTriviaBefore方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.InsertTriviaBefore方法的具体用法?C# SyntaxNode.InsertTriviaBefore怎么用?C# SyntaxNode.InsertTriviaBefore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.InsertTriviaBefore方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyDocComment
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);
}
示例2: TraverseAndConvert
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;
}