用 roslyn fix 替换评论(在正确的地方添加琐事)
replacing comments with roslyn fix (adding trivia in the correct place)
给定以下 'Code Fix'
我正在尝试替换 属性 的文档元素,并将其替换为我的快速修复文档。
我正在为琐事
而苦苦挣扎
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
之前有一个space,换行符在正确的位置,并且public string
在一个新行开始。即
/// <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);
您需要在评论中添加一个 EndOfDocumentationCommentToken
标记。您可以尝试以下操作:.WithEndOfComment(SyntaxFactory.Token(SyntaxKind.EndOfDocumentationCommentToken))
.
对了,大家可以看看this online tool参考一下。
给定以下 'Code Fix'
我正在尝试替换 属性 的文档元素,并将其替换为我的快速修复文档。
我正在为琐事
而苦苦挣扎 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
之前有一个space,换行符在正确的位置,并且public string
在一个新行开始。即
/// <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);
您需要在评论中添加一个 EndOfDocumentationCommentToken
标记。您可以尝试以下操作:.WithEndOfComment(SyntaxFactory.Token(SyntaxKind.EndOfDocumentationCommentToken))
.
对了,大家可以看看this online tool参考一下。