private static SyntaxNode MakeSingleFieldReadonly(SyntaxNode root, FieldDeclarationSyntax fieldDeclaration) { var newFieldDeclaration = fieldDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)) .WithTrailingTrivia(fieldDeclaration.GetTrailingTrivia()) .WithLeadingTrivia(fieldDeclaration.GetLeadingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); var newRoot = root.ReplaceNode(fieldDeclaration, newFieldDeclaration); return newRoot; }
private async Task<Document> MakeReadOnlyAsync(Document document, FieldDeclarationSyntax fieldDecl, CancellationToken cancellationToken) { var roKeyword = SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword); var roFieldDecl = fieldDecl.AddModifiers(roKeyword); var root = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(fieldDecl, roFieldDecl); var newDocument = document.WithSyntaxRoot(newRoot); return newDocument; }