本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.GetAnnotatedNodesAndTokens方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetAnnotatedNodesAndTokens方法的具体用法?C# SyntaxNode.GetAnnotatedNodesAndTokens怎么用?C# SyntaxNode.GetAnnotatedNodesAndTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetAnnotatedNodesAndTokens方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAnnotations
private void TestAnnotations(
string expectedText,
IList<TextSpan> expectedSpans,
SyntaxNode fixedRoot,
string annotationKind,
bool compareTokens,
ParseOptions parseOptions = null)
{
expectedSpans = expectedSpans ?? new List<TextSpan>();
var annotatedTokens = fixedRoot.GetAnnotatedNodesAndTokens(annotationKind).Select(n => (SyntaxToken)n).ToList();
Assert.Equal(expectedSpans.Count, annotatedTokens.Count);
if (expectedSpans.Count > 0)
{
var expectedTokens = TokenUtilities.GetTokens(TokenUtilities.GetSyntaxRoot(expectedText, GetLanguage(), parseOptions));
var actualTokens = TokenUtilities.GetTokens(fixedRoot);
for (var i = 0; i < Math.Min(expectedTokens.Count, actualTokens.Count); i++)
{
var expectedToken = expectedTokens[i];
var actualToken = actualTokens[i];
var actualIsConflict = annotatedTokens.Contains(actualToken);
var expectedIsConflict = expectedSpans.Contains(expectedToken.Span);
Assert.Equal(expectedIsConflict, actualIsConflict);
}
}
}
示例2: AnnotationResolver
private SyntaxToken AnnotationResolver(
SyntaxNode node,
TriviaLocation location,
SyntaxAnnotation annotation,
SyntaxNode callsite,
MethodDeclarationSyntax method)
{
var token = node.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault().AsToken();
if (token.RawKind != 0)
{
return token;
}
switch (location)
{
case TriviaLocation.BeforeBeginningOfSpan:
return callsite.GetFirstToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true);
case TriviaLocation.AfterEndOfSpan:
return callsite.GetLastToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true);
case TriviaLocation.AfterBeginningOfSpan:
return method.Body.OpenBraceToken.GetNextToken(includeZeroWidth: true);
case TriviaLocation.BeforeEndOfSpan:
return method.Body.CloseBraceToken.GetPreviousToken(includeZeroWidth: true);
}
return Contract.FailWithReturn<SyntaxToken>("can't happen");
}
示例3: ResolveAnnotation
private static SyntaxToken ResolveAnnotation(
SyntaxNode root,
TriviaLocation location,
SyntaxAnnotation annotation)
{
return root.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault().AsToken();
}
示例4: RemoveAnnotationIfExists
private static SyntaxNode RemoveAnnotationIfExists(SyntaxNode root, SyntaxAnnotation annotation)
{
var element = root.GetAnnotatedNodesAndTokens(annotation).FirstOrDefault();
if (element == default(SyntaxNodeOrToken))
{
return root;
}
if (element.IsNode)
{
var node = element.AsNode();
return root.ReplaceNode(
node,
node.WithoutAnnotations(annotation));
}
var token = element.AsToken();
return root.ReplaceToken(
token,
token.WithoutAnnotations(annotation));
}
示例5: GetAnnotatedSpans
private static IEnumerable<TextSpan> GetAnnotatedSpans(SyntaxNode node, SyntaxAnnotation annotation)
{
foreach (var nodeOrToken in node.GetAnnotatedNodesAndTokens(annotation))
{
var firstToken = nodeOrToken.IsNode ? nodeOrToken.AsNode().GetFirstToken(includeZeroWidth: true) : nodeOrToken.AsToken();
var lastToken = nodeOrToken.IsNode ? nodeOrToken.AsNode().GetLastToken(includeZeroWidth: true) : nodeOrToken.AsToken();
var previousToken = firstToken.GetPreviousToken();
var nextToken = lastToken.GetNextToken();
if (previousToken.RawKind != 0)
{
firstToken = previousToken;
}
if (nextToken.RawKind != 0)
{
lastToken = nextToken;
}
yield return TextSpan.FromBounds(firstToken.SpanStart, lastToken.Span.End);
}
}
示例6: GetAnnotatedSpans
private static IEnumerable<TextSpan> GetAnnotatedSpans(SyntaxNode node, SyntaxAnnotation annotation)
{
foreach (var nodeOrToken in node.GetAnnotatedNodesAndTokens(annotation))
{
var firstToken = nodeOrToken.IsNode ? nodeOrToken.AsNode().GetFirstToken(includeZeroWidth: true) : nodeOrToken.AsToken();
var lastToken = nodeOrToken.IsNode ? nodeOrToken.AsNode().GetLastToken(includeZeroWidth: true) : nodeOrToken.AsToken();
yield return GetSpan(firstToken, lastToken);
}
}