-
Hi Roslyn enthusiasts, I have these two approaches to add private async Task<Document> FixSummaryAsync(Document document, FieldDeclarationSyntax localFieldDeclaration, string comment, CancellationToken cancellationToken)
{
SemanticModel? semanticModel = await document.GetSemanticModelAsync(cancellationToken);
// #1
SyntaxTriviaList newDoc1 = SyntaxFactory.ParseLeadingTrivia($"/// <summary>{comment}</summary>\n");
// #2
XmlNodeSyntax summary = SyntaxFactory.XmlElement(
SyntaxFactory.XmlElementStartTag(SyntaxFactory.XmlName("summary")),
SyntaxFactory.List<XmlNodeSyntax>(new[] {
SyntaxFactory.XmlText(SyntaxFactory.TokenList(
SyntaxFactory.XmlTextLiteral(
SyntaxFactory.TriviaList(),
text: comment,
value: comment,
SyntaxFactory.TriviaList())))
}),
SyntaxFactory.XmlElementEndTag(SyntaxFactory.XmlName("summary"))
);
SyntaxTriviaList newDoc2 = SyntaxFactory.TriviaList(
SyntaxFactory.Trivia(SyntaxFactory.DocumentationComment(summary, SyntaxFactory.XmlText("\n")))
);
DocumentationCommentTriviaSyntax? documentation = localFieldDeclaration.GetDocumentationCommentTriviaSyntax();
if (documentation is null)
{
SyntaxNode? replacement = localFieldDeclaration.WithLeadingTrivia(newDoc1);
// Replace the old local declaration with the new local declaration.
SyntaxNode? oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
SyntaxNode? newRoot = oldRoot!.ReplaceNode(localFieldDeclaration, replacement);
return document.WithSyntaxRoot(newRoot);
}
return document;
} For some reason approach no. 2 adds also preserves correct indentation whereas approach no. 1 removes indentation of the field declaration. Does anybody know how to fix approach no. 1? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think there are some important things missing from the code you showed: |
Beta Was this translation helpful? Give feedback.
-
This helps me to resolve my issue: Replace
with
|
Beta Was this translation helpful? Give feedback.
This helps me to resolve my issue:
Replace
SyntaxNode? newRoot = oldRoot!.ReplaceNode(localFieldDeclaration, replacement);
with
SyntaxNode? newRoot = oldRoot!.ReplaceNode(localFieldDeclaration, replacement.WithAdditionalAnnotations(Formatter.Annotation));