本文整理汇总了C#中SyntaxNode.GetLeadingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetLeadingTrivia方法的具体用法?C# SyntaxNode.GetLeadingTrivia怎么用?C# SyntaxNode.GetLeadingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetLeadingTrivia方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleDeclarationAsync
private async Task<Document> HandleDeclarationAsync(Document document, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var declarationContext = node.Parent;
TypeSyntax typeSyntax = null;
if (declarationContext is VariableDeclarationSyntax)
{
typeSyntax = ((VariableDeclarationSyntax)declarationContext).Type;
}
else if (declarationContext is ForEachStatementSyntax)
{
typeSyntax = ((ForEachStatementSyntax)declarationContext).Type;
}
else
{
Contract.Fail($"unhandled kind {declarationContext.Kind().ToString()}");
}
var typeSymbol = semanticModel.GetTypeInfo(typeSyntax).ConvertedType;
var typeName = typeSymbol.GenerateTypeSyntax()
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
Debug.Assert(!typeName.ContainsDiagnostics, "Explicit type replacement likely introduced an error in code");
var newRoot = root.ReplaceNode(node, typeName);
return document.WithSyntaxRoot(newRoot);
}
示例2: ReplaceTypeWithVarAsync
private static Task<Document> ReplaceTypeWithVarAsync(Document document, SyntaxNode root, SyntaxNode node)
{
var implicitType = SyntaxFactory.IdentifierName("var")
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
var newRoot = root.ReplaceNode(node, implicitType);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例3: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(
Document document, SemanticModel model, SyntaxNode root,
SyntaxNode nodeToFix, string diagnosticId, CancellationToken cancellationToken)
{
ArrayTypeSyntax arrayType = GetArrayType(nodeToFix);
if (arrayType != null)
{
TypeSyntax elementType = arrayType.ElementType.WithoutLeadingTrivia().WithoutTrailingTrivia();
if (arrayType.RankSpecifiers.Count > 1)
{
elementType = SyntaxFactory.ArrayType(elementType, SyntaxFactory.List(arrayType.RankSpecifiers.Skip(1)));
}
InvocationExpressionSyntax syntax = InvokeStaticGenericParameterlessMethod(
model.Compilation.GetTypeByMetadataName("System.Array"),
EmptyArrayDiagnosticAnalyzer.ArrayEmptyMethodName,
elementType.WithoutLeadingTrivia().WithoutTrailingTrivia());
if (nodeToFix.HasLeadingTrivia)
{
syntax = syntax.WithLeadingTrivia(nodeToFix.GetLeadingTrivia());
}
if (nodeToFix.HasTrailingTrivia)
{
syntax = syntax.WithTrailingTrivia(nodeToFix.GetTrailingTrivia());
}
if (syntax != null)
{
root = root.ReplaceNode(nodeToFix, syntax);
document = document.WithSyntaxRoot(root);
}
}
return Task.FromResult(document);
}
示例4: GetLeadingWhitespaceLength
private static int GetLeadingWhitespaceLength(SyntaxNode node)
{
if (!(node is IXmlElement))
{
return 0;
}
var leadingTrivia = node.GetLeadingTrivia();
if (leadingTrivia == null)
{
return 0;
}
int totalLength = 0;
foreach (var child in leadingTrivia.ChildNodes)
{
if (child.Kind == SyntaxKind.WhitespaceTrivia)
{
totalLength += child.FullWidth;
}
}
return totalLength;
}
示例5: CollectCommentRegions
public static void CollectCommentRegions(SyntaxNode node, List<OutliningSpan> spans)
{
if (node == null)
{
throw new ArgumentNullException("node");
}
var triviaList = node.GetLeadingTrivia();
CollectCommentRegions(triviaList, spans);
}
示例6: CollectCommentBlockSpans
public static void CollectCommentBlockSpans(
SyntaxNode node, ArrayBuilder<BlockSpan> spans)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
var triviaList = node.GetLeadingTrivia();
CollectCommentBlockSpans(triviaList, spans);
}
示例7: IsAutogenerated
public static bool IsAutogenerated(SyntaxNode contents)
{
return contents.GetLeadingTrivia().Any(x => x.ToFullString().Contains("<auto-generated"));
}
示例8: Visit
public override SyntaxNode Visit(SyntaxNode node)
{
_cancellationToken.ThrowIfCancellationRequested();
if (node == null)
{
return node;
}
// Process children first
node = base.Visit(node);
// Check the leading trivia for doc comments.
if (node.GetLeadingTrivia().Any(SyntaxKind.SingleLineDocumentationCommentTrivia))
{
var newLeadingTrivia = new List<SyntaxTrivia>();
foreach (var trivia in node.GetLeadingTrivia())
{
if (trivia.Kind() == SyntaxKind.SingleLineDocumentationCommentTrivia)
{
newLeadingTrivia.Add(SyntaxFactory.Comment("//"));
newLeadingTrivia.Add(SyntaxFactory.ElasticCarriageReturnLineFeed);
var structuredTrivia = (DocumentationCommentTriviaSyntax)trivia.GetStructure();
newLeadingTrivia.AddRange(ConvertDocCommentToRegularComment(structuredTrivia));
}
else
{
newLeadingTrivia.Add(trivia);
}
}
node = node.WithLeadingTrivia(newLeadingTrivia);
}
return node;
}
示例9: RemoveComments
private static SyntaxNode RemoveComments(SyntaxNode ele)
{
if (ele.HasLeadingTrivia)
{
var oldCommentsTrivias = from trivia in ele.GetLeadingTrivia().Where(
x => (x.Kind == SyntaxKind.MultiLineCommentTrivia || x.Kind == SyntaxKind.SingleLineCommentTrivia))
select trivia;
foreach (var comment in oldCommentsTrivias)
{
ele = ele.ReplaceTrivia(comment, SyntaxTriviaList.Empty);
}
}
if (ele.DescendantTrivia() != null && ele.DescendantTrivia().Count() > 0)
{
var containingComments = from trivia in ele.DescendantTrivia().Where(
x => (x.Kind == SyntaxKind.MultiLineCommentTrivia || x.Kind == SyntaxKind.SingleLineCommentTrivia))
select trivia;
foreach (var comment in containingComments)
{
ele = ele.ReplaceTrivia(comment, SyntaxTriviaList.Empty);
}
}
return ele;
}