public void UseSyntaxAnnotations()
{
var tree = SyntaxTree.ParseText(@"
using System;
class Program
{
static void Main()
{
int i = 0; Console.WriteLine(i);
}
}");
// Tag all tokens that contain the letter 'i'.
var rewriter = new MyAnnotator();
SyntaxNode oldRoot = tree.GetRoot();
SyntaxNode newRoot = rewriter.Visit(oldRoot);
Assert.IsFalse(oldRoot.ContainsAnnotations);
Assert.IsTrue(newRoot.ContainsAnnotations);
// Find all tokens that were tagged with annotations of type MyAnnotation.
IEnumerable<CommonSyntaxNodeOrToken> annotatedTokens = newRoot.GetAnnotatedNodesAndTokens<MyAnnotation>();
var results = string.Join("\r\n",
annotatedTokens.Select(nodeOrToken =>
{
Assert.IsTrue(nodeOrToken.IsToken);
MyAnnotation annotation = nodeOrToken.GetAnnotations<MyAnnotation>().Single();
return string.Format("{0} (position {1})", nodeOrToken.ToString(), annotation.Position);
}));
Assert.AreEqual(@"using (position 2)
static (position 4)
void (position 2)
Main (position 2)
int (position 0)
i (position 0)
WriteLine (position 2)
i (position 0)", results);
}