本文整理汇总了C#中SyntaxNode.WithAdditionalAnnotations方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.WithAdditionalAnnotations方法的具体用法?C# SyntaxNode.WithAdditionalAnnotations怎么用?C# SyntaxNode.WithAdditionalAnnotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.WithAdditionalAnnotations方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSymbolToDeclarationAnnotationMap
private Dictionary<ISymbol, SyntaxAnnotation> CreateSymbolToDeclarationAnnotationMap(
IEnumerable<ISymbol> includedMembers,
ref Solution solution,
out List<DocumentId> documentIds,
SyntaxNode typeNode,
out SyntaxAnnotation typeNodeAnnotation,
CancellationToken cancellationToken)
{
var symbolToDeclarationAnnotationMap = new Dictionary<ISymbol, SyntaxAnnotation>();
var currentRoots = new Dictionary<SyntaxTree, SyntaxNode>();
documentIds = new List<DocumentId>();
var typeNodeRoot = typeNode.SyntaxTree.GetRoot(CancellationToken.None);
typeNodeAnnotation = new SyntaxAnnotation();
currentRoots[typeNode.SyntaxTree] = typeNodeRoot.ReplaceNode(typeNode, typeNode.WithAdditionalAnnotations(typeNodeAnnotation));
documentIds.Add(solution.GetDocument(typeNode.SyntaxTree).Id);
foreach (var includedMember in includedMembers)
{
var location = includedMember.Locations.Single();
var tree = location.SourceTree;
SyntaxNode root;
if (!currentRoots.TryGetValue(tree, out root))
{
root = tree.GetRoot(cancellationToken);
documentIds.Add(solution.GetDocument(tree).Id);
}
var token = root.FindToken(location.SourceSpan.Start);
var annotation = new SyntaxAnnotation();
symbolToDeclarationAnnotationMap.Add(includedMember, annotation);
currentRoots[tree] = root.ReplaceToken(token, token.WithAdditionalAnnotations(annotation));
}
foreach (var root in currentRoots)
{
var document = solution.GetDocument(root.Key);
solution = solution.WithDocumentSyntaxRoot(document.Id, root.Value, PreservationMode.PreserveIdentity);
}
return symbolToDeclarationAnnotationMap;
}
示例2: Visit
public override SyntaxNode Visit(SyntaxNode node)
{
if (node == _contextNode)
{
_seenContextNode = true;
// Annotate the context node so we can find it again in the new tree
// after we've added the close curly.
return node.WithAdditionalAnnotations(s_annotation);
}
// rewrite this node normally.
var rewritten = base.Visit(node);
if (rewritten == node)
{
return rewritten;
}
// This node changed. That means that something underneath us got
// rewritten. (i.e. we added the annotation to the context node).
Debug.Assert(_seenContextNode);
// Ok, we're past the context node now. See if this is a node with
// curlies. If so, if it has a missing close curly then add in the
// missing curly. Also, even if it doesn't have missing curlies,
// then still ask to format its close curly to make sure all the
// curlies up the stack are properly formatted.
var braces = rewritten.GetBraces();
if (braces.Item1.Kind() == SyntaxKind.None &&
braces.Item2.Kind() == SyntaxKind.None)
{
// Not an item with braces. Just pass it up.
return rewritten;
}
// See if the close brace is missing. If it's the first missing one
// we're seeing then definitely add it.
if (braces.Item2.IsMissing)
{
if (!_addedFirstCloseCurly)
{
var closeBrace = SyntaxFactory.Token(SyntaxKind.CloseBraceToken)
.WithAdditionalAnnotations(Formatter.Annotation);
rewritten = rewritten.ReplaceToken(braces.Item2, closeBrace);
_addedFirstCloseCurly = true;
}
}
else
{
// Ask for the close brace to be formatted so that all the braces
// up the spine are in the right location.
rewritten = rewritten.ReplaceToken(braces.Item2,
braces.Item2.WithAdditionalAnnotations(Formatter.Annotation));
}
return rewritten;
}