鉴于以下“代码修复”,
我正在尝试替换属性的文档元素,并将其替换为我的快速修复文档。
我在琐事中挣扎
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);