4

鉴于以下“代码修复”,
我正在尝试替换属性的文档元素,并将其替换为我的快速修复文档。

我在琐事中挣扎

    private Task<Document> AddDocumentationAsync(CodeFixContext context, SyntaxNode root, DocumentationCommentTriviaSyntax xmlElementSyntax)
    {
        const string comment = "THIS IS THE NEW COMMENT";
        var text =
            SyntaxFactory.XmlText(SyntaxFactory.TokenList(
                SyntaxFactory.XmlTextLiteral(
                    SyntaxFactory.TriviaList(),
                    comment,
                    comment,
                    SyntaxFactory.TriviaList())));

        var textList = SyntaxFactory.List<XmlNodeSyntax>(new[] {text});
        XmlNodeSyntax[] nodes = new XmlNodeSyntax[]
        {
            SyntaxFactory.XmlElement(
                SyntaxFactory.XmlElementStartTag(SyntaxFactory.XmlName("summary")),
                textList,
                SyntaxFactory.XmlElementEndTag(SyntaxFactory.XmlName("summary"))),
        };
        var list = SyntaxFactory.List<XmlNodeSyntax>(nodes);
        var newDocumentation = SyntaxFactory.DocumentationCommentTrivia(
            SyntaxKind.SingleLineDocumentationCommentTrivia,
            list);

        var spaced = newDocumentation.WithTriviaFrom(xmlElementSyntax);
        var n1 = root.ReplaceNode(xmlElementSyntax, spaced);

        var newDocument = context.Document.WithSyntaxRoot(n1);
        return Task.FromResult(newDocument);
    }

我得到这个输出。

在此处输入图像描述

如何确保<summary在正确的位置之前有一个空格和换行符,并从新行public string开始。IE

    /// <summary>
    /// THIS IS THE NEW COMMENT
    /// </summary>
    public string Test { get; set; }

我的注册码是这样的。

    var documentationStructure = node.GetDocumentationCommentTriviaSyntax();
    var xmlElementSyntax = documentationStructure;
    var action = CodeAction.Create("Create XML Documentation", c => this.AddDocumentationAsync(context, root, xmlElementSyntax), "Create XML Documentation");
    context.RegisterCodeFix(
        action,
        diagnostic);
4

1 回答 1

4

您需要EndOfDocumentationCommentToken在评论中添加一个标记。您可以尝试以下方法:.WithEndOfComment(SyntaxFactory.Token(SyntaxKind.EndOfDocumentationCommentToken)).

顺便说一句,您可以查看此在线工具以供参考。

于 2015-11-11T15:15:24.063 回答