我的注释在哪里? [英] Where is my annotation?

查看:76
本文介绍了我的注释在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对类、属性和方法进行注释,然后尝试检索带注释的节点时,只返回一个类.为什么?

When I try to annotate class, property and method, and then try to retrieve annotated node, only the class one is returned. Why?

这里是注释的代码

SyntaxAnnotation propertyAnnotation = null;
SyntaxAnnotation classAnnotation = null;
SyntaxAnnotation setMethodAnnotation = null;

document = document
    .AnnotateClass(classDeclaration, out classAnnotation)
    .AnnotateProperty(propertyDeclaration, out propertyAnnotation)
    .AnnotateSetMethod(setMethodDeclaration, out setMethodAnnotation);

我在 IDocument 上定义了这些扩展方法

I have these extension methods on IDocument defined

    internal static IDocument AnnotateSetMethod(this IDocument document, MethodDeclarationSyntax method,
                                                                        out SyntaxAnnotation annotation)
    {
        annotation = new SyntaxAnnotation();

        var newRoot = document.GetSyntaxRoot()
                                .ReplaceNode(method, method.WithAdditionalAnnotations(annotation));

        return document.UpdateSyntaxRoot(newRoot);

    }

    internal static IDocument AnnotateProperty(this IDocument document, PropertyDeclarationSyntax property,
                                               out SyntaxAnnotation annotation)
    {
        annotation = new SyntaxAnnotation();

        var newRoot = document.GetSyntaxRoot()
                                .ReplaceNode(property, property.WithAdditionalAnnotations(annotation));

        return document.UpdateSyntaxRoot(newRoot);
    }

    internal static IDocument AnnotateClass(this IDocument document, ClassDeclarationSyntax classDeclaration,
                                              out 

        SyntaxAnnotation annotation)
        {
            annotation = new SyntaxAnnotation();

            var newRoot = document.GetSyntaxRoot()
                                    .ReplaceNode(classDeclaration, classDeclaration.WithAdditionalAnnotations(annotation));

            return document.UpdateSyntaxRoot(newRoot);
        }

public static TSyntaxNode GetAnnotatedNode<TSyntaxNode>(this IDocument document, SyntaxAnnotation annotation)
                where TSyntaxNode : CommonSyntaxNode
            {
                return document.GetSyntaxRoot().GetAnnotatedNode<TSyntaxNode>(annotation);
            }

如果我这样做

var propertyDeclaration = document.GetAnnotatedNode<PropertyDeclarationSyntax>(propertyAnnotation);

我收到一个错误,但如果我尝试使用 ClassDeclarationSyntax,它可以正常工作.

I get an error, but if I try with ClassDeclarationSyntax it works fine.

推荐答案

我的水晶球告诉我,除了您的 AnnotateClass 之外,所有的 .Replace 节点调用都失败了.看看你得到的新根是否与旧根完全相同.

My crystal ball is telling me that the .Replace node calls in all but your AnnotateClass are failing. See if the new roots you get back are the exact same object as the old roots.

这是因为一旦您向类添加了注释,您现在就有了一个新树,因此您拥有的属性语法不再在"该树中——它是一个新节点.这是不变性的方式——一旦在某处创建了一个新节点,树中的所有节点实际上都是新的,因为您可以从任何其他节点到达任何节点.(正是因为这个问题,我们首先添加了语法注释....否则你会创建一些节点并且无法返回它们.)

This is because once you've added an annotation to the class, you now have a new tree, and so the property syntax you have is no longer "in" that tree -- it's a new node. This is the way of immutability -- once a new node is created somewhere, all the nodes in the tree are effectively new since you can get to any node from any other node. (It's because of this problem that we added syntax annotations in the first place....otherwise you'd make a few nodes and have no way to get back to them.)

您有几种方法可以解决这个问题:

You have a few ways to approach this:

  1. 使用 SyntaxRewriter,您可以在一个阶段完成全部重写.您可以覆盖 VisitClass、VisitProperty 等,并一次性生成带有注释的新节点.
  2. 不是调用 ReplaceNode,而是调用 ReplaceNodes,您可以一次替换所有三个节点.

在任何一种情况下,出于性能原因,进行一次重写总是比一堆重写更可取.就像我说的,一旦你替换了树中的一个节点并取回了一个新的根,你的所有实例都已经改变,可能需要重新创建.这很昂贵并且会产生内存压力.

In either case, doing a single rewrite is always preferable to a bunch of rewrites for performance reasons. Like I said, once you replace a node in a tree and get back a new root, all your instances have changed and may have to be re-created. This is expensive and produces memory pressure.

[从技术上讲,这种说法是谎言:我们确实懒惰地构建东西并重用很多东西.但改写得越少越好.]

[Technically, that statement is a lie: we do build stuff lazily and reuse lots stuff. But the less rewrites the better.]

这篇关于我的注释在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆